function - How do I build a binary tree in Java using this method? -
i have recursive java method builds binary tree:
bt build() { return(a[++k] == 0 ? null : new bt(build(), build())); }
bt class looks this:
class bt { bt l; bt r; bt(bt l, bt r) { l = l; r = r; } }
how build class work? if wanted build tree n nodes, how many times build function called in terms of n?
each call of build
function either creates node or returns null.
there n+1 null pointers in binary tree n nodes. because each node has 2 outgoing edges , each node, except root, has 1 incoming edge.
this gives 2*n+1 calls of build
create tree n nodes.
Comments
Post a Comment