Beispiel #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('------')
 def test_recursive_preorder(self):
     bst = BinarySearchTree()
     seed(0)
     data = sample(range(1, 400), k=20)
     rc.recursion_count = 0
     for datum in data:
         bst.add(datum)
     rc.recursion_count = 0
     bst.preorder(bst.root)
     self.assertGreater(rc.recursion_count, 15)
Beispiel #3
0
def main():
    """Main function"""
    tree = BinarySearchTree()
    tree.add(21)
    tree.add(26)
    tree.add(30)
    tree.add(9)
    tree.add(4)
    tree.add(14)
    tree.add(28)
    tree.add(18)
    tree.add(15)
    tree.add(10)
    tree.add(2)
    tree.add(3)
    tree.add(7)

    pre = tree.preorder()

    for item in pre:
        print(item, end=', ')

    print('')
    print(tree)

    tree.remove(21)
    tree.remove(9)
    tree.remove(4)
    tree.remove(18)
    tree.remove(15)
    tree.remove(7)

    print(tree)
Beispiel #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)

    test.remove(21)
    test.remove(9)
    test.remove(4)
    test.remove(18)
    test.remove(15)
    test.remove(7)
    print(test)
Beispiel #5
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]
    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)
Beispiel #6
0
 def test_preorder_output(self):
     bst = BinarySearchTree()
     seed(0)
     data = sample(range(1, 400), k=123)
     for datum in data:
         bst.add(datum)
     output = bst.preorder()
     self.assertEqual(output[0], data[0])
Beispiel #7
0
 def test_preorder_type(self):
     bst = BinarySearchTree()
     seed(0)
     data = sample(range(1, 400), k=123)
     for datum in data:
         bst.add(datum)
     output = bst.preorder()
     self.assertTrue(isinstance(output, list))
 def test_preorder(self):
     bst = BinarySearchTree()
     seed(0)
     data = sample(range(1, 400), k=123)
     for datum in data:
         bst.add(datum)
     output = bst.preorder(bst.root)
     self.assertTrue(isinstance(output, list))
     self.assertEqual(len(output), 123)
     self.assertEqual(output[0], data[0])
Beispiel #9
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)
Beispiel #10
0
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())
Beispiel #11
0
def main():
    """Function to run the binary search tree"""
    bst = BinarySearchTree()
    lyst = [21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7]
    for i in lyst:
        bst.add(i)
    print(bst.preorder())
    print(bst)
    rem_lyst = [21, 9, 4, 18, 15, 7]
    for i in rem_lyst:
        bst.remove(i)
    print(bst)
Beispiel #12
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()
    tree.add(data)

    print(str(tree.preorder())[1:-1])
    print(tree)

    tree.remove(remove_data)

    print(tree)
Beispiel #13
0
def main():
    """
    Main function. Creates BST, puts in preorder form
    prints list then goes through bst and prints all data.
    """
    bst = BinarySearchTree()
    addvals = [21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7]
    for add in addvals:
        bst.add(add)
    preordered_bst = bst.preorder()
    output = ''
    for node_data in preordered_bst:
        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)
Beispiel #14
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)
Beispiel #15
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())