def testNotFoundSearch(self):
     node_5 = Node(5, None, None)
     bst = BST(node_5)
     bst.insert(node_5, 4)
     bst.insert(node_5, 2)
     bst.insert(node_5, 1)
     bst.insert(node_5, 3)
     bst.insert(node_5, 6)
     bst.insert(node_5, 7)
     bst.insert(node_5, 8)
     bst.insert(node_5, 0)
     bst = BST(node_5)
     self.assertFalse(bst.search(node_5, 9))
Example #2
0
def main():
    tree = BST([16, 9, 18, 3, 14, 19, 1, 5])
    assert (find_ancestors(tree, 5)) == [3, 9, 16]
    assert (find_ancestors(tree, 14)) == [9, 16]
    assert (find_ancestors(tree, 16)) == []
    assert (find_ancestors(tree, 18)) == [16]
    assert (find_ancestors(tree, 19)) == [18, 16]
    assert (find_ancestors(tree, 20)) == []
    assert (find_ancestors(tree, '.')) == []

    tree2 = BST(['F', 'D', 'I', 'B', 'E', 'L', 'A'])
    assert (find_ancestors(tree2, 'B')) == ['D', 'F']
    assert (find_ancestors(tree2, 3)) == []
Example #3
0
def test_it():
    tree = BST()
    random.seed(12345)
    number_of_nodes = 10
    tree.random(number_of_nodes)
    assert tree.min() == 0
    assert tree.max() == 9
Example #4
0
def main(argv):
    input_file = argv[1]

    # Instance of the binary search tree
    tree = BST()

    with open(input_file, 'r') as file_ob:
        for line in file_ob:
            line = line.split()

            if line[0] == 'insert':
                tree.insert(int(line[1]))

            if line[0] == 'delete':
                tree.delete(int(line[1]))

            if line[0] == 'preorder':
                tree.traverse('preorder', tree.getRoot())
                print("")

            if line[0] == 'inorder':
                tree.traverse('inorder', tree.getRoot())
                print("")

            if line[0] == 'postorder':
                tree.traverse('postorder', tree.getRoot())
                print("")

        # Close the file
        file_ob.close()
 def testInsertThreeNodes(self):
     node_1 = Node(5, None, None)
     bst = BST(node_1)
     bst.insert(node_1, 4)
     bst.insert(node_1, 2)
     bst.insert(node_1, 1)
     self.assertEquals(node_1.left.left.left.data, 1)
Example #6
0
def main(argv):
    myBST = BST()

    # Loop over input file (filename passed via argv).
    input_file = argv[1]
    with open(input_file, 'r') as file_ob:
        # Split each line into a task and number (if one exists)
        #hint: use the split method for strings
        # Perform the function corresponding to the specified task
        # i.e., insert, delete, inorder, preorder, postorder
        # Close the file when you're done.
        for line in file_ob:
            if ' ' in line:
                command, num = line.split()
                if command == "insert":
                    myBST.insert(num)
                if command == "delete":
                    myBST.deleteBook(num)
                #print command
                #print num
            else:
                line = line.split()
                myBST.traverse(line[0], myBST.getRoot())
                print ''
            if 'str' in line:
                break
    pass
Example #7
0
 def testFindMinNode(self):
     #check that min node of empty tree is None
     tree = BST()
     self.assertEqual(tree.findMinNode(),None)
     #check min of small balanced BST
     keys = [10,5,15,2,7,12,20]
     vals = ["a","b","c","d","e","f","g","h","i"]
     tree.makeBST(keys,vals)
     self.assertEqual(tree.findMinNode().getKey(),2)
     #check min node of non-balanced, large BST
     tree = BST()
     keys = [100,50,200,25,75,150,250,10,36,63,8,125,175,225,290,20,27,47,55,70,77,90,110,137,163,190,213,230,270,300]
     values = ["Newry","Azerbijan","Navan","Napoli","Japan","Ashbourne","Cyprus","New York City Baby","L.A.P.D","Bermuda","Bali","Atlantis","Paris","Memphis","Florida","Summerhill","Castlebar",
     "London","Kent","Gloucester","Verona","Raheny","Vienna","Washington","Zanzibar","Crete","Panama","Miltown","Leopardstown","Cape town","Alkatraz","Diagon-alley","Jellystone Park","Rathoath"]
     tree.makeBST(keys,values)
     self.assertEqual(tree.findMinNode().getKey(),8)
