Пример #1
0
 def unserialized_tree(cls, nodes_info):
     cur = nodes_info.pop(0)
     if cur == '#':
         return None
     cur_node = Node(cur)
     cur_node.left = cls.unserialized_tree(nodes_info)
     cur_node.right = cls.unserialized_tree(nodes_info)
     return cur_node
Пример #2
0
    def reconstruct_detail(cls, arr, start, end):
        if start > end:
            return None

        pos = (start + end) // 2
        node = Node(arr[pos])

        node.left = cls.reconstruct_detail(arr, start, pos - 1)
        node.right = cls.reconstruct_detail(arr, pos + 1, end)

        return node
Пример #3
0
    def reconstruct_in_mid_order_detail(cls, mid_arr, mid_start, mid_end, pos_arr, pos_start, pos_end, d):
        if pos_start > pos_end:
            return

        root = Node(pos_arr[pos_end])
        pos = d.get(pos_arr[pos_end])
        root.left = cls.reconstruct_in_mid_order_detail(mid_arr, mid_start, pos-1, pos_arr, pos_start, pos_start +
                                                        pos - mid_start - 1, d)
        root.right = cls.reconstruct_in_mid_order_detail(mid_arr, pos+1, mid_end, pos_arr, pos_start + pos - mid_start,
                                                         pos_end - 1, d)

        return root
Пример #4
0
    def reconstruct_in_pre_order_detail(cls, pre_arr, pre_start, pre_end, in_arr, in_start, in_end, d):
        if pre_start > pre_end:
            return
        root = Node(pre_arr[pre_start])
        pos = d.get(pre_arr[pre_start])
        # 注意 这里 pos - in_start表示前序遍历数组的长度,再加上pre_start就表示分割后的前序数组的范围
        root.left = cls.reconstruct_in_pre_order_detail(pre_arr, pre_start + 1, pre_start+(pos-in_start), in_arr,
                                                        in_start, pos-1, d)
        root.right = cls.reconstruct_in_pre_order_detail(pre_arr, pre_start+(pos-in_start)+1, pre_end, in_arr,
                                                         pos+1, in_end, d)

        return root
Пример #5
0
    def reconstruct_in_pre_pos_order_detail(cls, p, pi, pj, s, si, sj, d):
        head = Node(s[sj])
        sj -= 1

        if pi == pj:
            return head

        pi += 1
        index = d.get(p[pi])

        head.left = cls.reconstruct_in_pre_pos_order_detail(p, pi, pi+index-si, s, si, index, d)

        head.right = cls.reconstruct_in_pre_pos_order_detail(p, pi+index-si+1, pj, s, index+1, sj, d)
        return head
Пример #6
0
    def recontruct_bst_detail(cls, arr, start, end):
        if start > end:
            return None

        less = -1
        more = end

        cur_node = Node(arr[end])

        for i in range(start, end):
            if arr[i] < arr[end]:
                less = i
            else:
                more = i if more == end else more

        cur_node.left = cls.recontruct_bst_detail(arr, start, less)
        cur_node.right = cls.recontruct_bst_detail(arr, more, end - 1)
        return cur_node
Пример #7
0
    def get_root(cls, start, end):
        res = list()
        if start > end:
            res.append(None)

        for i in range(start, end+1):
            node = Node(i)
            left_nodes = cls.get_root(start, i-1)
            right_nodes = cls.get_root(i+1, end)
            r = product(left_nodes, right_nodes)
            for left, right in r:
                node.left = left
                node.right = right
                res.append(deepcopy(node))
            #
            # for left in left_nodes:
            #     for right in right_nodes:
            #         node.left = left
            #         node.right = right
            #         res.append(cls.clone_tree(node))

        return res
Пример #8
0
            return list(self.errors[0])

        if len(self.errors) == 2:
            return [self.errors[0][0], self.errors[1][1]]

    def get_all_nodes(self, head):
        if head is None:
            return None
        if head.left is not None:
            self.get_all_nodes(head.left)
        self.nodes.append(head)
        if head.right is not None:
            self.get_all_nodes(head.right)


if __name__ == '__main__':
    head14 = Node(5)
    head14.left = Node(3)
    head14.right = Node(7)
    head14.left.left = Node(2)
    head14.left.right = Node(8)
    head14.right.left = Node(6)
    head14.right.right = Node(4)
    head14.left.left.left = Node(1)

    bst = BSTChecker()
    bst.get_all_nodes(head14)
    err_nodes = bst.get_wrong_nodes()
    for err_node in err_nodes:
        print(err_node.value)
Пример #9
0
                    elif node.value < temp.value:
                        temp = temp.left
                    else:
                        temp = temp.right
            else:
                if node.left is not None:
                    queue.append(node.left)
                if node.right is not None:
                    queue.append(node.right)
                count += 1

        return count


