Exemple #1
0
    def delete(self, value):
        '''Delete a BTNode that contains value value from this BST. If there
        is no such BTNode, raise a NoSuchValueException.

        '''

        # special case: empty tree
        if self._root is None:
            raise NoSuchValueException
        # special case: deleting a root
        if self._root.value == value:
            # create a fake root (use min - 1 as value), then remove root
            fake_root = BTNode(_get_min(self._root) - 1)
            fake_root.right = self._root
            _delete(fake_root, self._root.value, None)
            self._root = fake_root.right
        else:
            _delete(self._root, value, None)
Exemple #2
0
def _delete_node(node, parent):
    '''Remove the BTNode node from the tree rooted at BTNode.
    Pre: parent is the BTNode parent of node
         node is not None
    Hint: this is where you use get_min
    '''

    # leaf is included in this case; where node.left == None
    if node.right is None:
        _update_parent(node, parent, node.left)
    # Need to delete the min node and update parent
    else:
        min_value = _get_min(node.right)
        substitute = BTNode(min_value)
        _delete(node, min_value, parent)
        substitute.left = node.left
        substitute.right = node.right
        _update_parent(node, parent, substitute)
Exemple #3
0
    def delete(self, value):
        '''Delete a BTNode that contains value value from this BST. If there
        is no such BTNode, raise a NoSuchValueException.

        '''

        # this one will take a bit longer :)
        # special case: empty tree
        if self._root is None:
            raise NoSuchValueException
        # special case: deleting a root
        if self._root.value == value:
            # create a fake root (use min - 1 as value), then remove root
            fake_root = BTNode(_get_min(self._root) - 1)
            fake_root.right = self._root
            _delete(fake_root, self._root.value, None)
            self._root = fake_root.right
        else:
            _delete(self._root, value, None)

        # Still don't understand why case 1 doesn't work
        """