return cur_o2

    def set_map(self, _root):
        if _root is None:
            return

        if _root.left is not None:
            self.map.update({_root.left: _root})
        if _root.right is not None:
            self.map.update({_root.right: _root})

        self.set_map(_root.left)
        self.set_map(_root.right)


if __name__ == '__main__':
    n_list = [17, 5, 10, 12, 35, 2, 11, 29, 38]
    bst = BST(n_list)
    root = bst.get_root()
    flag1, n1, p1 = bst.search(root, root, 2)
    flag2, n2, p2 = bst.search(root, root, 11)
    lowest_ancestor_node = LowestAncestorNode(root)
    result = lowest_ancestor_node.query_lowest_ancestor_node(n1, n2)
    print('lowest ancestor node is: {0}'.format(result.data))

    print('=================================================')
    print('no extra space: ')
    result_1 = LowestAncestorNode.get_lowest_ancestor_node(root, n1, n2)
    print('lowest ancestor node is: {0}'.format(result_1.data))
예제 #2
0
    @staticmethod
    def process(node):
        if node is None:
            return ReturnType(True, 0)

        left_data = IsBalancedTree.process(node.left)
        right_data = IsBalancedTree.process(node.right)

        height = max(left_data.height, right_data.height) + 1

        is_balanced = left_data.is_balanced and right_data.is_balanced and abs(left_data.height - right_data.height) < 2

        return ReturnType(is_balanced, height)


if __name__ == '__main__':
    node_list = [17, 5, 35, 2, 11, 29, 38]
    bst = BST(node_list)
    root = bst.get_root()
    is_b = IsBalancedTree.is_balanced(root)
    print('is balanced: {0}'.format(is_b))

    print('=========================================')
    root_n = Node(1)
    root_n.left = Node(2)
    root_n.right = Node(1)
    root_n.left.left = Node(1)
    root_n.left.left.left = Node(4)
    is_b = IsBalancedTree.is_balanced(root_n)
    print('is balanced: {0}'.format(is_b))
예제 #3
0
        while not _queue.empty():
            node = _queue.get()
            left = node.left
            right = node.right

            if left is not None:
                level_map[left] = level_map[node] + 1
                _queue.put(left)
            if right is not None:
                level_map[right] = level_map[node] + 1
                _queue.put(right)

            if level_map[node] > cur_level:
                cur_level = level_map[node]
                cur_width = 1
            else:
                cur_width += 1

            max_width = max(cur_width, max_width)

        return max_width


if __name__ == '__main__':
    node_list = [17, 5, 35, 2, 11, 29, 38]
    tree_max_width = TreeMaxWidth()
    bst_tree = BST(node_list)
    width = tree_max_width.get_max_width(bst_tree.get_root())
    print(width)
예제 #4
0
        if node is None:
            return ReturnType(True, sys.maxsize, -sys.maxsize)

        left_data = IsBST.process(node.left)
        right_data = IsBST.process(node.right)

        v_min = min(node.data, min(left_data.min, right_data.min))
        v_max = max(node.data, max(left_data.max, left_data.max))

        is_bst = left_data.max < node.data \
            and left_data.isBST \
            and right_data.min > node.data \
            and right_data.isBST

        return ReturnType(is_bst, v_min, v_max)


if __name__ == '__main__':
    node_lyst = [17, 5, 35, 2, 11, 29, 38, 10, 12]
    bst = BST(node_lyst)
    result = IsBST.is_bst(bst.get_root())
    print(result)
    flag, n, p = bst.search(bst.get_root(), bst.get_root(), 38)
    print('=========================================')

    node = Node(10)
    node.left = Node(15)
    node.right = Node(5)
    node.left.left = Node(28)
    result_2 = IsBST.is_bst(node)
    print(result_2)
            queue.put(values[i])

        return SerializeAndReconstructTree.recon_by_pre_order(queue)

    @staticmethod
    def recon_by_pre_order(queue):
        value = queue.get()

        if value == '#':
            return None

        head = Node(int(value))
        head.left = SerializeAndReconstructTree.recon_by_pre_order(queue)
        head.right = SerializeAndReconstructTree.recon_by_pre_order(queue)

        return head


if __name__ == '__main__':
    sat = SerializeAndReconstructTree()
    n_list = [17, 5, 35, 2, 11, 29, 38]
    bst = BST(n_list)
    root = bst.get_root()
    str_tree = SerializeAndReconstructTree.serialize_by_pre(root)
    print('serialized tree: ', str_tree)

    print('reconstructed tree: ', end='')
    recon_tree = SerializeAndReconstructTree.reconstruct_by_pre_string(
        str_tree)
    bst.pre_order_un_recur(recon_tree)
예제 #6
0
            while parent is not None and parent.right is not cur:
                cur = parent
                parent = cur.parent
            return parent

    @staticmethod
    def get_most_right(node):
        if node is None:
            return

        cur = node

        while cur.right is not None:
            cur = cur.right

        return cur


if __name__ == '__main__':
    n_list = [17, 5, 10, 12, 35, 2, 11, 29, 38]
    bst = BST(n_list)
    root = bst.get_root()
    flag, n, p= bst.search(root, root, 10)
    bst.in_order_un_recur(root)
    precursor = Precursor.precursor(n)
    print(precursor.data)
    f1, n1, p1 = bst.search(root, root, 38)
    precursor1 = Precursor.precursor(n1)
    print(precursor1.data)
예제 #7
0
            return SuccessorNode.get_most_left(node.right)
        else:
            cur = node
            parent = cur.parent
            while parent is not None and parent.left is not cur:
                cur = cur.parent
                parent = cur.parent
            return parent

    @staticmethod
    def get_most_left(node):
        if node is None:
            return

        cur = node
        while cur.left is not None:
            cur = node.left

        return cur


if __name__ == '__main__':
    n_list = []
    for i in self_range(4, 50, step=4):
        n_list.append(i)
    bst = BST(n_list)
    root = bst.get_root()
    bst.in_order_un_recur(root)
    flag, n, p = bst.search(root, root, 4)
    print(SuccessorNode.get_successor_node(n).data)
예제 #8
0
        stack = LifoQueue()
        stack.put(root)
        level_map = dict()
        cur_depth = 0  # 当前的第几层

        level_map[root] = 1

        while not stack.empty():
            node = stack.get()
            print(node.data, end=' ')

            if node.right is not None:
                stack.put(node.right)
                level_map[node.right] = level_map[node] + 1
            if node.left is not None:
                stack.put(node.left)
                level_map[node.left] = level_map[node] + 1

            if level_map[node] > cur_depth:
                cur_depth += 1
        print()

        return cur_depth


if __name__ == '__main__':
    node_list = [17, 5, 35, 2, 11, 29, 38, 10, 12]
    bst_tree = BST(node_list)
    depth = DFS.depth_first_search(bst_tree.get_root())
    print(depth)