ArticleslgStudy

computer science

Tree traversal

Tree traversal is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tree traversal rather than just read about it. In short: In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure exactly once. Such traversals are classified by the order in which the nodes are visited.

Tree traversal — main illustration
Tree traversal — illustration

Key takeaways

  • Tree traversal belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tree traversal to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tree traversal from memory before moving on to harder problems.

Reference excerpt

In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure exactly once. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well.

Types

Unlike linked lists, one-dimensional arrays and other linear data structures, which are canonically traversed in linear order, trees may be traversed in multiple ways. They may be traversed in depth-first or breadth-first order. There are three common ways to traverse them in depth-first order: in-order, pre-order and post-order. Beyond these basic traversals, various more complex or hybrid schemes are possible, such as depth-limited searches like iterative deepening depth-first search. The latter, as well as breadth-first search, can also be used to traverse infinite trees, see below.

Data structures for tree traversal Traversing a tree involves iterating over all nodes in some manner. Because from a given node there is more than one possible next node (it is not a linear data structure), then, assuming sequential computation (not parallel), some nodes must be deferred—stored in some way for later visiting. This is often done via a stack (LIFO) or queue (FIFO). As a tree is a self-referential (recursively defined) data structure, traversal can be defined by recursion or, more subtly, corecursion, in a natural and clear fashion; in these cases the deferred nodes are stored implicitly in the call stack. Depth-first search is easily implemented via a stack, including recursively (via the call stack), while breadth-first search is easily implemented via a queue, including corecursively.

Depth-first search

In depth-first search (DFS), the search tree is deepened as much as possible before going to the next sibling. To traverse binary trees with depth-first search, the following operations are performed at each node:

If the current node is empty then return. Execute the following three operations in a certain order: N: Visit the current node. L: Recursively traverse the current node's left subtree. R: Recursively traverse the current node's right subtree. The trace of a traversal is called a sequentialisation of the tree. The traversal trace is a list of each visited node. No one sequentialisation according to pre-, in- or post-order describes the underlying tree uniquely. Given a tree with distinct elements, either pre-order or post-order paired with in-order is sufficient to describe the tree uniquely. However, pre-order with post-order leaves some ambiguity in the tree structure. There are three methods at which position of the traversal relative to the node (in the figure: red, green, or blue) the visit of the node shall take place. The choice of exactly one color determines exactly one visit of a node as described below. Visit at all three colors results in a threefold visit of the same node yielding the “all-order” sequentialisation:

F-B-A-A-A-B-D-C-C-C-D-E-E-E-D-B-F-G-G- I-H-H-H- I- I-G-F

Pre-order, NLR Visit the current node (in the figure: position red). Recursively traverse the current node's left subtree. Recursively traverse the current node's right subtree. The pre-order traversal is a topologically sorted one, because a parent node is processed before any of its child nodes is done.

Post-order, LRN Recursively traverse the current node's left subtree. Recursively traverse the current node's right subtree. Visit the current node (in the figure: position blue). Post-order traversal can be useful to get postfix expression of a binary expression tree.

In-order, LNR Recursively traverse the current node's left subtree. Visit the current node (in the figure: position green). Recursively traverse the current node's right subtree. In a binary search tree ordered such that in each node the key is greater than all keys in its left subtree and less than all keys in its right subtree, in-order traversal retrieves the keys in ascending sorted order.

Reverse pre-order, NRL Visit the current node. Recursively traverse the current node's right subtree. Recursively traverse the current node's left subtree.

Reverse post-order, RLN Recursively traverse the current node's right subtree. Recursively traverse the current node's left subtree. Visit the current node.

Reverse in-order, RNL Recursively traverse the current node's right subtree. Visit the current node. Recursively traverse the current node's left subtree. In a binary search tree ordered such that in each node the key is greater than all keys in its left subtree and less than all keys in its right subtree, reverse in-order traversal retrieves the keys in descending sorted order.

Arbitrary trees To traverse arbitrary trees (not necessarily binary trees) with depth-first search, the following operations are performed at each node:

If the current node is empty then return. Visit the current node for pre-order traversal. For each i from 1 to the current node's number of subtrees − 1, or from the latter to the former for reverse traversal, do: Recursively traverse the current node's i-th subtree. Visit the current node for in-order traversal. Recursively traverse the current node's last subtree. Visit the current node for post-order traversal. Depending on the problem at hand, pre-order, post-order, and especially one of the number of subtrees − 1 in-order operations may be optional. Also, in practice more than one of pre-order, post-order, and in-order operations may be required. For example, when inserting into a ternary tree, a pre-order operation is performed by comparing items. A post-order operation may be needed afterwards to re-balance the tree.

Breadth-first search

In breadth-first search (BFS) or level-order search, the search tree is broadened as much as possible before going to the next depth.

Other types There are also tree traversal algorithms that classify as neither depth-first search nor breadth-first search. One such algorithm is Monte Carlo tree search, which concentrates on analyzing the most promising moves, basing the expansion of the search tree on random sampling of the search space.

Applications

… excerpt ends here. Continue reading the full article.

Illustrations

Tree traversal: Level-order: F, B, G, A, D, I, C, E, H.
Level-order: F, B, G, A, D, I, C, E, H.
Tree traversal: Tree representing the arithmetic expression: A * (B − C) + (D + E)
Tree representing the arithmetic expression: A * (B − C) + (D + E)

Worked examples

Example 1 — a first encounter with Tree traversal

Start with the simplest possible case. Write down what Tree traversal claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tree traversal before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tree traversal ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tree traversal

In research
Tree traversal appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tree traversal in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tree traversal is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, Iteration in programming, Recursion, so understanding it makes those chapters shorter.
In everyday life
Look for Tree traversal outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Tree traversal” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Tree traversal in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tree traversal means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tree traversal out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tree traversal in simple terms?

In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure exactly once. Such traversals are classified by the order in which the…

Why does Tree traversal matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tree traversal?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tree traversal.

Tags

  • Graph algorithms
  • Iteration in programming
  • Recursion
  • Trees (data structures)

Keep exploring