I'm studying trees right now, and I'd like to find some examples of how to create a binary(or k-ary) tree one level at a time, since I can't find it anywhere and my brain isn't working.
Anyone have any input on this? C's lack of functions doesn't help either. Thanks ahead, /prog/
Name:
Anonymous2010-05-25 13:27
One level at a time? Keep a running count of the lowest depth of the tree and how many nodes are on that level. Save the root, it should always be k*x where k is the arbitrary number of leaves any node can have and x is the depth. If you have added k*x leaves to the given level, reset the counter to zero and then start adding nodes to the next level.
If the method is not of your concern and its only the results in which you are interested, add the new node anywhere, count for the level above the new node, then rebalance the tree if need be.