← Course home

Lookup and priority · Lesson 30 of 52

Tree traversals

Compare depth-first and level-order traversals and predict their visit orders.

Full lessonAbout 10 min

You will learn to predict preorder, inorder and postorder sequences and trace level-order traversal with a queue.

Recommended first: Binary search tree operations, Queue and deque operations.

On this page 6 sections

The idea

Preorder of the tree built from 4, 2, 6, 1 is 4, 2, 1, 6.

A traversal is a rule for the order in which you visit nodes. The same tree produces different sequences under different rules. Build the binary search tree by inserting 4, then 2, then 6, then 1. preorder visits a node before either child and returns 4, 2, 1, 6. postorder visits both children before the node and returns 1, 2, 6, 4. The inorder walk, left then node then right, returns 1, 2, 4, 6. That sorted order appears only because the tree is a search tree. level_order uses a queue. It returns 4, 2, 6, 1. The root leaves first. Its children are enqueued left then right, so 2 is ahead of 6, and 1 waits behind them. Depth-first walks use a stack, or the call stack. Breadth-first walks use a queue. The container is the difference.

A worked trace

StepStateWhat changed
Preorderfirst = 4Preorder visits 4, then 2, then 1, then 6.
Levelqueue = 4Level order is 4, then 2, then 6, then 1.
Childnode = 11 hangs under 2, so it waits behind 6.

Why it is correct

Preorder is correct by induction on the tree. The first value is the root. The next values are the preorder of the left subtree, then the preorder of the right subtree. Postorder appends the root after both recursive sequences. Level order is the queue lesson again. A node is enqueued when its parent is visited. Every node at depth d enters the queue before any node at depth d + 1 is dequeued. Marking is unnecessary on a tree because there is one path from the root and no edge back to an ancestor.

A common mistake

Inorder is the sorted order. It is not preorder, and it is not level order.

What it costs

Each walk visits every node once and looks at every child link once, so the time is linear in the number of nodes. The call stack of a skewed preorder is linear. A balanced tree keeps that stack logarithmic. The level-order queue holds one level at a time. Can hold about half the nodes of a wide level, so extra memory is linear in the worst case. Use inorder when you want the sorted keys. Use level order when the depth itself is the question. Each walk is Θ(n) time, and the queue can use Θ(n) extra memory.

Run it

python
from bitsandbytes.trees.bst import BinarySearchTreefrom bitsandbytes.trees.traversals import preordertree = BinarySearchTree()for value in (4, 2, 6, 1):    tree.insert(value)print(preorder(tree.root))# prints: [4, 2, 1, 6] 

Practice and next step

Open Balanced search trees next. A balanced tree keeps the height logarithmic when keys arrive already sorted.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.trees.traversals

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Preorder and level order of one tree

Insert 4, 2, 6, 1. Preorder is 4, 2, 1, 6. Level order is 4, 2, 6, 1.

first
4

State

  1. 04chosen
  2. 12chosen
  3. 21chosen
  4. 36chosen

Preorder visits 4, then 2, then 1, then 6.

Practice feedback is loading. The lesson explanation remains available above.