简体   繁体   English

输入空二叉树的值

[英]Entering in Values for an Empty Binary Tree

I'm creating an empty complete binary tree of a certain height in an array implementation, where all the nodes currently hold None as its value.我正在数组实现中创建一个特定高度的空完整二叉树,其中所有节点当前都将 None 作为其值。

How would I enter in a list of values so that when printing in postorder traversal, the list of values would be reversed?我将如何输入值列表,以便在后序遍历中打印时,值列表将被反转?

Example:例子:

create_empty_bt(height=3) # total nodes = 2^h - 1
values = [7, 6, 5, 4, 3, 2, 1]
index = 0

enter_values(index, values, root)
print_tree_postorder(root, lst)
[1, 2, 3, 4, 5, 6, 7] # output

My code so far will print: [None, None, None, None, 5, 6, 7]到目前为止,我的代码将打印: [None, None, None, None, 5, 6, 7]

class Node:
    def __init__(self, v):
        self.value = v
        self.left = None
        self.right = None


def create_empty_bt(height): # create complete binary tree
    nodes = [i for i in range(2 ** height - 1, 0, -1)]
    for i in range(height):
        start = (2 ** i) - 1
        end = (2 ** (i + 1)) - 1
        for j in range(start, end):
            nodes[j] = Node(None)
            if j == 0:
                continue
            if (j % 2) == 0:
                parent = (j - 2) // 2 # right child node
                nodes[parent].right = nodes[j]
            else:
                parent = (j - 1) // 2 # left child node
                nodes[parent].left = nodes[j]
    return nodes[0]

# Postorder = L R V, Reverse = V R L
def enter_values(index, values, root):
    if root:
        if root.value is None:
            root.value = values[index]
            enter_values(index + 1, values, root.right)
        else:
            enter_values(index + 1, values, root.left)

def print_tree(node, lst): # postorder
    if node:
        print_tree(node.left, lst)
        print_tree(node.right, lst)
        lst.append(node.value)

Thanks in advance!提前致谢!

You are making this a little harder than you need to.你让这比你需要的更难。 Consider that a balanced tree of height h is tree with two children of height h - 1 .考虑高度为h的平衡树是具有两个高度为h - 1孩子的树。 With this in mind you can create a tree with values of None simply with:考虑到这一点,您可以简单地创建一个值为None的树:

class Node:
    def __init__(self, v):
        self.value = v
        self.left = None
        self.right = None

def create_empty_bt(height):
    if height == 0:
        return None
    n = Node(None)
    n.left = create_empty_bt(height - 1) 
    n.right = create_empty_bt(height -1)
    return n

To perform a post-order traversal, you just call the function on the children in the correct order:要执行后序遍历,您只需以正确的顺序调用子项上的函数:

def po_traverse(node):
    if node is None:
        return
    yield from po_traverse(node.left) 
    yield from po_traverse(node.right) 
    yield node.value

This returns a generator, to get a list just ask:这将返回一个生成器,要获取列表,只需询问:

list(po_traverse(node))

To insert, you use the same technique and set the node's value to the last element of the list as the recursion unwinds:要插入,您可以使用相同的技术,并在递归展开时将节点的值设置为列表的最后一个元素:

def insertList(node, l):
    if node is None: 
        return 
    insertList(node.left, l)
    insertList(node.right, l)
    node.value = l.pop()

Note: this consumes the list.注意:这会消耗列表。 You can avoid that by copying it or by rewriting the function such that it returns a slice of the list as it unwinds — but I though this showed the structure more clearly.您可以通过复制它或重写函数来避免这种情况,以便它在展开时返回列表的一部分——但我虽然这更清楚地显示了结构。

Using this gives you:使用它可以为您提供:

> t = create_empty_bt(height=3) # total nodes = 2^h - 1
> list(po_traverse(t))
[None, None, None, None, None, None, None]

> insertList(t, [7, 6, 5, 4, 3, 2, 1])
> list(po_traverse(t))
[1, 2, 3, 4, 5, 6, 7]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM