6.2Binary Tree with an Array
Postorder traversal
In a postorder traversal you visit the left subtree first, then the right subtree, and the node itself last. The code is the same as preorder and inorder; this time the print line moves after both calls.
- The postorder order, left, right, root, and how to find a tree's postorder output by hand
- Why the root always comes out last in postorder
- Where the "children first, then the parent" order is useful
1Idea
postorder(i) calls itself first for the left child's index 2*i+1, then for the right child's index 2*i+2, and prints items[i] only when both are done. A node is never printed before all the nodes below it.
So the first node printed is always a leaf and the last one is always the root. The postorder output of the example tree is 40 50 20 60 30 10: first the subtree of 20, then the subtree of 30, and the root last.
This order is used when a node's children must be finished before you work on the node. When freeing a linked tree, the children are freed before the parent. When evaluating an expression tree, both operands are found before the operator is applied. Finding the size of a folder is the same idea: the subfolders are added up first.
How to find it by hand: print a node when you leave it for the last time, after both of its subtrees are done. Preorder prints on the first visit, inorder when the left side is done, postorder on the last departure.
2Step by step
The drawing shows the tree on top and its array below. A visited node turns green, and the tree node gets its position in the output under it. The stack under the drawing shows the calls that have not finished.
- Start. The tree has 6 nodes,
countis 6.postorder(0)is about to be called; the root will be printed last. postorder(7): we went left all the way, from 0 to 1 to 3, and nothing has been printed yet. 7 ≥ 6, there is no node at this index. This is the base case: the call returns without printing.postorder(3): 40 is a leaf, and both child calls returned at once. Now it is printed: 40. Output:40.postorder(4): 50 is a leaf, and both child calls returned at once. Now it is printed: 50. Output:40 50.postorder(1): both the left and the right subtree have been printed. The node is printed last: 20. Output:40 50 20.postorder(5): 60 is a leaf, and both child calls returned at once. Now it is printed: 60. Output:40 50 20 60.postorder(6): the left subtree of 30, 60, has been printed. Its right child would be at index2*2+2 = 6, but 6 ≥count. The cell is empty, so the call returns without printing.postorder(2): the left subtree has been printed and there is no right child. The node is printed last: 30. Output:40 50 20 60 30.postorder(0): both the left and the right subtree have been printed. The node is printed last: 10. Output:40 50 20 60 30 10. In postorder the root always comes out last.- Done. The postorder output is
40 50 20 60 30 10. The first node printed is a leaf, the last one is the root.
#define CAPACITY 15 struct Tree { int items[CAPACITY]; int count;}; void postorder(struct Tree* t, int i) { if (i >= t->count) { return; // empty place: no node } postorder(t, 2 * i + 1); // the left subtree first postorder(t, 2 * i + 2); // then the right subtree printf("%d ", t->items[i]); // the root last}class ArrayTree { int[] items; int count = 0; ArrayTree(int capacity) { items = new int[capacity]; } void postorder(int i) { if (i >= count) { return; // empty place: no node } postorder(2 * i + 1); // the left subtree first postorder(2 * i + 2); // then the right subtree System.out.print(items[i] + " "); // the root last }}class ArrayTree: def __init__(self, capacity): self.items = [None] * capacity self.count = 0 def postorder(self, i=0): if i >= self.count: return # empty place: no node self.postorder(2 * i + 1) # the left subtree first self.postorder(2 * i + 2) # then the right subtree print(self.items[i], end=" ") # the root lastThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each node is printed once, and the calls for missing children are bounded by the number of nodes.
The call stack gets as deep as the tree's height; for the complete tree in an array that is about log n.
Preorder, inorder and postorder make the same number of calls. They take the same time and the same memory.
4Common mistake
A common mistake is thinking postorder is preorder reversed:
# preorder: 10 20 40 50 30 60# reversed: 60 30 50 40 20 10 (not postorder)# postorder (right): 40 50 20 60 30 10Reversed preorder is the reverse of root, left, right, so the right subtree comes out before the left one. In postorder the left subtree always comes before the right one. The two agree only on where the root goes: last.
5Try it
Let the array [5, 3, 8, 1, 4] hold a binary tree in level order (count = 5). What is the postorder output?
The answer is A. The left subtree first: the children of 3, 1 and 4, are printed, then 3. Then the right subtree: 8. The root last: 5. The output is 1 4 3 8 5.
Show the answer
The answer is A. The left subtree first: the children of 3, 1 and 4, are printed, then 3. Then the right subtree: 8. The root last: 5. The output is 1 4 3 8 5.
Which traversal is used to free every node of a linked tree?
The answer is C. Once a node is freed, the pointers to its children are lost. So both subtrees are deleted first and the node itself last: that is the postorder order.
Show the answer
The answer is C. Once a node is freed, the pointers to its children are lost. So both subtrees are deleted first and the node itself last: that is the postorder order.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.