§ 02 — Data Structures and Algorithms
Tree traversal is the process of visiting every node in a tree exactly once. Traversal is important when printing, searching, evaluating, or copying tree structures.
For binary trees, the common traversal methods are:
| Traversal | Order |
|---|---|
| Preorder | Visit root, then left subtree, then right subtree. |
| Inorder | Visit left subtree, then root, then right subtree. |
| Postorder | Visit left subtree, then right subtree, then root. |
| Level order | Visit nodes level by level from left to right. |
Preorder visits the root before its subtrees.
Root -> Left -> Right
This is useful when copying a tree or displaying a prefix expression.
Inorder visits the left subtree first, then the root, then the right subtree.
Left -> Root -> Right
For a binary search tree, inorder traversal displays values in sorted order.
Postorder visits both subtrees before the root.
Left -> Right -> Root
This is useful when deleting a tree or evaluating postfix expressions.
Level-order traversal visits nodes from top to bottom, moving left to right at each level. It is commonly implemented using a queue.
02 — Activity 11: Tree Traversal PracticePro reviewer — not included in this printout.
ProReviewer — locked
Part of this subject's paid reviewers.
Done with this module? Track it — your progress shows on the subject list.
Studying with blockmates? Send them this lesson.
Up next
Lesson 9: Selection Sort→←Previous: Lesson 7: Tree Data Structure