4.2 KiB
Binary tree traversal
From a physical structure perspective, a tree is a data structure based on linked lists. Hence, its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms.
The common traversal methods for binary trees include level-order traversal, pre-order traversal, in-order traversal, and post-order traversal.
Level-order traversal
As shown in the figure below, level-order traversal traverses the binary tree from top to bottom, layer by layer. Within each level, it visits nodes from left to right.
Level-order traversal is essentially a type of breadth-first traversal, also known as breadth-first search (BFS), which embodies a "circumferentially outward expanding" layer-by-layer traversal method.
Code implementation
Breadth-first traversal is usually implemented with the help of a "queue". The queue follows the "first in, first out" rule, while breadth-first traversal follows the "layer-by-layer progression" rule, the underlying ideas of the two are consistent. The implementation code is as follows:
[file]{binary_tree_bfs}-[class]{}-[func]{level_order}
Complexity analysis
- Time complexity is
O(n)
: All nodes are visited once, takingO(n)
time, wheren
is the number of nodes. - Space complexity is
O(n)
: In the worst case, i.e., a full binary tree, before traversing to the bottom level, the queue can contain at most(n + 1) / 2
nodes simultaneously, occupyingO(n)
space.
Preorder, in-order, and post-order traversal
Correspondingly, pre-order, in-order, and post-order traversal all belong to depth-first traversal, also known as depth-first search (DFS), which embodies a "proceed to the end first, then backtrack and continue" traversal method.
The figure below shows the working principle of performing a depth-first traversal on a binary tree. Depth-first traversal is like "walking" around the entire binary tree, encountering three positions at each node, corresponding to pre-order, in-order, and post-order traversal.
Code implementation
Depth-first search is usually implemented based on recursion:
[file]{binary_tree_dfs}-[class]{}-[func]{post_order}
!!! tip
Depth-first search can also be implemented based on iteration, interested readers can study this on their own.
The figure below shows the recursive process of pre-order traversal of a binary tree, which can be divided into two opposite parts: "recursion" and "return".
- "Recursion" means starting a new method, the program accesses the next node in this process.
- "Return" means the function returns, indicating the current node has been fully accessed.
Complexity analysis
- Time complexity is
O(n)
: All nodes are visited once, usingO(n)
time. - Space complexity is
O(n)
: In the worst case, i.e., the tree degenerates into a linked list, the recursion depth reachesn
, the system occupiesO(n)
stack frame space.