Inorder Traversal of a Binary Search Tree

Inorder traversal visits the left subtree, then the node, then the right subtree. Under the search-tree rule, keys appear in sorted order.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Use one small arrangement
  2. Contrast the responsibilities of other visits
  3. Show the middle visit on a branching example
  4. Test a zero-child and a one-child node

Use one small arrangement

With root five, left node two, and right node nine, the sequence is 2, 5, 9. Search-tree ordering establishes why the same ordering holds if each child is a full subtree.

Contrast the responsibilities of other visits

A layout routine may need the parent before its children, even for this same tree. Traversal choice depends on which information is needed at emission time. See preorder and postorder for parent-first and child-first conventions.[1]

Show the middle visit on a branching example

For root seven, left child three with right descendant five, and right child nine, inorder visits the left subtree before seven. It visits three before five because three has no left child. Under binary search tree ordering, the full sequence is sorted. See binary search tree order.

Tree edges: 7 left→3, 7 right→9, 3 right→5
Inorder: 3, 5, 7, 9
Preorder: 7, 3, 5, 9

Test a zero-child and a one-child node

Starting with a root-only tree gives one output. A node with a missing left child still needs to emit its own key before its right branch. Inorder is a traversal definition for any binary tree, while sorted output requires the search ordering invariant.

References

  1. Jeff Erickson, Algorithms ↩