예제 #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_tree_height(self):
     bst = BinarySearchTree()
     bst.add(123)
     self.assertEqual(bst.height(), 1)
     bst.add(12)
     bst.add(2)
     self.assertEqual(bst.height(), 3)
예제 #3
0
    def test_tree_height(self):
        bst = BinarySearchTree()
        self.assertEqual(bst.height(), -1)
        for i in range(511):
            bst.add(i)

        self.assertEqual(bst.height(), 510)
        bst.rebalance_tree()
        self.assertEqual(bst.height(), 8)
예제 #4
0
def main():
    """ Main driver function to test binary search. """
    test = BinarySearchTree()
    test.add(21)
    test.add(26)
    test.add(30)
    test.add(9)
    test.add(4)
    test.add(14)
    test.add(28)
    test.add(18)
    test.add(15)
    test.add(10)
    test.add(2)
    test.add(3)
    test.add(7)

    preorder = test.preorder()
    for item in preorder:
        print(str(item) + ", ", end=" ")
    print("")
    print(test)
    print("Lonsg string so I can see the test height: ", test.height())

    test.remove(21)
    test.remove(9)
    test.remove(4)
    test.remove(18)
    test.remove(15)
    test.remove(7)
    print(test)

    bst = BinarySearchTree()
    print("Produced: ", bst.height())
    print("Expects: ", -1)
    for i in range(511):
        bst.add(i)

    print("Produced: ", bst.height())
    print("Expects: ", 510)
    bst.rebalance_tree()
    print("Produced: ", bst.height())
    print("Expects: ", 8)

    newtest = BinarySearchTree()
    for i in range(7):
        newtest.add(i)
    newtest.rebalance_tree()
    print(newtest)

    lasttest = BinarySearchTree()
    lasttest.add(1)
    print("Added")
    lasttest.add(1)
예제 #5
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())
예제 #6
0
파일: main.py 프로젝트: asheux/pyalgorithms
def main():
    """
    Main function
    """
    node = BinarySearchTree()
    arr = [21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7]
    for _, value in enumerate(arr):
        node.add(value)

    print(node.preorder(node.root))
    print(node.height())
예제 #7
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())