views:

120

answers:

4

Hi,

I already have a working binary tree database. Unfortunately, it needs to have the ability to balance itself. I don't want to rewrite the whole thing, I just want to include a function that will balance the tree. Any algorithms or ideas?

A: 

look for balanced trees like avl red black

Anonymous
+2  A: 

AVL and RedBlack trees are self balancing trees. You can traverse your original tree and insert the nodes in these trees. Afterwards you can keep the new tree and discard your original tree.

Aman Jain
I think what the OP had in mind was to keep all his code and just add a function that balances his regular binary tree.
JoelFan
+1  A: 

I have found the Stanford libavl tutorial quite helpful.

Check out the examples in AVL tree wiki.

Also, try to play with the AVL tree animations available in the web, such as

ArunSaha
+1  A: 

AVL and Red-Black trees are balanced binary trees. I have an implementation of AVL trees. Look here. It supports insertion and search. Deletion is not implemented yet.

Appu