예제 #1
0
def main():
    """Driver function for a binary search tree."""
    bst = BinarySearchTree()
    data = [21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7]
    # data = [25, 50, 57, 100, 125, 150, 200]
    for i in data:
        bst.add(i)

    print('21, 9, 4, 2, 3, 7, 14, 10, 18, 15, 26, 30, 28,')
    bst.preorder()
    print(bst)

    # data2 = [21,9,4,18,15,7]
    # for i in data2:
    #     bst.remove(i)

    # bst.preorder()
    # print(bst)

    print('inorder: ', bst.inorder())
    print('Height: ', bst.height())
    print('Length: ', bst.__len__())
    bst.remove(14)
    bst.remove(7)
    bst.remove(9)
    print(bst.rebalance_tree())
    print('Height: ', bst.height())
    # bst.display()
    print('-=-=-')
    bst._is_balanced()
    print('------')
예제 #2
0
    def test_inorder(self):
        bst = BinarySearchTree()
        seed(0)
        data = sample(range(1, 4000), k=126)
        for x in data:
            bst.add(x)

        data.sort()
        tree_data = list(bst.inorder())
        self.assertEqual(data, tree_data)
예제 #3
0
    def test_rebalance(self):
        bst = BinarySearchTree()
        seed(0)
        data = sample(range(1, 4000), k=126)
        data.sort()
        for x in data:
            bst.add(x)
        original_height = bst.height()
        self.assertEqual(data[0], bst.root.data)

        bst.rebalance_tree()
        tree_data = list(bst.inorder())
        self.assertEqual(data, tree_data)
        self.assertNotEqual(original_height, bst.height())
예제 #4
0
파일: main.py 프로젝트: ryanbalog/project6
def main():
    '''Driver for program'''
    tree = BinarySearchTree()
    for val in VALUES:
        tree.add(val)

    for item in tree.preorder():
        print(item, end=", ")
    print("\n")
    print(tree)

    for item in tree.inorder():
        print(item, end=", ")

    tree.rebalance_tree()
    print("\n")
    for item in tree.inorder():
        print(item, end=", ")

    print("\n")
    print(tree)
    x = BinarySearchTree()
    print(x.height())