def create_preorder_inorder(preorder, prerange, inorder, inrange): pre_left, pre_right = prerange if pre_right - pre_left == 0: return TreeNode(preorder[pre_right]) # find root in preorder value = preorder[pre_left] node = TreeNode(value) # find root in inorder in_left, in_right = inrange i = in_left while i <= in_right: if inorder[i] == value: break i += 1 # get left node left_inorder = (in_left, i - 1) left_diff = (i - 1) - in_left left_preorder = (pre_left + 1, pre_left + left_diff + 1) node.left = create_preorder_inorder(preorder, left_preorder, inorder, left_inorder) # get right node right_inorder = (i + 1, in_right) right_diff = in_right - (i + 1) right_preorder = (pre_right - right_diff, pre_right) node.right = create_preorder_inorder(preorder, right_preorder, inorder, right_inorder) return node
def create_postorder_inorder(postorder, postrange, inorder, inrange): post_left, post_right = postrange if post_right - post_left == 0: return TreeNode(postorder[post_right]) # find root in postorder value = postorder[post_right] node = TreeNode(value) # find root in inorder in_left, in_right = inrange i = in_left while i <= in_right: if inorder[i] == value: break i += 1 # generate left node left_inrange = (in_left, i - 1) left_diff = (i - 1) - in_left left_postrange = (post_left, post_left + left_diff) node.left = create_postorder_inorder(postorder, left_postrange, inorder, left_inrange) # generate right node right_inrange = (i + 1, in_right) right_diff = in_right - (i + 1) right_postrange = (post_right - right_diff - 1, post_right - 1) node.right = create_postorder_inorder(postorder, right_postrange, inorder, right_inrange) return node
def create_bst_from_array(arr, left, right): if left > right: return None middle = (left + right) // 2 result = TreeNode(arr[middle]) result.left = create_bst_from_array(arr, left, middle - 1) result.right = create_bst_from_array(arr, middle + 1, right) return result
def test_count_unival_full(self): root = TreeNode(0) root.left = TreeNode(1) root.right = TreeNode(0) root.right.left = TreeNode(1) root.right.right = TreeNode(0) root.right.left.left = TreeNode(1) root.right.left.right = TreeNode(1) assert count_unival(root) == 5
def test_symmetric_good(self): root = TreeNode(1) root.left = TreeNode(2) root.left.left = TreeNode(3) root.left.right = TreeNode(4) root.right = TreeNode(2) root.right.left = TreeNode(4) root.right.right = TreeNode(3) assert symmetry.isTreeSymmetric(root)
def test_is_bst_bad_basic(self, func): root = TreeNode(2) root.left = TreeNode(1) root.right = TreeNode(4) root.right.left = TreeNode(6) root.right.right = TreeNode(5) assert func(root) is False
def test_count_complete_small(self): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) assert count_complete(root) == 5
def test_is_bst_good_basic(self, func): root = TreeNode(2) root.left = TreeNode(1) root.right = TreeNode(4) root.right.left = TreeNode(3) root.right.right = TreeNode(5) assert func(root) is True
def test_count_unival_partial_2(self): root = TreeNode(5) root.left = TreeNode(5) root.left.left = TreeNode(5) root.right = TreeNode(5) root.right.right = TreeNode(5) assert count_unival(root) == 5
def test_remove_bad_edge(self): root = TreeNode(6) root.left = TreeNode(3) root.right = TreeNode(8) root.left.left = TreeNode(2) root.left.left.right = TreeNode(8) remove_bad_edge(root) assert root.right is None
def huffman(frequencies): result = {} least_queue = [] for ch, freq in frequencies.items(): heapq.heappush(least_queue, (freq, TreeNode(ch))) while len(least_queue) > 1: l_freq, l_node = heapq.heappop(least_queue) r_freq, r_node = heapq.heappop(least_queue) i_node = TreeNode("*") i_node.left = l_node i_node.right = r_node i_freq = l_freq + r_freq heapq.heappush(least_queue, (i_freq, i_node)) l_freq, l_node = heapq.heappop(least_queue) populate(l_node, result, "") return result
def test_connect_nodes(self, func): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.right.left = TreeNode(5) root.right.right = TreeNode(6) func(root) assert root.left.next is root.right assert root.right.next is None assert root.left.left.next is root.right.left assert root.right.left.next is root.right.right assert root.right.right.next is None
def test_is_bst_bad_left(self, func): root = TreeNode(2) root.left = TreeNode(1) root.left.right = TreeNode(5) root.left.right.left = TreeNode(3) root.left.right.right = TreeNode(7) assert func(root) is False
def test_inorder_optimal_moderate(self): """ 1 / \ 2 3 / 5 / \ 8 6 """ r = TreeNode(1) r.left = TreeNode(2) r.left.left = TreeNode(5) r.left.left.left = TreeNode(8) r.left.left.right = TreeNode(6) r.right = TreeNode(3) assert [n for n in inorder_optimal(r)] == [8, 5, 6, 2, 1, 3]
def test_is_balanced(self): root = TreeNode(1) root.insert_left(2) root.insert_right(3) root.left.insert_left(4) root.left.insert_right(5) assert is_balanced(root) is True
def test_inorder_optimal_symmetric(self): """ 1 / \ 2 2 / \ 3 3 """ r = TreeNode(1) r.left = TreeNode(2) r.left.left = TreeNode(3) r.right = TreeNode(2) r.right.right = TreeNode(3) assert [n for n in inorder_optimal(r)] == [3, 2, 1, 2, 3]
def test_is_balanced_optimal_good(self): root = TreeNode(1) root.insert_left(2) root.insert_right(3) root.left.insert_left(4) root.left.insert_right(5) balanced, _ = is_balanced_optimal(root) assert balanced is True
def test_inorder_optimal_simple(self): """ 1 / \ 2 3 """ r = TreeNode(1) r.left = TreeNode(2) r.right = TreeNode(3) assert [n for n in inorder_optimal(r)] == [2, 1, 3]
def test_duplicate_subtree(self): root = TreeNode("A") root.left = TreeNode("B") root.left.left = TreeNode("D") root.left.right = TreeNode("E") root.right = TreeNode("C") root.right.right = TreeNode("B") root.right.right.left = TreeNode("D") root.right.right.right = TreeNode("E") assert duplicate_subtree(root) is True
def test_root(self): root = TreeNode(1) assert root.value == 1 assert root.left is None assert root.right is None
def test_children(self): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) assert root.left.value == 2 assert root.right.value == 3
def test_count_complete_big(self): root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) root.right.left = TreeNode(6) root.right.right = TreeNode(7) root.left.left.left = TreeNode(8) root.left.left.right = TreeNode(9) root.left.right.left = TreeNode(10) root.left.right.right = TreeNode(11) assert count_complete(root) == 11