6.2Binary Tree with an Array
Inorder traversal
In an inorder traversal you visit the left subtree first, then the node itself, then the right subtree. The code is the same as preorder; only the print line moves between the two calls. That small change turns the output around completely.
- The inorder order, left, root, right, and how to find a tree's inorder output by hand
- Why the first node printed is the leftmost node
- Why inorder gives sorted output on a binary search tree
1Idea
inorder(i) first calls itself for the left child's index 2*i+1. That call also goes down to its own left child first. So the traversal goes all the way down to the leftmost node without printing anything; that is the first value printed. In the example tree it is 40.
A node is printed only after its whole left subtree has been printed. Then the traversal moves to the right subtree. The inorder output of the example tree is 40 20 50 10 60 30: the root 10 comes after the three nodes on its left and before the two on its right.
An easy way to find it by hand is to imagine light falling on the tree from above: reading the nodes from left to right, without moving them, gives the inorder order. This always works if the tree is drawn neatly.
Inorder really matters for a binary search tree. There everything left of a node is smaller and everything right of it is larger, so inorder gives the values from smallest to largest.
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.inorder(0)is about to be called, but the root will not be printed right away. inorder(7): we went left all the way, from 0 to 1 to 3. 7 ≥ 6, there is no node at this index. This is the base case: the call returns without printing. So 40 has no left child.inorder(3): the left child index 7 is empty, so it is this node's turn at once: 40. Output:40. The right child index 8 is empty too, so that call returns at once.inorder(1): the left subtree is done, now it is this node's turn: 20. Output:40 20. Now we move to the right child, index 4.inorder(4): the left child index 9 is empty, so it is this node's turn at once: 50. Output:40 20 50. The right child index 10 is empty too, so that call returns at once.inorder(0): the whole left subtree of the root,40 20 50, has been printed. Only now is the root printed: 10. Output:40 20 50 10. Then we move to the right child, index 2.inorder(5): the left child index 11 is empty, so it is this node's turn at once: 60. Output:40 20 50 10 60. The right child index 12 is empty too, so that call returns at once.inorder(2): the left subtree is done, now it is this node's turn: 30. Output:40 20 50 10 60 30. That was the last node.inorder(6): the right child of 30 would be at index2*2+2 = 6, but 6 ≥count. The cell is empty, so the call returns without printing.- Done. The inorder output is
40 20 50 10 60 30. The root 10 is right in the middle: its left subtree before it, its right subtree after it.
#define CAPACITY 15 struct Tree { int items[CAPACITY]; int count;}; void inorder(struct Tree* t, int i) { if (i >= t->count) { return; // empty place: no node } inorder(t, 2 * i + 1); // the left subtree first printf("%d ", t->items[i]); // then the root inorder(t, 2 * i + 2); // the right subtree last}class ArrayTree { int[] items; int count = 0; ArrayTree(int capacity) { items = new int[capacity]; } void inorder(int i) { if (i >= count) { return; // empty place: no node } inorder(2 * i + 1); // the left subtree first System.out.print(items[i] + " "); // then the root inorder(2 * i + 2); // the right subtree last }}class ArrayTree: def __init__(self, capacity): self.items = [None] * capacity self.count = 0 def inorder(self, i=0): if i >= self.count: return # empty place: no node self.inorder(2 * i + 1) # the left subtree first print(self.items[i], end=" ") # then the root self.inorder(2 * i + 2) # the right subtree 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. The tree in an array is complete, so that is about log n.
All three traversals take the same time and memory. The only difference between them is when a node is printed.
4Common mistake
A common mistake is putting the root first in the inorder output. In inorder the root does not come first; it comes after its left subtree:
# Wrong: 10 40 20 50 60 30 (root first)# Right: 40 20 50 10 60 30Another mistake is believing that inorder gives sorted output on any tree. That is true only for a binary search tree. The example tree is not a search tree, and its output is not sorted.
5Try it
Let the array [5, 3, 8, 1, 4] hold a binary tree in level order (count = 5). What is the inorder output?
The answer is A. In the left subtree, 3 has the children 1 and 4. Inorder: 1 first, then 3, then 4; then the root 5, and 8 on the right last. The output is 1 3 4 5 8.
Show the answer
The answer is A. In the left subtree, 3 has the children 1 and 4. Inorder: 1 first, then 3, then 4; then the root 5, and 8 on the right last. The output is 1 3 4 5 8.
In an inorder traversal, which value is printed first?
The answer is B. Every call goes left before printing. Printing starts at the first node with no left child, and that node is the leftmost one.
Show the answer
The answer is B. Every call goes left before printing. Printing starts at the first node with no left child, and that node is the leftmost one.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.