# adding a node to a Binary Tree from left to right

This topic is 4512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

now I'm talking about plain ol' Binary Trees, not Binary Search/Sort Trees. I wanted to know (without using arrays as my base) if there is a way to add a node and have it insert and fill the tree from left to right. I've gotten as far as a 2-level depth but afterwards I get stuck. It just keeps adding nodes to the left. Any suggestions or is this an impossibility without using one of the speciality binary trees?

##### Share on other sites
I have no idea what you just asked.

##### Share on other sites
Ok... let me try again.

In my binary tree, I want to add a node and have it automatically populate the tree from left to right. Then go down a level, again add itself to the tree from left to right.
so...
       a                      a   /   \                  /    b      c   then         b     c                        /                       d

and so forth.

Clearer?

##### Share on other sites
Ok. Give your tree an extra bit of information: each node keeps track of the minimum depth of each of its sub trees. If a node has no subtrees its depth is 0. If it has two subtrees its depth is min(depth(left), depth(right)). Can you see how you could use that information to decide where to add the next node?

##### Share on other sites
ok... so checkDepth(), if a node has a depth of 0, add a left node. then checkDepth() again, if depth != 0 add a right node.

i take it, this will be a recursive task.

##### Share on other sites
Not really. That would cause you to create a tree where you build on the right with only one left node along the spine. Think of depth as a balancing factor. Also, it can be done iteratively.

##### Share on other sites
It seems like the grasshopper will be in the temple a little longer than intended.

##### Share on other sites
I foresee a complication if the subtrees have equal length.

Here is an idea I'll throw out.

// assume node is a pointer typevoid add(node tree, node newbie) { list<node> nodelist int depth = 1 list.pushback(tree)  while( !add_node (nodelist, newbie, depth) );}bool add_node (list&, node, depth&) { for i from 0 to depth:   temp = list.popfront()   if either temp.child == null:      that temp.child = node      return true   else      list.pushback(temp.leftchild)      list.pushback(temp.rightchild)   depth = depth*2}

Basically scan down the tree one level at a time, keeping track of all the nodes on a horizontal level, and adding the node to the first null encountered from left to right.

##### Share on other sites
No, there are no complications if the two subtrees have equal depth. Draw out a tree where the two subtrees have equal depth that has been created by this type of algorithm. Do you see where the node would go?

##### Share on other sites
I'm not gonna lie to you... I don't get the code you just posted.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
15
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002300
×