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

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -