@@ -566,6 +566,9 @@ def _right_rotate(self, j, k):
566566 self .tree [self .tree [k ].parent ].left = k
567567 self .tree [j ].parent = k
568568 self .tree [k ].right = j
569+ kp = self .tree [k ].parent
570+ if kp is None :
571+ self .root_idx = k
569572
570573 def _left_right_rotate (self , j , k ):
571574 i = self .tree [k ].right
@@ -617,6 +620,9 @@ def _left_rotate(self, j, k):
617620 self .tree [self .tree [k ].parent ].right = k
618621 self .tree [j ].parent = k
619622 self .tree [k ].left = j
623+ kp = self .tree [k ].parent
624+ if kp is None :
625+ self .root_idx = k
620626
621627class AVLTree (SelfBalancingBinaryTree ):
622628 """
@@ -645,9 +651,6 @@ def _right_rotate(self, j, k):
645651 super (AVLTree , self )._right_rotate (j , k )
646652 self .tree [j ].height = max (self .left_height (self .tree [j ]),
647653 self .right_height (self .tree [j ])) + 1
648- kp = self .tree [k ].parent
649- if kp is None :
650- self .root_idx = k
651654 if self .is_order_statistic :
652655 self .tree [j ].size = (self .left_size (self .tree [j ]) +
653656 self .right_size (self .tree [j ]) + 1 )
@@ -682,9 +685,6 @@ def _left_rotate(self, j, k):
682685 self .right_height (self .tree [j ])) + 1
683686 self .tree [k ].height = max (self .left_height (self .tree [k ]),
684687 self .right_height (self .tree [k ])) + 1
685- kp = self .tree [k ].parent
686- if kp is None :
687- self .root_idx = k
688688 if self .is_order_statistic :
689689 self .tree [j ].size = (self .left_size (self .tree [j ]) +
690690 self .right_size (self .tree [j ]) + 1 )
0 commit comments