def test_del_root_in_single_node_tree(): """Test deletion of root in tree of one node.""" from bst import Tree new_bst = Tree() new_bst.insert(10) new_bst.delete(10) assert new_bst.root is None
def upside_down(tree): d = {} traverse_left(tree.root, None, d) inverted = Tree() inverted.root = d['root'] return inverted
def test_find(self): tree = Tree(lt) for (k, v) in [(0, "0"), (2, "2"), (2, "3"), (1, "1")]: tree.insert(k, v) assert tree.find(0) == (0, "0") assert tree.find(1) == (1, "1") assert tree.find(2) == (2, "3")
def main(): one = Node(1, "") two = Node(2, "") three = Node(3, "") four = Node(4, "") five = Node(5, "") six = Node(6, "") seven = Node(7, "") eight = Node(8, "") nine = Node(9, "") one.left = two one.right = three two.left = four two.right = five four.left = six four.right = seven tree = Tree() tree.root = one tree.levelorder() inverted = upside_down(tree) inverted.levelorder()
def brute_sweep_intersections(segments): start = time() counter = 0 status = { "upper": 0, "lower": 1, } points = [] event_queue = Tree(event_lt) status_queue = {} for segment in segments: event_queue.insert(upper_end(*segment), (status["upper"], segment)) event_queue.insert(lower_end(*segment), (status["lower"], segment)) while not event_queue.empty(): (_, (label, segment)) = event_queue.pop() if label == status["upper"]: for other in list(status_queue.keys()): counter += 1 point = point_of_intersection(segment, other) if point is not None: points.append(point) status_queue[segment] = None else: del status_queue[segment] print(results(True, counter, not len(points) == len(set(points)), start)) return (segments, points)
def balanced_3_nodes(): """Create balanced tree with 3 nodes.""" from bst import Tree t = Tree() t.insert(10) t.insert(5) t.insert(15) return t
def test_if_iterable_is_inserted(bst): """Test for iterables.""" from bst import Tree test_iter = [5, 3, 7] test_node = Tree(test_iter) assert test_node.root.data == test_iter[0] assert test_node.root.left.data == test_iter[1] assert test_node.root.right.data == test_iter[2]
def test_insert_mulitple_nodes_return_correct_size(): """Test for multiple node insertion.""" from bst import Tree test_tree = Tree() test_values = random.sample(range(100), 10) for i in test_values: test_tree.insert(i) assert test_tree.size() == len(test_values)
def test_breadth_first_traversal(): """Test breadth_first traversal.""" from bst import Tree test_list = [10, 7, 5, 9, 12, 11, 13] test_tree = Tree(test_list) x = test_tree.breadth_first() for i in x: assert i == [10, 7, 12, 5, 9, 11, 13]
def test_search_mulitple_nodes(): """Test multiple node searches.""" from bst import Tree test_tree = Tree() test_values = random.sample(range(100), 10) for i in test_values: test_tree.insert(i) for j in test_values: assert test_tree.search(j).data == j
def test_search_one_node(): """Test search for one node from inserted list of nodes.""" from bst import Tree test_tree = Tree() test_node_list = [1, 2, 3, 4, 5] for i in test_node_list: test_tree.insert(i) assert test_tree.search(test_node_list[2]).data == 3
def insert_balanced(num): num_of_runs = num from bst import Tree tree = Tree(50) while num_of_runs > 0: left_num = LESSER[num_of_runs] right_num = GREATER[num_of_runs] tree.insert(left_num) tree.insert(right_num) num_of_runs -= 1 return tree
def test_get_depth_small_tree(): """Test multiple nodes has depth of 3.""" from bst import Tree test_tree = Tree() test_tree.insert(5) test_tree.insert(2) test_tree.insert(3) test_tree.insert(1) test_tree.insert(7) assert test_tree.get_depth(test_tree.root) == 3
def test(): from bst import Tree x = [10, 33, 1, 5, 65, 3, 25, 167, 34, 6, 15, 28] bst = Tree(x[0]) for val in x[1:]: bst.insert(bst.root, val) i = BSTIterator(bst.root) while i.hasNext(): print(i.next())
def test_in_order_one(): """Test sort one node.""" from bst import Tree test_list = [10] test_tree = Tree(test_list) path = test_tree.in_order(test_tree.root) count = 0 for i in path: assert i.data == test_list[count] count += 1
def test_in_order_multiple_nodes(): """Test sort multiple nodes.""" from bst import Tree test_list = [5, 2, 6, 1, 4] test_tree = Tree(test_list) path = test_tree.in_order(test_tree.root) test_results = [1, 2, 4, 5, 6] count = 0 for i in path: assert i.data == test_results[count] count += 1
def test_post_order_multiple_nodes(): """Test postorder on larger tree.""" from bst import Tree test_list = [10, 7, 12, 5, 9, 11, 13, 4] test_tree = Tree(test_list) test_results = [7, 5, 4, 9, 12, 11, 13, 10] path = test_tree.post_order(test_tree.root) count = 0 for i in path: assert i.data == test_results[count] count += 1
def get_avg_height(N: int, t: int, print_statements=False) -> float: height_sum = 0 for i in range(t): rand = [random.randrange(1, 501) for x in range(N)] tree = Tree() for x in rand: tree.insert(x) height_sum += tree.height() if print_statements: print("The height of the tree with {} nodes is {}".format( N, tree.height())) avg = height_sum / t if print_statements: print("The average height is", avg) return avg
def main(): level = int(sys.argv[1]) for _ in range(100): r_array = list(random.randint(-20, 20) for _ in range(7)) r_in = ' '.join(map(str, r_array)) user_out = os.popen('echo "{}" | ./a.out'.format(r_in)).read().strip() user_array = list(map(int, user_out.split())) print('{}\t->\t{}'.format(r_in, user_out)) tree = Tree() tree.add(r_array) if level == 0: expected_array = breadth_first_search(tree) elif level == 1: expected_array = pre_order_search(tree) elif level == 2: expected_array = post_order_search(tree) else: raise NotImplementedError assert user_array == expected_array, 'expected {}, got {}'.format( expected_array, user_array)
def empty_t(): """Create empty tree.""" from bst import Tree return Tree()
def test_set_passed_as_iterable(): """Set passed as interable should populate the tree""" tree = Tree([10, 5, 100]) assert tree.root.value == 10 assert tree.root.left.value == 5 assert tree.root.right.value == 100
def test_tuple_passed_as_iterable(): """Tuple passed as interable should populate the tree""" tree = Tree((10, 5, 100)) assert tree.root.value == 10 assert tree.root.left.value == 5 assert tree.root.right.value == 100
def bst(): from bst import Tree return Tree()
def test_create_tree_none_root(): """Test create an empty tree.""" from bst import Tree test_tree = Tree() assert test_tree.size() == 0
def test_get_depth_one_node(): """Test one node has depth of one.""" from bst import Tree test_tree = Tree() test_tree.insert(5) assert test_tree.get_depth(test_tree.root) == 1
def test_remove_left_child_larger_list(): """Test remove right child from a larger list.""" from bst import Tree test_tree = Tree([10, 7, 12, 15, 5]) test_tree.delete_node(7) assert test_tree.root.left.data == 5
def test_insert_one_node_return_size_one(): """Test for one node insertion.""" from bst import Tree test_tree = Tree() test_tree.insert(1) assert test_tree.size() == 1
def one_t(): """Create tree with one node.""" from bst import Tree t = Tree() t.insert(10) return t
def test_get_depth_no_nodes(): """Test no nodes has depth 0.""" from bst import Tree test_tree = Tree() assert test_tree.get_depth() == 0
def test_remove_root_larger_list(): """Test remove root from larger list.""" from bst import Tree test_tree = Tree([10, 7, 12, 15, 5]) test_tree.delete_node(10) assert test_tree.root.data == 7