示例#1
0
 def _remove(self, node: Node):
     leftChild = node.left
    # print(node)
     rightChild = node.right
     child = leftChild if node.left else rightChild
     if node is self.root:
         print("root")
         if child: #removing root while it has a single child
             self.root = child
             self.root.parent = None
             self.root.color = True
         else:
             self.root = None
     elif not node.color:
         if not (leftChild or rightChild):
             self._removeLeaf(node)
         else:
             raise Exception("BlackHeightError")
     else:#Node is black
         if leftChild:
             if leftChild.left or leftChild.right:
                 raise Exception("BlackNode red child problem1")
         if rightChild:
             if rightChild.right or rightChild.left:
                 raise Exception("BlackNode red child problem2")
         if child and (not child.color):#red child
             node.val = child.val
             node.left = child.left
             node.right = child.right
         else:#black child
             self._removeBlackNode(node)
示例#2
0
 def _fixRotation(self, parent :Node, grandParent : Node, greatGrandParent : Node):
     parent.parent = greatGrandParent
     if greatGrandParent:
         if greatGrandParent > grandParent:
             greatGrandParent.left = parent
         else:
             greatGrandParent.right = parent
     else:
         self.root = parent
示例#3
0
 def _rotateRight(self, node : Node, parent : Node, grandParent: Node):
     greatGrandParent = grandParent.parent
     self._fixRotation(parent, grandParent, greatGrandParent)
     oldRight = parent.right
     parent.right = grandParent
     grandParent.parent = parent
     grandParent.left = oldRight
     if oldRight:
         oldRight.parent = grandParent