def test_bst_size(self):
     tree = BSTree()
     self.assertEqual(tree.size, 0)
     tree.insert(100)
     self.assertEqual(tree.size, 1)
     tree.insert(200)
     tree.insert(50)
     self.assertEqual(tree.size, 3)
     tree.remove(100)
     self.assertEqual(tree.size, 2)
 def test_inserts_return_boolean_result(self):
     tree = BSTree()
     self.assertTrue(tree.insert(100))
     self.assertFalse(tree.insert(100))
 def test_does_not_insert_repeated_values(self):
     tree = BSTree()
     tree.insert(100)
     tree.insert(100)
     self.assertEqual(tree.size, 1)
Beispiel #4
0
This should be accomplished using a Breadth First Search algorithm.
"""

from queue import Queue
from trees.binary_search_tree import BSTree


def print_list(l):
    print(" ".join(map(str, l)))


if __name__ == "__main__":
    values = [10, 5, 15, 1, 7, 12, 20, -1, 11, 300, -5, -10]
    tree = BSTree()
    for v in values:
        tree.insert(v)

    root_node = tree._root
    q = Queue()

    q.put((root_node, 0))
    printing_row = 0
    row_values = []

    while not q.empty():
        current_node, current_level = q.get()

        # If where are one level deeper, print row an clean `row_values`.
        if current_level != printing_row:
            printing_row += 1
            print_list(row_values)