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.
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]
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
. With this in mind you can create a tree with values of None
simply with:
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.