Exemplo n.º 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)
Exemplo n.º 2
0
 def remove(self, node: Node, value):
     if  node and value < node.val:
         return self.remove(node.left, value)
     elif node and value > node.val:
         return self.remove(node. right, value)
     elif node and value == node.val:
         nodeToRemove = node
         if node.right and node.left:
             suc = self._getInorderSuccessor(node)
             node.val = suc.val
             nodeToRemove = suc
            # print(nodeToRemove)
             #print(self.root)
         self._remove(nodeToRemove)
         self.count -= 1
     else:
         return