Concept
A tree is a non-linear data structure. Compared with a linear data structure (linked list, array), a tree consists of nodes, the average running time of the tree is shorter (the complexity of the sort time related to the tree is usually not high). Generally, the tree has many branches, and there are many branches under the branch, it will be very troublesome to study in the program. Because the tree is non-linear, and the memory of our computer is linearly stored, we cannot design it if it is too complex. Therefore, let's first look at the simple and frequently used ---> binary tree.
Binary Tree
A binary tree means that each node cannot have more than two sons.
Tree Creation
A tree is composed of nodes. Therefore, when we define a tree, we usually define a node, which is connected to a node. The Node definition is: one data and two pointers (if a node points to a node or no node points to null ).
As mentioned above, a tree is composed of several nodes. When nodes are connected, the tree is composed of one data and two pointers.
Therefore, creating a tree is actually creating a node and then connecting to the node.
Tree traversal
If we can traverse the tree like an array (view its data), it means the tree has been created ~
There are three methods for binary tree traversal:
First-order traversal
First access the root node, then access the left node, and then access the right node (root> left> right)
Sequential Traversal
First access the left node, then access the root node, and then access the right node (left-> root-> right)
Post-order traversal
First access the left node, then access the right node, and finally access the root node (left-> right-> root)
An example of traversing a binary tree:
The preceding binary tree is used as an example:
For first-order traversal: 10-> 9-> 20-> 15-> 35
If the traversal is in the middle order: 9-> 10-> 15-> 20-> 35
It may be necessary to explain that after accessing 10 nodes, 20 nodes are found, but there are still child nodes under 20. Therefore, the first access is 20 left son 15 nodes. Because 15 nodes have no sons. Therefore, 20 nodes are returned and 20 nodes are accessed. Last accessed 35 nodes
For post-order traversal: 9-> 15-> 35-> 20-> 10
It may be necessary to explain: first access 9 nodes, then access 20 nodes, but there are still child nodes under 20, so the first access is 20 left son 15 nodes. Because 15 nodes have no sons. So I went to access 35 nodes. Because 35 nodes have no sons, 20 nodes are returned and 10 nodes are finally returned.
To sum up one sentence: First (root-> left-> right), middle (left-> root-> right), and then (left-> right-> root ). If you access a node with a child, process the child first and then return
Regardless of the first and second traversal, The traversal of each node. If a child node is accessed, the child node is processed first (the logic is the same)
So we can easily think of recursion.
Interestingly, we can restore the original binary tree by means of the first and middle or middle order and the latter, but the original binary tree cannot be restored through the first order and the latter.
That is to say, we can determine a binary tree through the middle and first or middle and last orders!
Data Structure-tree