Example #8
0
def test_preorder():
    for i in range(20):
        bst = BST()
        randomlist = random.sample(range(1, 100), 35)
        for x in randomlist:
            bst.insert(x)
        assert bst.preorder(bst.root) == bst.preorder_iterative()
Example #9
0
def main():
    #create BST:
    print('create BST with elements: 10, 5, 2, 6, 1, 4, 20, 15, 25')
    bst = BST(10)
    insert_in_BST = [5, 2, 6, 1, 4, 20, 15, 25]
    for item in insert_in_BST:
        bst.insert(item)

    # test find() -> find root with key = 2 and print key:
    print('find root with key=2 and print key:')
    print(bst.find(2).key)

    # find min/max:
    print('find node with min key and print key (should be 1):')
    print(bst.findMin().key)
    print('find node with max key and print key (should be 25):')
    print(bst.findMax().key)

    # traverse tree / delete nodes:
    print('traverse tree in order and print all keys:')
    bst.traverseInOrder()
    print('delete node with key=5 and traverse tree again:')
    bst.delete(5)
    bst.traverseInOrder()

    # get root and print it's key:
    print('get root key (should be 10)')
    print(bst.getRoot().key)
Example #10
0
def runBST(arr):
    start = millis()
    bst = BST()
    bst.insertList(arr)
    print("BST to list: \n" + str(bst.toList()))
    end = millis()
    print("time spent: " + str(round(end - start, 4)) + " ms\n")
Example #11
0
    def test_basic(self):
        bin = BST()
        bin.insert(5)
        self.assertEqual(bin.root.val, 5)

        bin.insert(6)
        self.assertEqual(bin.root.right.val, 6)
Example #12
0
def testDelete():
    t = BST()
    t.put(2)
    t.put(1)
    t.put(3)
    t.delete(1)
    assert not t.contains(1)
Example #13
0
def main(argv):
    #create a new tree
    tree = BST()



    #open file argument
    file = open(argv[1], "r")
    # goes for amount of of lines in f
    for line in file:
        text = line.split()
        #test cases for what the first element in the list is
        if text[0] == "insert":
            #saves the second element, since it exists
            data = text[1]
            tree.insert(data)
        elif text[0] == "delete":
            #saves element, then calls delete
            data = text[1]
            tree.delete(data)
        elif text[0] == "preorder" or text[0] == "postorder" or text[0] == "inorder":
            #tests for if a traverse then calls the function
            tree.traverse(text[0], tree.getRoot())
        else:
            print("incorrect format")

    return 0
Example #14
0
    def test_next(self):
        bin = BST()
        bin.insert(7)
        bin.insert(3)
        bin.insert(8)
        bin.insert(1)
        bin.insert(5)
        bin.insert(2)
        bin.insert(4)
        bin.insert(6)
        bin.insert(10)
        bin.insert(9)
        bin.insert(20)
        bin.insert(11)
        bin.insert(12)

        node1 = bin.next(bin.root)
        node2 = bin.next(node1)
        node3 = bin.next(node2)
        node4 = bin.next(node3)
        node5 = bin.next(node4)
        node6 = bin.next(node5)

        self.assertEqual(node1.val, 8)
        self.assertEqual(node2.val, 9)
        self.assertEqual(node3.val, 10)
        self.assertEqual(node4.val, 11)
        self.assertEqual(node5.val, 12)
        self.assertEqual(node6.val, 20)
        self.assertEqual(bin.next(node6), None)
Example #15
0
def full_tree():
    test_tree = BST()
    test_tree.insert(5)
    test_tree.insert(8)
    test_tree.insert(3)
    test_tree.insert(4)
    return test_tree
Example #16
0
File: BST.py Project: Nereya/ca117
def main():
    # Read each test case

    bst = BST()
    for ele in [2, 7, 4, 8, 5]:
        bst.add(ele)
    print(bst.count_in_range(3, 5))
 def testHeightV2(self):
     node_2 = Node(2, None, None)
     node_7 = Node(7, None, None)
     node_4 = Node(4, node_2, node_7)
     node_6 = Node(6, None, None)
     node_5 = Node(5, node_4, node_6)
     bst = BST(node_5)
     self.assertEquals(2, bst.get_height(node_5))
