def test_tree_two(): tree = BinaryTree() one = Node(42) two = Node(100) three = Node(600) four = Node(15) five = Node(160) six = Node(200) seven = Node(350) eight = Node(125) nine = Node(175) ten = Node(4) eleven = Node(500) tree.root = one one.left = two one.right = three two.left = four two.right = five three.left = six three.right = seven five.left = eight five.right = nine seven.left = ten seven.right = eleven return tree
def test_binary_tree_instance(): grok = BinaryTree() assert grok.root == None groot = Node('I am Groot') grok.root = groot assert grok.root.value == 'I am Groot' assert grok.__repr__() == 'An instance of a BinaryTree, root is I am Groot'
def test_one_node(): tree = BinaryTree() node1 = Node(5) tree.root = node1 actual = tree.breadth_first() expected = [5] assert actual == expected
def test_breadth_first_one_node(): tree = BinaryTree() a = Node('A') tree.root = a actual = tree.breadth_first() expected = ['A'] assert actual == expected
def test_breadth_on_big_left_branch(): tree = BinaryTree() tree.add(1) tree.root.left = Node(2) tree.root.left.left = Node(3) tree.root.left.left.left = Node(4) tree.root.left.left.left.left = Node(5) assert tree.breadth_traverse() == [1, 2, 3, 4, 5]
def test_max(): tree = BinaryTree() tree.add(3) tree.add(4) tree.add(11) tree.add(5) tree.add(9) assert tree.find_maximum_value() == 11
def test_all_negatives(): tree = BinaryTree() tree.add(-3) tree.add(-4) tree.add(-11) tree.add(-5) tree.add(-9) assert tree.find_maximum_value() == -3
def test_tree2(): tree = BinaryTree() tree.root = Node(50) tree.root.left = Node(25) tree.root.right = Node(75) tree.root.left.left = Node(20) tree.root.right.right = Node(80) tree.root.left.right = Node(40) tree2 = BinaryTree() tree2.root = Node(50) tree2.root.left = Node(3) tree2.root.right = Node(4) tree2.root.left.left = Node(6) tree2.root.right.right = Node(7) tree2.root.left.right = Node(8) actual = tree_intersection(tree, tree2) expected = [50] assert actual == expected
def test_add_left_and_right(): tree = BinaryTree() node1 = Node(1) node2 = Node(3) node3 = Node(5) node1.left = node2 node1.right = node3 tree.root = node1 assert node1.left.value == 3 and node1.right.value == 5
def test_tree1(): tree = BinaryTree() tree.root = Node(50) tree.root.left = Node(25) tree.root.right = Node(75) tree.root.left.left = Node(20) tree.root.right.right = Node(80) tree.root.left.right = Node(40) tree2 = BinaryTree() tree2.root = Node(50) tree2.root.left = Node(25) tree2.root.right = Node(75) tree2.root.left.left = Node(20) tree2.root.right.right = Node(80) tree2.root.left.right = Node(40) actual = tree_intersection(tree, tree2) expected = [50, 25, 20, 40, 75, 80] assert actual == expected
def test_left_right_child_tree(): tree = BinaryTree() one = Node(25) two = Node(12) three = Node(30) tree.root = one one.left = two one.right = three assert one.left and one.right expected = two and three
def test_ret_in_order(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) actual = BinaryTree.breadth_first(tree) expected = [1, 2, 3, 4, 5] assert actual == expected
def test_breadth_balanced_tree(): tree = BinaryTree() tree.add(1) tree.root.left = Node(2) tree.root.left.left = Node(4) tree.root.left.right = Node(5) tree.root.right = Node(3) tree.root.right.left = Node(6) tree.root.right.right = Node(7) assert tree.breadth_traverse() == [1, 2, 3, 4, 5, 6, 7]
def test_post_order_traversal(): tree = BinaryTree() tree.root = Node(1) tree.root.left = Node(2) tree.root.right = Node(3) tree.root.left.left = Node(4) tree.root.left.right = Node(5) actual = tree.post_order() expected = [4, 5, 2, 3, 1] assert actual == expected
def test_breadth_balanced_negative_tree(): tree = BinaryTree() tree.add(-1) tree.root.left = Node(-2) tree.root.left.left = Node(-4) tree.root.left.right = Node(-5) tree.root.right = Node(-3) tree.root.right.left = Node(-6) tree.root.right.right = Node(-7) assert tree.breadth_traverse() == [-1, -2, -3, -4, -5, -6, -7]
def test_postorder_traversal(): tree = BinaryTree() node1 = Node(1) node2 = Node(3) node3 = Node(5) node1.left = node2 node1.right = node3 tree.root = node1 actual = tree.post_order() expected = [3, 5, 1] assert actual == expected
def test_return_breadth_list(): tree = BinaryTree() node1 = Node(1) node2 = Node(9) node3 = Node(5) node1.left = node2 node1.right = node3 tree.root = node1 actual = tree.breadth_first() expected = [1, 9, 5] assert actual == expected
def test_return_desired_value(): tree = BinaryTree() node1 = Node(1) node2 = Node(3) node3 = Node(5) node1.left = node2 node1.right = node3 tree.root = node1 actual = tree.find_max_value() expected = 5 assert actual == expected
def binary(): node1 = TNode(15) node1.left = TNode(7) node1.right = TNode(5) node1.left.left = TNode(10) node1.left.right = TNode(6) node1.left.right.left = TNode(20) node1.left.right.right = TNode(25) node1.right.right = TNode(9) node1.right.right.left = TNode(21) tree = BinaryTree(node1) return tree
def test_find_maximum_on_left(): grok = BinaryTree() groot = Node(12) i = Node(9) am = Node(5) starlord = Node(3) rocket = Node(7) grok.root = i grok.root.left = am grok.root.right = starlord grok.root.left.left = groot grok.root.left.right = rocket actual = grok.find_maximum_value() assert actual == 12
def test_breadth_first(): grok = BinaryTree() groot = Node('Groot') i = Node('I') am = Node('am') starlord = Node('Iowa') rocket = Node('Trash Panda') grok.root = i grok.root.left = am grok.root.right = groot grok.root.left.left = starlord grok.root.left.right = rocket expected = ['I', 'am', 'Groot', 'Iowa', 'Trash Panda'] assert grok.breadth_first() == expected
def test_post_order(): grok = BinaryTree() groot = Node('Groot') i = Node('I') am = Node('Am') starlord = Node('Future Antman') rocket = Node('Trash Panda') grok.root = i grok.root.left = am grok.root.right = starlord grok.root.left.left = groot grok.root.left.right = rocket assert grok.post_order() == [ 'Groot', 'Trash Panda', 'Am', 'Future Antman', 'I' ]
def test_post_order(): tree = BinaryTree() tree.root = Node(50) tree.root.left = Node(25) tree.root.right = Node(75) tree.root.left.left = Node(20) tree.root.right.right = Node(80) tree.root.left.right = Node(40) """ 50 25 75 20 40 80 """ actual = tree.post_order() expected = [20, 40, 25, 80, 75, 50] assert actual == expected
def test_tree(): tree = BinaryTree() one = Node(1) two = Node(2) three = Node(3) four = Node(4) five = Node(5) six = Node(6) tree.root = one one.left = two one.right = three two.left = four two.right = five three.left = six return tree
def test_breadth_first_even(): tree = BinaryTree() a = Node('A') b = Node('B') c = Node('C') d = Node('D') e = Node('E') f = Node('F') tree.root = a a.left = b a.right = c b.left = d b.right = e c.left = f actual = tree.breadth_first() expected = ['A', 'B', 'C', 'D', 'E', 'F'] assert actual == expected
node1.right = TNode(250) node1.right.left = TNode(200) node1.right.right = TNode(350) node1.right.right.left = TNode(300) node1.right.right.right = TNode(500) node2 = TNode(42) node2.left = TNode(100) node2.left.left = TNode(15) node2.left.right = TNode(160) node2.left.right.left = TNode(125) node2.left.right.right = TNode(175) node2.right = TNode(600) node2.right.left = TNode(200) node2.right.right = TNode(350) node2.right.right.left = TNode(4) node2.right.right.right = TNode(500) binary_tree = BinaryTree(node1) binary_tree2 = BinaryTree(node2) print(tree_intersection(binary_tree, binary_tree2))
def test_binary_tree_add_twice(): tree = BinaryTree(4) tree.add(2) tree.add(3) assert tree.root.right.value == 3
def test_binary_tree_add_once(): tree = BinaryTree(4) tree.add(3) assert tree.root.left.value == 3
def test_binary_tree_root(): tree = BinaryTree(5) assert tree.root.value == 5
def test_empty_binary_tree(): tree = BinaryTree() assert isinstance(tree, BinaryTree) assert tree.root is None