def test_search_node(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8]: tree.add_value(val) result = tree.search(9) expected_node = Node(9) expected_node.add_node(Node(8)) expected_node.add_node(Node(11)) self.assertEqual(result, expected_node)
:param root: :return: """ pass def is_same_tree(self, t1, t2): """ 判断两个二叉树是否完全相同 :param t1: :param t2: :return: """ if t1 is None and t2 is None: return True elif t1 is None or t2 is None: return False if t1.item != t2.item: return False left = self.is_same_tree(t1.lchild, t2.lchild) right = self.is_same_tree(t1.rchild, t2.rchild) return left & right if __name__ == '__main__': tree = Tree() for i in range(1, 11): tree.add(i) judge = TreeJudge() result = judge.is_balance_tree(tree.root) != -1 print(result)
def test_node_depth(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8, 13, 15, 12, 10, 14]: tree.add_value(val) result = tree.depth(14) self.assertEqual(result, 5)
def test_node_height(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8, 13, 15, 12, 10, 14]: tree.add_value(val) result = tree.height(9) self.assertEqual(result, 4)
def test_tree_level(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8]: tree.add_value(val) result = tree.level() self.assertEqual(result, 3)
def test_node_degree(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8]: tree.add_value(val) result = tree.degree(6) self.assertEqual(result, 1)
def test_internal_nodes(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8]: tree.add_value(val) result = tree.internal_nodes() self.assertEqual(result, [3, 6, 7, 9])
def test_leaf_nodes(self): tree = Tree() for val in [7, 3, 6, 9, 11, 5, 2, 8]: tree.add_value(val) result = tree.leaf_nodes() self.assertEqual(result, [2, 5, 8, 11])