Пример #1
0
def build(startIndex, endIndex, currentRoot,
          time):  # 后序的index  time是因为后序每一层的中间在最后 所以每往右一层都要整体往左移1个
    inorderIndex = find(currentRoot.item)

    if endIndex - startIndex < 2:
        if endIndex - startIndex <= 0:
            return
        if inorder[inorderIndex + 1] == postorder[endIndex - 1]:
            currentRoot.setrightChild(Node(postorder[endIndex - 1]))
            return
        else:
            currentRoot.setLeftChild(Node(postorder[endIndex - 1]))
            return
    leftStart = startIndex
    leftEnd = inorderIndex - 1 - time  # endIndex-(endIndex-inorderIndex)-1
    # 括号代表leftLength 这括号里的endIndex本应是中序的endIndex 但中序后序子树长度一样 所以用后序的endIndex没问题
    rightStart = inorderIndex - time  # endIndex-(endIndex-inorderIndex)
    rightEnd = endIndex - 1

    leftChild = Node(postorder[leftEnd]) if leftStart <= leftEnd else None
    rightChild = Node(postorder[rightEnd]) if rightStart <= rightEnd else None
    currentRoot.setLeftChild(leftChild)
    currentRoot.setrightChild(rightChild)

    build(leftStart, leftEnd, leftChild, time) if leftChild != None else None
    build(rightStart, rightEnd, rightChild, time +
          1) if rightChild != None else None
Пример #2
0
 def test_tree_exotic(self):
     node_4_6 = Node(8)
     node_3_2 = Node(7)
     node_3_3 = Node(5, None, node_4_6)
     node_2_1 = Node(4, None, node_3_2)
     node_2_2 = Node(3, node_3_3, None)
     node_1_1 = Node(1, node_2_1, node_2_2)
     tree = Tree(node_1_1)
     answer = [[
         '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|',
         '|', '|'
     ],
               [
                   '|', '|', '|', '4', '|', '|', '|', '|', '|', '|', '|',
                   '3', '|', '|', '|'
               ],
               [
                   '|', '|', '|', '|', '|', '7', '|', '|', '|', '5', '|',
                   '|', '|', '|', '|'
               ],
               [
                   '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '8',
                   '|', '|', '|', '|'
               ]]
     assert tree.tree_print() == answer
Пример #3
0
 def test_tree_single(self):
     node_4_1 = Node(4)
     node_3_1 = Node(3, node_4_1, None)
     node_2_1 = Node(2, node_3_1, None)
     node_1_1 = Node(1, node_2_1, None)
     tree = Tree(node_1_1)
     answer = [[
         '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|',
         '|', '|'
     ],
               [
                   '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|',
                   '|', '|', '|', '|'
               ],
               [
                   '|', '3', '|', '|', '|', '|', '|', '|', '|', '|', '|',
                   '|', '|', '|', '|'
               ],
               [
                   '4', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|',
                   '|', '|', '|', '|'
               ]]
     assert tree.tree_print() == answer
Пример #4
0
 def test_tree_one_node(self):
     node_a = Node(10)
     tree = Tree(node_a)
     answer = [['10']]
     assert tree.tree_print() == answer
Пример #5
0
 def test_tree_full(self):
     node_4_1 = Node(8)
     node_4_2 = Node(9)
     node_4_3 = Node(10)
     node_4_4 = Node(11)
     node_4_5 = Node(12)
     node_4_6 = Node(13)
     node_4_7 = Node(14)
     node_4_8 = Node(15)
     node_3_1 = Node(4, node_4_1, node_4_2)
     node_3_2 = Node(5, node_4_3, node_4_4)
     node_3_3 = Node(6, node_4_5, node_4_6)
     node_3_4 = Node(7, node_4_7, node_4_8)
     node_2_1 = Node(2, node_3_1, node_3_2)
     node_2_2 = Node(3, node_3_3, node_3_4)
     node_1_1 = Node(1, node_2_1, node_2_2)
     tree = Tree(node_1_1)
     answer = [[
         '|', '|', '|', '|', '|', '|', '|', '1', '|', '|', '|', '|', '|',
         '|', '|'
     ],
               [
                   '|', '|', '|', '2', '|', '|', '|', '|', '|', '|', '|',
                   '3', '|', '|', '|'
               ],
               [
                   '|', '4', '|', '|', '|', '5', '|', '|', '|', '6', '|',
                   '|', '|', '7', '|'
               ],
               [
                   '8', '|', '9', '|', '10', '|', '11', '|', '12', '|',
                   '13', '|', '14', '|', '15'
               ]]
     assert tree.tree_print() == answer
Пример #6
0

def check(currentNode, layer):
    output[layer - 1] += (str(currentNode.item) + " ")
    if currentNode.leftChild != None:
        check(currentNode.leftChild, layer + 1)
    if currentNode.rightChild != None:
        check(currentNode.rightChild, layer + 1)


if __name__ == '__main__':
    record = []

    line1 = list(map(int, input().split()))
    n = line1[0]
    root = Node(line1[1])
    record.append(root)
    for i in range(n):
        line = list(map(int, input().split(" ")))
        father = None
        for node in record:
            if node.item == line[0]:
                father = node
        if father == None:
            father = Node(line[0])
            record.append(father)
        if line[1] != 0:
            lch = Node(line[1])
            father.setLeftChild(lch)
            record.append(lch)
        if line[2] != 0:
Пример #7
0
        leftSum = 1000000
        rightSum = 1000000
        if currentNode.leftChild != None:
            tuple = dfs(currentNode.leftChild,
                        sum + currentNode.leftChild.item)
            leftItem = tuple[0]
            leftSum = tuple[1]
        if currentNode.rightChild != None:
            tuple = dfs(currentNode.rightChild,
                        sum + currentNode.rightChild.item)
            rightItem = tuple[0]
            rightSum = tuple[1]
        if leftSum < rightSum:
            return leftItem, leftSum
        elif leftSum > rightSum:
            return rightItem, rightSum
        else:
            if leftItem < rightItem:
                return leftItem, leftSum
            else:
                return rightItem, rightSum


if __name__ == '__main__':
    inorder = list(map(int, input().split(" ")))  # 中序遍历
    postorder = list(map(int, input().split(" ")))  # 后序遍历
    root = Node(postorder[-1])
    build(0, len(postorder) - 1, root, 0)
    tree = Tree(root)
    print(dfs(root, 0)[0])