Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 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
Пример #4
0
 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
Пример #5
0
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
Пример #6
0
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
Пример #7
0
 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
Пример #8
0
 def test_symmetric_bad(self):
     root = TreeNode(1)
     root.left = TreeNode(2)
     root.left.right = TreeNode(3)
     root.right = TreeNode(2)
     root.right.right = TreeNode(3)
     assert not symmetry.isTreeSymmetric(root)
Пример #9
0
 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
Пример #10
0
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
Пример #11
0
 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
Пример #12
0
 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]
Пример #13
0
 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
Пример #14
0
 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
Пример #15
0
 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
Пример #16
0
 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]
Пример #17
0
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
Пример #18
0
 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]
Пример #19
0
 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