if __name__ == '__main__':
    head = Node(6)
    head.left = Node(1)
    head.left.left = Node(0)
    head.left.right = Node(3)
    head.right = Node(12)
    head.right.left = Node(10)
    head.right.left.left = Node(4)
    head.right.left.left.left = Node(2)
    head.right.left.left.right = Node(5)
    head.right.left.right = Node(14)
    head.right.left.right.left = Node(11)
    head.right.left.right.right = Node(15)
    head.right.right = Node(13)
    head.right.right.left = Node(20)
    head.right.right.right = Node(16)
    print(BiggestTop.find_biggest_top(head))
Пример #10
0
        queue.append((head, None))
        while len(queue) > 0:
            cur, parent = queue.pop(0)
            hash_table[cur] = parent

            if cur.left:
                queue.append((cur.left, cur))
            if cur.right:
                queue.append((cur.right, cur))

        return hash_table


if __name__ == '__main__':
    head = Node(1)
    head.left = Node(2)
    head.right = Node(3)
    head.left.left = Node(4)
    head.left.right = Node(5)
    head.right.left = Node(6)
    head.right.right = Node(7)
    head.right.right.left = Node(8)

    o1 = head.left.right
    o2 = head.right.left

    print("o1 : " + str(o1.value))
    print("o2 : " + str(o2.value))
    print("ancestor : " + str(AncestorFinder.find_ancestor_by_recurise(head, o1, o2).value))
    print("ancestor : " + str(AncestorFinder.find_ancestor_by_hash(head, o1, o2).value))
    print("===============")
Пример #11
0
        if head is None:
            return '#!'

        s = ''

        s += str(head.value)
        s += '!'

        s += cls.pre_order_visit(head.left)
        s += cls.pre_order_visit(head.right)
        return s


if __name__ == '__main__':
    t1 = Node(1)
    t1.left = Node(2)
    t1.right = Node(3)
    t1.left.left = Node(4)
    t1.left.right = Node(5)
    t1.right.left = Node(6)
    t1.right.right = Node(7)
    t1.left.left.right = Node(8)
    t1.left.right.left = Node(9)

    t2 = Node(2)
    t2.left = Node(4)
    t2.left.right = Node(8)
    t2.right = Node(5)
    t2.right.left = Node(9)

    print(TopTreeTool.is_all_top(t1, t2))
Пример #12
0
        while len(stack1) > 0:
            cur = stack1.pop()
            if cur.left is not None:
                stack1.append(cur.left)
            if cur.right is not None:
                stack1.append(cur.right)

            stack2.append(cur.value)

        while len(stack2) > 0:
            print(stack2.pop(), end=' ')

if __name__ == '__main__':
    head = Node(5)
    head.left = Node(3)
    head.right = Node(8)
    head.left.left = Node(2)
    head.left.right = Node(4)
    head.left.left.left = Node(1)
    head.right.left = Node(7)
    head.right.left.left = Node(6)
    head.right.right = Node(10)
    head.right.right.left = Node(9)
    head.right.right.right = Node(11)

    RecursiveVisit.visit_in_first_order(head)
    print()
    LoopVisit.visit_in_first_order(head)
    print()
    print('===========================')
Пример #13
0
        lmax = cls.find_max_distance_detail(head.left, record)
        max_from_left = record[0]

        rmax = cls.find_max_distance_detail(head.right, record)
        max_from_right = record[0]

        record[0] = max([max_from_left, max_from_right]) + 1

        cur_node_length = max_from_left + max_from_right + 1

        return max([lmax, rmax, cur_node_length])


if __name__ == '__main__':
    head1 = Node(1)
    head1.left = Node(2)
    head1.right = Node(3)
    head1.left.left = Node(4)
    head1.left.right = Node(5)
    head1.right.left = Node(6)
    head1.right.right = Node(7)
    head1.left.left.left = Node(8)
    head1.right.left.right = Node(9)
    print(MaxDistance.find_max_distance(head1))

    head2 = Node(1)
    head2.left = Node(2)
    head2.right = Node(3)
    head2.right.left = Node(4)
    head2.right.right = Node(5)
    head2.right.left.left = Node(6)
Пример #14
0
            return
        cls.print_in_order(head, 0, 'H', 17)

    @classmethod
    def print_in_order(cls, head, height, char, length):
        if head is None:
            return
        cls.print_in_order(head.right, height + 1, 'v', length)
        cls.print_cur(head, height, char, length)
        cls.print_in_order(head.left, height + 1, '^', length)

    @classmethod
    def print_cur(cls, head, height, char, length):
        print_str = char + str(head.value) + char
        left_str = int((length - len(print_str)) / 2)
        right_str = length - len(print_str) - left_str
        print(' ' * length * height + ' ' * left_str + char + str(head.value) +
              char + ' ' * right_str)


if __name__ == '__main__':
    import sys

    head = Node(1)
    head.left = Node(-222222222)
    head.right = Node(3)
    head.left.left = Node(sys.maxsize)
    head.right.left = Node(55555555)
    head.right.right = Node(66)
    head.left.left.right = Node(777)
    PrintTree.print_tree(head)