Exemplo n.º 1
0
 def delete_and_splay(self, key):
     """ After regular BST delete, the former node's parent is hoisted to
     the root.
     """
     removed = BST.delete(self, key)
     self.splay(removed[PARENT])
     return removed
Exemplo n.º 2
0
 def delete_and_splay(self, key):
     """ After regular BST delete, the former node's parent is hoisted to
     the root.
     """
     removed = BST.delete(self, key)
     self.splay(removed[PARENT])
     return removed
Exemplo n.º 3
0
    def delete_and_reballance(self, key):
        # Node is a recently removed leaf.
        node = BST.delete(self, key)

        if node == None:
            return None

        ancestor = node
        while ancestor != None:
            ancestor = ancestor[PARENT]

            # Ignore ballanced ancestors.
            if -2 < self.get_ballance_factor(ancestor) < 2:
                continue

            if self.depth(ancestor[LEFT]) > self.depth(ancestor[RIGHT]):
                FIRST = LEFT
            else:
                FIRST = RIGHT

            if self.depth(ancestor[FIRST][LEFT]) > self.depth(ancestor[FIRST][RIGHT]):
                SECOND = LEFT
            else:
                SECOND = RIGHT

            #import pdb; pdb.set_trace()
            self.reballance(ancestor[FIRST][SECOND])

        return node