def test_successor_returns_successor_given_right_subtree_exists(): bst = BinarySearchTree(5) bst.insert_node(TreeNode(8)) bst.insert_node(TreeNode(7)) successor_node = TreeNode(6) bst.insert_node(successor_node) assert_equal(successor(bst.root), successor_node)
def test_successor_returns_none_given_leaf_of_right_most_branch(): from_node = TreeNode(8) bst = BinarySearchTree(5) bst.insert_node(TreeNode(3)) bst.insert_node(TreeNode(7)) bst.insert_node(TreeNode(6)) bst.insert_node(from_node) assert_is_none(successor(from_node))
def test_successor_returns_next_parent_on_right_given_no_right_sub_tree_exists(): bst = BinarySearchTree(5) successor_node = TreeNode(4) bst.insert_node(successor_node) bst.insert_node(TreeNode(2)) from_node = TreeNode(3) bst.insert_node(from_node) assert_equal(successor(from_node), successor_node)
def test_successor_returns_parent_given_no_right_sub_tree_exists_parent_on_right(): bst = BinarySearchTree(5) bst.insert_node(TreeNode(3)) parent = TreeNode(8) bst.insert_node(parent) from_node = TreeNode(7) bst.insert_node(from_node) bst.insert_node(TreeNode(9)) assert_equal(successor(from_node), parent)
def test_is_binary_search_tree_returns_true_given_valid_bst(): bst = TreeNode(5) bst.left = TreeNode(3) bst.right = TreeNode(6) bst.left.left = TreeNode(2) bst.left.right = TreeNode(4) assert_true(is_binary_search_tree(bst))
def test_in_balance_returns_false_when_tree_not_balanced(): root = TreeNode(5) root.left = TreeNode(3) root.left.left = TreeNode(2) root.left.left.left = TreeNode(1) root.right = TreeNode(6) assert_false(is_balanced(root))
def build_minimal_tree(arr): """Builds a tree of minimal height by recursively partitioning the array Args: arr: list from which to build the binary tree Returns: Binary Search Tree of minimal height """ if len(arr) > 0: root_index = len(arr) // 2 root = TreeNode(arr[root_index]) root.left = build_minimal_tree(arr[:root_index]) root.right = build_minimal_tree(arr[root_index + 1:]) return root return None
def test_is_binary_search_tree_returns_false_given_invalid_bst(): bst = TreeNode(5) bst.left = TreeNode(3) bst.right = TreeNode(6) bst.left.left = TreeNode(2) bst.left.right = TreeNode( 40 ) # This node violates the order property since it is greater than the root assert_false(is_binary_search_tree(bst))
def test_in_balanced_returns_true_when_tree_is_balanced(): root = TreeNode(5) root.left = TreeNode(3) root.left.left = TreeNode(2) root.right = TreeNode(6) assert_true(is_balanced(root))
def insert(self, data): """ Inserts the data and maintains the order property in the tree Args: data: data to be inserted in a Tree """ self._insert_node(TreeNode(data), self.root)
def test_list_of_depths_populates_from_multi_level_tree(): root = TreeNode(5) root.left = TreeNode(2) root.left.left = TreeNode(1) root.left.right = TreeNode(3) root.left.right.right = TreeNode(4) root.right = TreeNode(6) result = list_of_depths(root) expected = [ [root], [root.left, root.right], [root.left.left, root.left.right], [root.left.right.right], ] assert_equal(result, expected)
def test_list_of_depths_returns_populates_correctly_from_stick_tree(): root = TreeNode(5) root.left = TreeNode(4) root.left.left = TreeNode(3) root.left.left.left = TreeNode(2) root.left.left.left.left = TreeNode(1) result = list_of_depths(root) expected = [[root], [root.left], [root.left.left], [root.left.left.left], [root.left.left.left.left]] assert_equal(result, expected)
def __init__(self, data): """ Args: data: data to be inserted at the root of the tree """ self.root = TreeNode(data)