コード例 #1
0
ファイル: AVLTree.py プロジェクト: somu-a/CS46Container
    def insert(self, value):
        '''
        FIXME:
        Implement this function.

        The lecture videos provide a high-level
        overview of how to insert into an AVL tree,
        and the textbook provides full python code.
        The textbook's class hierarchy for their AVL tree
        code is fairly different from our class hierarchy,
        however, so you will have to adapt their code.

        HINT:
        It is okay to add @staticmethod helper functions for this code.
        The code should look very similar to the code for your insert
        function for the BST,
        but it will also call the left and right rebalancing functions.
        '''
        if self.root is None:
            self.root = Node(value)
            return
        elif value == self.root.value:
            return
        else:
            BST.insert(self, value)
            while not self.is_avl_satisfied():
                self.root = self.rebalance(self.root)
コード例 #2
0
def test__BST_insert(xs):
    xs = list(set(xs))
    bst = BST()
    for x in xs:
        bst.insert(x)
        assert x in bst.to_list('inorder')
        assert bst.is_bst_satisfied()
コード例 #3
0
def test__BST_remove_and_insert3(xs,ys):
    '''
    This test performs a mixture of both insertions and removals.
    This ensures that there are no weird interactions between inserting and removing.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    for y in ys:
        bst.insert(y)
        x = bst.find_smallest()
        bst.remove(x)
        assert bst.is_bst_satisfied()
コード例 #4
0
def test__BST_remove_and_insert1(xs,ys):
    '''
    This test performs a mixture of both insertions and removals.
    This ensures that there are no weird interactions between inserting and removing.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    for y in ys:
        bst.insert(y)
        x = random.choice(bst.to_list('inorder'))
        bst.remove(x)
        assert bst.is_bst_satisfied()