Example #1
0
    def insert(self, value):
        '''Insert a new BTNode with value value into this BST. Do not insert
        duplicates.

        '''
        if self._root is None:
            self._root = BTNode(value)
            return self
        return _insert(self._root, BTNode(value))
Example #2
0
    def __init__(self, value, colour=BLACK, left=None, right=None,
                 parent=None):
        '''Init a RBTNode with value value, colour colour, parent parent, left
        child left, and right child right.

        '''

        BTNode.__init__(self, value, left, right)
        self.colour = colour
        self.parent = parent
Example #3
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)
Example #4
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)
Example #5
0
def random_insert(node, value):
    '''Return node with value insterted randomly into a subtree rooted at node;
    insertion at leaf level;
    at each level, go right with prob 50% and left with prob 50%.'''

    # use random.random() to decide left or right

    if node is None:
        return BTNode(value)
    # Insert to the left is > 0.5
    if random.random() > 0.5:
        if node.left is None:
            node.left = BTNode(value)
        else:
            random_insert(node.left, value)
    else:
        if node.right is None:
            node.right = BTNode(value)
        else:
            random_insert(node.right, value)
    return node
Example #6
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
        """
Example #7
0
        '''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 :)
        pass


class NoSuchValueException(Exception):
    pass


if __name__ == '__main__':
    BT = BST(
        BTNode(5, BTNode(3, BTNode(2, BTNode(1), None), BTNode(4)),
               BTNode(6, None, BTNode(7))))
    # the string shuld be '\n\t\t7\n\n\t6\n\n5\n\n\t\t4\n\n\t3\n\n\t\t2\n\n\t\t\t1\n'
    print(BT)
    print(BT.preorder())
    print(BT.inorder())
    print(BT.postorder())
    print(BT.is_bst())
    print(BT.size())
    print(BT.fringe())
    print(BT.height())

    print(20 * '=')

    for x in (0, 4.5, 10):
        BT.insert(x)
        print(BT)
Example #8
0
    def __str__(self):
        '''Return a str representation of this RedBlackTree.'''

        return '{}: {}'.format(BTNode.__str__(self), 'Red' if self.colour == RED else 'Black')