6.3Binary Tree with Linked Nodes
Inorder traversal
An inorder traversal is made of the same three lines as preorder; only the print line moves between the two calls. A node is printed after its left subtree is done and before the right subtree starts: left, root, right. This small change has a big result: in a binary search tree inorder prints the values in sorted order.
- What inorder means: left subtree, root, right subtree
- Why a node is printed only after its left subtree is finished
- Why inorder gives sorted output on a binary search tree
1Idea
When the function reaches a node it does not print it right away. It first walks the whole left subtree; only when that call returns does it print the node and move to the right subtree. That is why the first value printed is not the root but the node reached by always going left from the root: 40 in the sample tree.
On the sample tree the inorder output is 40 20 50 10 60 30. If you look at the drawing from left to right, inorder reads the nodes in that order: every node sits to the right of its left subtree and to the left of its right subtree. The base case is again the empty subtree.
In a binary search tree this left-to-right reading means sorted output, because there every value in the left subtree is smaller than the node and every value on the right is larger. That is why inorder is used to check that a BST is built correctly and to print its values in order. On an expression tree, inorder gives the familiar infix form.
The same traversal in an array tree is the same three lines; the children sit at indexes 2*i+1 and 2*i+2.
2Step by step
The drawing walks the sample tree in inorder. The orange node belongs to the current call; the green ones have been printed. If a node is orange but not yet green, its call is waiting for the left subtree to finish.
- Start. The output is empty.
- The first call starts at the root, but 10 is not printed yet. The left subtree comes first:
inorder(20)is called. - 20 waits too; its left subtree has to be walked first:
inorder(40)is called. - The left of 40 is NULL and that call returns at once. Now it is 40's turn: 40 is printed. Its right is NULL too. Output: 40.
- Back in 20. Its left subtree is done, so 20 is printed. Output: 40 20.
- The right subtree of 20: both children of 50 are NULL, so 50 is printed. Output: 40 20 50.
- Back in 10. Its whole left subtree is done; 10 is printed. Output: 40 20 50 10.
- Now the right subtree of 10.
inorder(30)is called; 30 first waits for its own left side. - The left of 30 is 60: both its children are NULL, so 60 is printed. Output: 40 20 50 10 60.
- Back in 30, and 30 is printed. Its right is NULL. Output: 40 20 50 10 60 30.
- All calls are finished. The inorder output is 40 20 50 10 60 30.
struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right;}; void inorder(struct TreeNode* root) { if (root == NULL) return; // empty subtree: base case inorder(root->left); // 1. left subtree printf("%d ", root->data); // 2. root inorder(root->right); // 3. right subtree}class TreeNode { int data; TreeNode left, right; TreeNode(int data) { this.data = data; }} void inorder(TreeNode root) { if (root == null) return; // empty subtree: base case inorder(root.left); // 1. left subtree System.out.print(root.data + " "); // 2. root inorder(root.right); // 3. right subtree}class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def inorder(root): if root is None: # empty subtree: base case return inorder(root.left) # 1. left subtree print(root.data, end=" ") # 2. root inorder(root.right) # 3. right subtreeThe 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.
In a balanced tree h is about log n; in a tree whose nodes all sit on one branch it is n. An inorder without recursion has to keep the stack by hand; doing that with a stack is a common exam question.
4Common mistake
A common mistake is thinking of inorder as "root first" and expecting the root to be printed first. In code the mistake is usually the order of the lines:
void inorder(struct TreeNode* root) { if (root == NULL) return; inorder(root->left); inorder(root->right); printf("%d ", root->data); // in the wrong place}This function prints postorder, not inorder: 40 50 20 60 30 10. The easiest way to tell the three traversals apart is where the print line sits: before the two calls for preorder, between them for inorder, after them for postorder.
5Try it
A tree has root 1, left child 2, right child 3, and 4 is the left child of 2. What is its inorder output?
The answer is B. Go left from the root: 4 is printed first, then 2. 2 has no right child; back in 1, 1 is printed, and the right subtree, 3, comes last.
Show the answer
The answer is B. Go left from the root: 4 is printed first, then 2. 2 has no right child; back in 1, 1 is printed, and the right subtree, 3, comes last.
What does the inorder output of a binary search tree look like?
The answer is B. In a BST the left subtree holds smaller values and the right subtree larger ones. Inorder prints the left, then the node, then the right, so the values come out sorted.
Show the answer
The answer is B. In a BST the left subtree holds smaller values and the right subtree larger ones. Inorder prints the left, then the node, then the right, so the values come out sorted.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.