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' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -