Inorder traversal gives sorted order in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to traverse a binary search tree in order grows as the tree gets bigger.
Specifically, how does the number of steps change when we do an inorder traversal to get sorted values?
Analyze the time complexity of the following inorder traversal code.
function inorderTraversal(node) {
if (node == null) return;
inorderTraversal(node.left);
visit(node.value); // process current node
inorderTraversal(node.right);
}
This code visits each node in a binary search tree in ascending order.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls visiting each node once.
- How many times: Exactly once per node in the tree.
Each node is visited one time, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The work grows in a straight line as the tree size grows.
Time Complexity: O(n)
This means the time to complete the traversal grows directly with the number of nodes in the tree.
[X] Wrong: "Inorder traversal takes longer because it visits nodes multiple times."
[OK] Correct: Each node is visited exactly once during inorder traversal, so the time grows linearly, not more.
Knowing that inorder traversal visits each node once helps you explain how to get sorted data from a binary search tree efficiently.
"What if the tree is not a binary search tree but just any binary tree? How would the time complexity of inorder traversal change?"
Practice
inorder traversal on a binary search tree (BST)?Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits the left child, then the node itself, then the right child.Step 2: Apply inorder traversal to BST property
Since BST nodes have smaller values on the left and larger on the right, inorder traversal visits nodes in ascending order.Final Answer:
It visits nodes in ascending sorted order. -> Option DQuick Check:
Inorder traversal = ascending order [OK]
- Confusing inorder with preorder or postorder
- Thinking traversal visits nodes randomly
- Assuming it visits nodes in descending order
Solution
Step 1: Recall inorder traversal order
Inorder traversal means visiting the left subtree first, then the node, then the right subtree.Step 2: Match the correct sequence
Visit left child, then node, then right child matches this order exactly: left child, node, right child.Final Answer:
Visit left child, then node, then right child -> Option AQuick Check:
Inorder = left, node, right [OK]
- Mixing up preorder and inorder sequences
- Visiting node before left child
- Visiting right child before node
10
/ \
5 15
/ \ \
3 7 20Solution
Step 1: Perform inorder traversal on the BST
Visit left subtree (3, 5, 7), then root (10), then right subtree (15, 20).Step 2: Write nodes in visited order
The order is 3, 5, 7, 10, 15, 20.Final Answer:
[3, 5, 7, 10, 15, 20] -> Option CQuick Check:
Inorder traversal output = sorted ascending list [OK]
- Listing nodes in preorder or postorder instead
- Confusing left and right subtree order
- Forgetting to include all nodes
def inorder(node):
if node:
inorder(node.right)
print(node.value)
inorder(node.left)Solution
Step 1: Analyze the traversal order in code
The code visits right child first, then node, then left child, which is reverse of inorder.Step 2: Understand effect on BST traversal
This reversed order prints nodes in descending order, not ascending sorted order.Final Answer:
The function visits right child before left child, reversing order. -> Option AQuick Check:
Inorder must visit left before right [OK]
- Swapping left and right subtree calls
- Printing node value in wrong place
- Not checking for null node
[4, 2, 5, 1, 6]. Which of the following is true?Solution
Step 1: Check if inorder traversal sequence is sorted
The sequence [4, 2, 5, 1, 6] is not in ascending order.Step 2: Understand BST property and inorder traversal
In a BST, inorder traversal always produces a sorted ascending sequence. Since this is not sorted, the tree is not a BST.Final Answer:
The tree is not a BST because inorder traversal is not sorted. -> Option BQuick Check:
Inorder sorted = BST; not sorted = not BST [OK]
- Assuming inorder always sorts any tree
- Confusing balanced tree with BST
- Ignoring order of traversal output
