Beispiel #1
0
    def remove(self, item):
        """Removes and returns item from the tree."""

        # Helper function to adjust placement of an item
        def liftMaxInLeftSubtreeToTop(top):
            # Replace top's datum with the maximum datum in the left subtree
            # Pre:  top has a left child
            # Post: the maximum node in top's left subtree
            #       has been removed
            # Post: top.getRoot() = maximum value in top's left subtree
            parent = top
            currentNode = top.getLeft()
            while not currentNode.getRight().isEmpty():
                parent = currentNode
                currentNode = currentNode.getRight()
            top.setRoot(currentNode.getRoot())
            if parent == top:
                top.setLeft(currentNode.getLeft())
            else:
                parent.setRight(currentNode.getLeft())

        # Begin main part of the method
        if self.isEmpty(): return None

        # Attempt to locate the node containing the item
        itemRemoved = None
        preRoot = BinaryTree(None)
        preRoot.setLeft(self._tree)
        parent = preRoot
        direction = 'L'
        currentNode = self._tree
        while not currentNode.isEmpty():
            if currentNode.getRoot() == item:
                itemRemoved = currentNode.getRoot()
                break
            parent = currentNode
            if currentNode.getRoot() > item:
                direction = 'L'
                currentNode = currentNode.getLeft()
            else:
                direction = 'R'
                currentNode = currentNode.getRight()

        # Return None if the item is absent
        if itemRemoved == None: return None

        # The item is present, so remove its node

        # Case 1: The node has a left and a right child
        #         Replace the node's value with the maximum value in the
        #         left subtree
        #         Delete the maximium node in the left subtree
        if not currentNode.getLeft().isEmpty() \
           and not currentNode.getRight().isEmpty():
            liftMaxInLeftSubtreeToTop(currentNode)
        else:

            # Case 2: The node has no left child
            if currentNode.getLeft().isEmpty():
                newChild = currentNode.getRight()

        # Case 3: The node has no right child
            else:
                newChild = currentNode.getLeft()

        # Case 2 & 3: Tie the parent to the new child
            if direction == 'L':
                parent.setLeft(newChild)
            else:
                parent.setRight(newChild)

        # All cases: Reset the root (if it hasn't changed no harm done)
        #            Decrement the collection's size counter
        #            Return the item
        self._tree = preRoot.getLeft()
        self._size -= 1
        return itemRemoved
Beispiel #2
0
print "Preorder:"
lyst = []
d.preorder(lyst)
print lyst

print "Inorder:"
lyst = []
d.inorder(lyst)
print lyst

print "Postorder:"
lyst = []
d.postorder(lyst)
print lyst

print "Levelorder:"
lyst = []
d.levelorder(lyst)
print lyst

print "Iterator:"
for item in d: print item,

print "Testing an exception:"
t = a.getLeft()
print "True:", t.isEmpty()
t.setRoot("An item")


    
    def remove(self, item):
        """Removes and returns item from the tree."""

        # Helper function to adjust placement of an item
        def liftMaxInLeftSubtreeToTop(top):
            # Replace top's datum with the maximum datum in the left subtree
            # Pre:  top has a left child
            # Post: the maximum node in top's left subtree
            #       has been removed
            # Post: top.getRoot() = maximum value in top's left subtree
            parent = top
            currentNode = top.getLeft()
            while not currentNode.getRight().isEmpty():
                parent = currentNode
                currentNode = currentNode.getRight()
            top.setRoot(currentNode.getRoot())
            if parent == top:
                top.setLeft(currentNode.getLeft())
            else:
                parent.setRight(currentNode.getLeft())

        # Begin main part of the method
        if self.isEmpty(): return None
        
        # Attempt to locate the node containing the item
        itemRemoved = None
        preRoot = BinaryTree(None)
        preRoot.setLeft(self._tree)
        parent = preRoot
        direction = 'L'
        currentNode = self._tree
        while not currentNode.isEmpty():
            if currentNode.getRoot() == item:
                itemRemoved = currentNode.getRoot()
                break
            parent = currentNode
            if currentNode.getRoot() > item:
                direction = 'L'
                currentNode = currentNode.getLeft()
            else:
                direction = 'R'
                currentNode = currentNode.getRight()
                
        # Return None if the item is absent
        if itemRemoved == None: return None
        
        # The item is present, so remove its node

        # Case 1: The node has a left and a right child
        #         Replace the node's value with the maximum value in the
        #         left subtree
        #         Delete the maximium node in the left subtree
        if not currentNode.getLeft().isEmpty() \
           and not currentNode.getRight().isEmpty():
            liftMaxInLeftSubtreeToTop(currentNode)
        else:
            
        # Case 2: The node has no left child
            if currentNode.getLeft().isEmpty():
                newChild = currentNode.getRight()
                
        # Case 3: The node has no right child
            else:
                newChild = currentNode.getLeft()
                
        # Case 2 & 3: Tie the parent to the new child
            if direction == 'L':
                parent.setLeft(newChild)
            else:
                parent.setRight(newChild)
            
        # All cases: Reset the root (if it hasn't changed no harm done)
        #            Decrement the collection's size counter
        #            Return the item
        self._tree = preRoot.getLeft()
        self._size -= 1
        return itemRemoved
Beispiel #4
0
print "Frontier:", frontier(d)

print "Preorder:"
lyst = []
d.preorder(lyst)
print lyst

print "Inorder:"
lyst = []
d.inorder(lyst)
print lyst

print "Postorder:"
lyst = []
d.postorder(lyst)
print lyst

print "Levelorder:"
lyst = []
d.levelorder(lyst)
print lyst

print "Iterator:"
for item in d:
    print item,

print "Testing an exception:"
t = a.getLeft()
print "True:", t.isEmpty()
t.setRoot("An item")