C++ tree AVL balance -


i've run in issue balance part of tree. have checkbal called after recursive insertion. if try add 5, 2 , 4 checks balance of 2 , continues 5 goes rotateleft part of right rotate correct. rotateleft function errors on second line.

what wrong implementation? i've searched on , compared did way people talk how it's done. got working. forgetting set n k @ end.

//============================================================================== //===== set balance ============================================================ snode<t>* checkbal(snode<t> *locroot) {     // make sure check children balanced well.     if (locroot->left != null)         locroot->left = checkbal(locroot->left);     if (locroot->right != null)         locroot->right = checkbal(locroot->right);      if(getheight(locroot->left) - getheight(locroot->right) > 1)     {         if(getheight(locroot->left->right) > getheight(locroot->left->left))             locroot->left = rotateright(locroot->left);         locroot = rotateleft(locroot);     }     else if(getheight(locroot->right) - getheight(locroot->left) > 1)     {         if(getheight(locroot->right->left) > getheight(locroot->right->right))             locroot->right = rotateleft(locroot->right);         locroot = rotateright(locroot);     }     updateheights(locroot);     return locroot; }     /*         extream cases of balancing tree requires double rotation                          \               d              /             b          'a' current root         if right->left (grandchild) larger right->right (grandchild)         first right rotate child left rotate parent           left > right 2 or more             left.left < left.right  (double right rotation)             left.left >= left.right (single right rotation)         right > left 2 or more             right.right < right.left (double left rotation)             right.right >= right.left (single left rotation)     */  snode<t>* rotateright(snode<t> *n) const { /*       n           k      / \         / \    (a)  k  =>   n  (c)        / \     / \      (b) (c) (a) (b) */     // k going our new parent     // move (c) k->right n->left     // set k->right n     // return new parent node update 1 above.     snode<t> *k = n->right;     n->right = k->left;             k->left = n;     return n = k; } 

rotateright(locroot->left); 

should be,

rotateright(locroot->right); 

but it's still wrong implementation. =p

should have different implementation left side of root , right side.
try wikipedia animation.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -