6.3Binary Tree with Linked Nodes
Postorder traversal
In a postorder traversal a node is printed after both of its subtrees are done: left, right, root. The code is again the same three lines as preorder; the print line moves after the two calls. This order is made for jobs that need "children first, then the parent": freeing a tree, working out the height of a subtree, evaluating an expression tree.
- What postorder means: left subtree, right subtree, root
- Why the root is always printed last
- Why a tree is freed in postorder
1Idea
When the function reaches a node it first walks the whole left subtree, then the whole right subtree. The node is printed only after both calls have returned. That is why the first value printed is always a leaf and the last one is the root.
On the sample tree the postorder output is 40 50 20 60 30 10. 20 comes after its own children 40 and 50; 10 is last. When a node is printed, everything below it has already been handled. The base case is again the empty subtree.
In C this property is a must when freeing a tree. Reading the left and right fields of a node after it has been freed is not allowed; the children have to be freed first, then the node. In other words, a postorder with free(root) in place of the printf line:
void freeTree(struct TreeNode* root) { if (root == NULL) return; freeTree(root->left); freeTree(root->right); free(root); // the children are already freed}The same idea gives the height and node count of a subtree and evaluates an expression tree: an operator is applied after the values of both its operands are known. The postorder output is also the postfix form of the expression. The same traversal is written in an array tree with the index formulas.
2Step by step
The drawing walks the sample tree in postorder. The orange node belongs to the current call; the green ones have been printed. A node that is orange but not green is waiting for its subtrees to finish.
- Start. The output is empty.
- The first call starts at the root. 10 will be printed last; the left subtree comes first:
postorder(20)is called. - 20 waits too. Its left subtree comes first:
postorder(40)is called. - Both children of 40 are NULL; both calls return at once and 40 is printed. Output: 40.
- Back in 20; now the right subtree. 50 has no children, so 50 is printed. Output: 40 50.
- Both subtrees of 20 are done: now 20 is printed. Output: 40 50 20.
- Back in 10; now the right subtree.
postorder(30)is called, and 30 waits too. - The left of 30 is 60: no children, so 60 is printed. Output: 40 50 20 60.
- The right of 30 is NULL and that call returns at once. 30 is printed. Output: 40 50 20 60 30.
- Both subtrees of 10 are done: the root is printed last. Output: 40 50 20 60 30 10.
- All calls are finished. The postorder output is 40 50 20 60 30 10.
struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right;}; void postorder(struct TreeNode* root) { if (root == NULL) return; // empty subtree: base case postorder(root->left); // 1. left subtree postorder(root->right); // 2. right subtree printf("%d ", root->data); // 3. root}class TreeNode { int data; TreeNode left, right; TreeNode(int data) { this.data = data; }} void postorder(TreeNode root) { if (root == null) return; // empty subtree: base case postorder(root.left); // 1. left subtree postorder(root.right); // 2. right subtree System.out.print(root.data + " "); // 3. root}class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def postorder(root): if root is None: # empty subtree: base case return postorder(root.left) # 1. left subtree postorder(root.right) # 2. right subtree print(root.data, end=" ") # 3. rootThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Every node is printed once and each node makes a fixed number of calls.
The call stack grows as long as the path from the root to the deepest leaf: the height h of the tree.
All three traversals take the same time and memory. The only difference between them is when the node is printed.
4Common mistake
In C the most dangerous mistake is freeing a node before its children when deleting a tree:
void freeTree(struct TreeNode* root) { if (root == NULL) return; free(root); // the node first freeTree(root->left); // reads freed memory freeTree(root->right);}Reading the fields of a freed node is undefined behaviour. The program sometimes seems to work and sometimes crashes. Deleting must be done in postorder, not preorder. When reading outputs, a common mistake is taking the first element of a postorder output for the root; in postorder the root is the last element.
5Try it
A tree has root 1, left child 2, right child 3, and 4 is the left child of 2. What is its postorder output?
The answer is A. First the whole left subtree: 4, then 2. Then the right subtree: 3. The root 1 is printed last.
Show the answer
The answer is A. First the whole left subtree: 4, then 2. Then the right subtree: 3. The root 1 is printed last.
What is the last element of a binary tree's postorder output?
The answer is B. In postorder a node is printed after both of its subtrees. The root's subtrees are the whole tree, so the root is printed last.
Show the answer
The answer is B. In postorder a node is printed after both of its subtrees. The root's subtrees are the whole tree, so the root is printed last.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.