![]() |
|
Help with an AVL TREE - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Help with an AVL TREE (/Thread-Help-with-an-AVL-TREE) Pages:
1
2
|
RE: Help with an AVL TREE - Apocalypse - 06-12-2014 (06-12-2014, 10:09 AM)Psycho_Coder Wrote:Yes, i understood.(06-12-2014, 10:03 AM)Apocalypse Wrote:(06-12-2014, 09:38 AM)Psycho_Coder Wrote: NO, My answer is correct and I did follow the sequence you gave. But he final graph will be this and I am very sure.Hm, Do you know any algorithm about traversing across a complete binary tree ? (at C for example) RE: Help with an AVL TREE - Psycho_Coder - 06-12-2014 You can traverse aTree in post order or preorder or inorder. Just Google for it. RE: Help with an AVL TREE - alok9shm - 06-13-2014 @Psycho_Coder already got it correct. Anyways, I added the Balance factor in his Tree. ![]() Do it like this: The First Element in your Array is your Root node [at least for now. It will be changed later] Add 100 as the root node Now pick the next element, if this element is bigger than the root node, insert it as the Right Child, else as the Left Child, and repeat with the rest elements. In each step, write down the Balance factor beside each node. [BF = Height of the node's Left Subtree - Right Subtree] Now, if the BF for a node is +1, -1 or 0, you can proceed normally, but if the BF becomes -2 or +2, you have to rotate the tree from the current node's position [Node that has BF +2 or -2]. Rotate if clockwise if +2 and Anti-clockwise if -2. [You do the opposite if it doesn't work out] Remember to add the next element only when your AVL tree is balanced, ie. you do not have +2 or -2 BF somewhere in the tree. RE: Help with an AVL TREE - alok9shm - 06-13-2014 @Psycho_Coder already got it correct. Anyways, I added the Balance factor in his Tree. ![]() Do it like this: The First Element in your Array is your Root node [at least for now. It will be changed later] Add 100 as the root node Now pick the next element, if this element is bigger than the root node, insert it as the Right Child, else as the Left Child, and repeat with the rest elements. In each step, write down the Balance factor beside each node. [BF = Height of the node's Left Subtree - Right Subtree] Now, if the BF for a node is +1, -1 or 0, you can proceed normally, but if the BF becomes -2 or +2, you have to rotate the tree from the current node's position [Node that has BF +2 or -2]. Rotate if clockwise if +2 and Anti-clockwise if -2. [You do the opposite if it doesn't work out] Remember to add the next element only when your AVL tree is balanced, ie. you do not have +2 or -2 BF somewhere in the tree. |