Traverse Binary Trees
A binary tree is a non-linear data structure. The so-called traversal binary tree is to access each node in the binary tree in a certain order, requiring each node to be accessed once and only once.
The traversal operation is actually a process of linearity of the non-linear structure, and the result is a linear sequence.
Binary Tree operations
(1) first-order traversal-the final condition is whether the binary tree is empty.
First, access the root node;
Then, access the left subtree in sequence;
Then, access the right subtree in sequence.
(2) sequential traversal-the condition for ending is whether the binary tree is empty LTr.
Traverse the left subtree in the middle order;
Then access the root node;
Traverse the right subtree in the middle order.
(3) Post-order traversal-the condition for ending is whether the binary tree is empty LRT.
Traverse the left subtree sequentially;
Traverse the right subtree in the descending order;
Then, access the root node in sequence.
(4) Two traversal sequences are known to be used to obtain the original binary tree.
1. If the first, middle, middle, and back orders are known, the original binary tree can be introduced.
2. If the first and last orders are known, the original binary tree cannot be obtained.
Steps:
First, find the root node;
Determine the left and right subtree Based on the root node.
Example 1:
Preface: abcdefgh
Central order: bdceafhg
After order?
Descending order: decbhgfa
Example 2:
Preface: abdghcefi
Middle Order: gdhbaecif
After order?
Post-order: ghdbeifca
Example 3:
Central order: bdceafhg
Descending order: decbhgfa
What is the first order?
Preface: abcdefgh
Data Structure Learning notes (4) --- traversing Binary Trees