Example #18
0
    def test_search(self):
        bin = BST()
        for i in range(50):
            bin.insert(i)

        self.assertEqual(bin.find(8).val, 8)
        self.assertEqual(bin.find(49).val, 49)
        self.assertEqual(bin.find(60), None)
 def testInsertFourNodes(self):
     node_1 = Node(5, None, None)
     bst = BST(node_1)
     bst.insert(node_1, 4)
     bst.insert(node_1, 2)
     bst.insert(node_1, 1)
     bst.insert(node_1, 6)
     self.assertEquals(node_1.right.data, 6)
Example #20
0
def test_range_count():
    bst = BST()
    bst.put('a', 1)
    bst.put('b', 1)
    bst.put('c', 1)
    bst.put('d', 1)
    bst.put('e', 1)
    assert bst.range_count('b', 'e') == 4
Example #21
0
def main():
    tree = BST([16, 9, 18, 3, 14, 19, 1, 5])
    assert find_common_ancestor(tree, 3, 14) == 9
    assert find_common_ancestor(tree, 3, 0) == []
    assert find_common_ancestor(tree, 9, 19) == 16
    assert find_common_ancestor(tree, 10, 30) == []
    assert find_common_ancestor(tree, 16, 1) == 16
    assert find_common_ancestor(tree, 3, 1) == 3
Example #22
0
File: L8.py Project: lili718/Labs-
def populate(length):
    out = []
    bst = BST()
    for i in range(0, length):
        n = random.randint(0, length)
        out.append(n)
        bst.append(n)
    return (out, bst)
    def setUp(self):
        self.list1 = [3, 2, 1, 5, 6, 7]

        # Creating the BST1
        self.BST1 = BST(dtype=int, key=4)

        for val in self.list1:
            self.BST1 = self.BST1.insert(val)
Example #24
0
def test_range_search():
    bst = BST()
    bst.put('a', 1)
    bst.put('b', 1)
    bst.put('c', 1)
    bst.put('d', 1)
    bst.put('e', 1)

    assert bst.range_search('b', 'd') == ['b', 'c', 'd']
Example #25
0
    def test_inorder_traversal(self):
        bst = BST()
        listi = [x for x in range(50)]
        random.shuffle(listi)
        for j in listi:
            bst.insert(j)

        inorderTreeWalk(bst.root)
        self.assertEqual(len(listi), bst.size)
Example #26
0
 def test_Insert_Node(self):
     bst = BST()
     tNode1 = Node(21)
     tNode2 = Node(20)
     treeInsert(bst, tNode1)
     treeInsert(bst, tNode2)
     self.assertEqual(2, len(bst))
     searched = treeSearch(bst.root, 20)
     self.assertIsNotNone(searched)
Example #27
0
def test_get():
    root = Node('b', 5)
    root.left = Node('a', 3)
    root.right = Node('c', 2)
    bst = BST(root)

    assert bst.get('a') == 3
    assert bst.get('c') == 2
    assert bst.get('d') is None
 def testRemoveNodeHasOneChildren(self):
     node_1 = Node(1, None, None)
     node_2 = Node(2, node_1, None)
     node_3 = Node(4, node_2, None)
     node_4 = Node(6, None, None)
     node_5 = Node(5, node_3, node_4)
     bst = BST(node_5)
     bst.removeNode(None, node_5, 2)
     self.assertEquals(node_3.left.data, 1)
 def testRemoveLeafNode2(self):
     node_1 = Node(1, None, None)
     node_2 = Node(2, node_1, None)
     node_3 = Node(4, node_2, None)
     node_4 = Node(6, None, None)
     node_5 = Node(5, node_3, node_4)
     bst = BST(node_5)
     bst.removeNode(None, node_5, 1)
     self.assertEquals(node_2.left, None)
Example #30
0
    def test_Search_Iter(self):
        bst = BST()
        listi = [x for x in range(50)]
        random.shuffle(listi)
        for j in listi:
            bst.insert(j)

        node = treeSearch(bst.root, 32)
        self.assertIsNotNone(node)
        self.assertEqual(32, node.value)