adding a node to a Binary Tree from left to right

Started by
22 comments, last by Timkin 18 years, 1 month ago
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?

Beginner in Game Development?  Read here. And read here.

 

Advertisement
I have no idea what you just asked.
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?

Beginner in Game Development?  Read here. And read here.

 

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?
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.

Beginner in Game Development?  Read here. And read here.

 

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.
It seems like the grasshopper will be in the temple a little longer than intended.

Beginner in Game Development?  Read here. And read here.

 

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.
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?
I'm not gonna lie to you... I don't get the code you just posted.

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement