def test_simple_successor(): tree = BSTree() root = BSTreeNode(4) tree.add_node(root) random.seed('test') data = map(lambda _: random.randint(0, 1000), range(0, 100)) nodes = [tree.add_node(BSTreeNode(i)) for i in data] sorted_nodes = list(BSTreeNode.inorder_walk(root)) for i in range(100): current_node = sorted_nodes[i] next_index = i + 1 if i != 100 else 100 previous_index = i - 1 if i != 0 else 0 expected_successor_node = sorted_nodes[next_index] actual_successor_node = BSTreeNode.get_successor_for_node(current_node) assert expected_successor_node.key == actual_successor_node.key expected_predecessor_node = sorted_nodes[previous_index] actual_predecessor_node = BSTreeNode.get_predecessor_for_node( current_node) assert actual_predecessor_node and ( expected_predecessor_node.key == actual_predecessor_node.key) or True
def test_simple_successor(): tree = BSTree() root = BSTreeNode(4) tree.add_node(root) random.seed('test') data = map(lambda _: random.randint(0, 1000), range(0, 100)) nodes = [tree.add_node(BSTreeNode(i)) for i in data] sorted_nodes = list(BSTreeNode.inorder_walk(root)) for i in range(100): current_node = sorted_nodes[i] next_index = i+1 if i!=100 else 100 previous_index = i-1 if i != 0 else 0 expected_successor_node = sorted_nodes[next_index] actual_successor_node = BSTreeNode.get_successor_for_node(current_node) assert expected_successor_node.key == actual_successor_node.key expected_predecessor_node = sorted_nodes[previous_index] actual_predecessor_node = BSTreeNode.get_predecessor_for_node(current_node) assert actual_predecessor_node and (expected_predecessor_node.key == actual_predecessor_node.key) or True
def test_insertion(): root = BSTreeNode(4) tree = BSTree() tree.add_node(root) values = [2, 7, 6, 12, 3, 14, 5, 2, 8, 3, 4, 15, 3] [tree.add_node(BSTreeNode(_)) for _ in values] expected = sorted(values + [root.key]) actual = [_.key for _ in BSTreeNode.inorder_walk(root, False)] assert expected == actual
def test_inorder_walk_non_recursive(): tree = BSTree() root = BSTreeNode(4) tree.add_node(root) values = [2, 7, 4, 15, 3] for i in values: tree.add_node(BSTreeNode(i)) expected = sorted(values + [root.key]) actual = [_.key for _ in BSTreeNode.inorder_walk(root, False)] assert expected == actual
def inorder_walk(self): yield from BSTreeNode.inorder_walk(self.root, False)