Ejemplo n.º 1
0
def main():
    tree = BinarySearchTree()  # instantiating the BinarySearchTree to be used.

    for x in range(511):
        tree.add(3)

    tree.rebalance_tree()

    print(tree)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def main():
    '''main'''

    data = [21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7]
    remove_data = [21, 9, 4, 18, 15, 7]

    tree = BinarySearchTree()
    for i in range(511):
        tree.add(i)

    tree.rebalance_tree()
Ejemplo n.º 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)
Ejemplo n.º 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())
Ejemplo n.º 6
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('------')
Ejemplo n.º 7
0
def main():
    """
    Main function. Creates BST, puts in preorder form
    prints list then goes through bst and prints all data.
    """
    bst = BinarySearchTree()
    for i in range(11):
        bst.add(i)
    bst.rebalance_tree()
    preorder = bst.preorder()
    output = ''
    for node_data in preorder:
        output += str(node_data)
        output += ', '
    print(output)
    str(bst)
    removevals = [21, 9, 4, 18, 15, 7]
    for remove in removevals:
        bst.remove(remove)
    str(bst)
Ejemplo n.º 8
0
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())
Ejemplo n.º 9
0
from binarysearchtree import BinarySearchTree

bst = BinarySearchTree()
lyst = [1, 2, 3, 4, 5, 6, 7, 8]
for i in lyst:
    bst.add(i)
#print(bst)
bst.add(0)
bst.add(-6)
bst.add(-3)

bst.rebalance_tree()
print(bst)