Binary tree traversal
There are three methods to traverse a binary tree:
(1) first traverse (DLR), first visit the root node, then traverse the left subtree, and finally traverse the right subtree. Note root-left-right.
(2) In the middle-order traversal (LDR), first traverse the left subtree, then access the root node, and finally traverse the right subtree. Note: Left-root-right.
(3) Post-sequential traversal (LRD): First traverse the left subtree, then traverse the right subtree, and finally access the root node. Note: Left-right-root.
Example 1: Binary Tree as shown in. If it is traversed in the forward order, the output sequence is. If it is traversed in the middle order, the output sequence is. If it is traversed in the descending order, the output sequence is.
Pre-order: Root A, left subtree B of A, and left subtree B does not. Look at right subtree, It is D, so A-B-D. Let's take A look at the right subtree of A, root C, left subtree E, left subtree F of E, right subtree G of E, and left subtree H of G. Connect to the C-E-F-G-H and the final result is ABDCEFGH
In the middle order: first access the left subtree of the root, B does not have the left subtree, it has the right subtree D, D does not have the left subtree, and the root A of the access tree below is connected to the slave.
Then access the right subtree of the root. The left subtree of C is F, the root of F is E, and the right subtree of E has the left subtree is H, find G from H, end the left subtree of C, and find the root C, no right subtree, and end. The link is FEHGC, and the intermediate result is BDAFEHGC.
Descending order: B has no left subtree, right subtree D, and then to root B. Let's look at the right subtree. The bottom left subtree is F, the right subtree of its root is H, then to the root G of H, and then to the root E of G, the root C of E has no right subtree and goes directly to C. Then, contact B for their root A, so DBFHGECA is connected.
Example 2: There are the following binary trees. The result of the forward traversal of this binary tree is ().
A) acbedgfh B) ABDGCEHF
C) hgfedcba d) ABCDEFGH
Resolution: root A first, left subtree first root B, B No left subtree, right subtree, root D first, in left subtree G, connected to ABDG. Right subtree of A, first rooted in C, C left subtree E, E without left subtree, right subtree with H, right subtree of C Only F, connected to CEHF. B's answer is ABDGCEHF.
Example 3: it is known that the binary tree's post-order traversal is DABEC, the middle-order traversal sequence is DEBAC, and its pre-order traversal sequence is ().
A) cedba B) ACBED C) DECAB D) DEABC
Resolution: It can be seen from the post-order traversal that C is the root node. It can be seen from the middle-order traversal that the Left subtree of C contains DEBA, and no knots on the Right of C, and no right subtree on the zhiroot node. First, traverse the root C in order. Only A starts with C in the answer, which is the correct answer.
The descending traversal is DABEC, and the middle traversal is DEBAC.
From the back-order traversal, we can see that C is the root node. When we see that the middle-order traversal is finally C, the root node does not have the right subtree. In the descending order of traversal from the back to the second is E, then the left subtree of the root node is E, combining two traversal, the first is D, then D is the leftmost subtree. It can be seen that the structure is C E D B
Example 4: the result of sequential traversal in a binary tree is ().
A). acbdfeg B). ACBDFGE C). ABDCGEF D). FCADBEG
Resolution: first, traversing the root in the middle order will separate the left and right subtree. F will not exclude C and D from the beginning and end of the answer. In the right subtree of F, G is the right subtree of E. In the middle traversal, access E first, then access G, and E is in front of G, excluding B. The answer is.
Example 5: The result of the following Binary Tree post-sequential traversal is ().
A) abcdef B) DBEAFC C) ABDECF D) DEBFCA
Resolution: the last post-order must be the root of a binary tree and the answer is D.
The chain storage structure of Binary Trees is an important type of data structure. Its format is defined as follows:
[Cpp]View plaincopy
- // Binary Tree node
- Typedef struct BiTNode {
- // Data
- Char data;
- // Left and right child pointer
- Struct BiTNode * lchild, * rchild;
- } BiTNode, * BiTree;
Create a binary tree:
By reading a string, the algorithm for creating a binary tree is as follows:
[Cpp]View plaincopy
- // Create a binary tree in sequence
- Int CreateBiTree (BiTree & T ){
- Char data;
- // Enter the value of the node in the binary tree in the First Order (one character). '#' indicates the empty tree.
- Scanf ("% c", & data );
- If (data = '#'){
- T = NULL;
- }
- Else {
- T = (BiTree) malloc (sizeof (BiTNode ));
- // Generate the root node
- T-> data = data;
- // Construct the left subtree
- CreateBiTree (T-> lchild );
- // Construct the right subtree
- CreateBiTree (T-> rchild );
- }
- Return 0;
- }
Binary tree traversal:
Traversal is the most basic operation of a tree. The so-called traversal of a binary tree is to travel all nodes of the binary tree in a certain rule and order, so that each node is accessed once, and only once. Because the binary tree structure is non-linear, tree traversal essentially converts each node of the binary tree into a linear sequence for representation.
Recursive Algorithm:
[Cpp]View plaincopy
- // Output
- Void Visit (BiTree T ){
- If (T-> data! = '#'){
- Printf ("% c", T-> data );
- }
- }
- // First-order traversal
- Void PreOrder (BiTree T ){
- If (T! = NULL ){
- // Access the root node
- Visit (T );
- // Access the left subnode
- PreOrder (T-> lchild );
- // Access the right subnode
- PreOrder (T-> rchild );
- }
- }
- // Sequential Traversal
- Void InOrder (BiTree T ){
- If (T! = NULL ){
- // Access the left subnode
- InOrder (T-> lchild );
- // Access the root node
- Visit (T );
- // Access the right subnode
- InOrder (T-> rchild );
- }
- }
- // Post-order traversal
- Void PostOrder (BiTree T ){
- If (T! = NULL ){
- // Access the left subnode
- PostOrder (T-> lchild );
- // Access the right subnode
- PostOrder (T-> rchild );
- // Access the root node
- Visit (T );
- }
- }
Non-recursive algorithms:
<1> sequential traversal:
[Idea]: After accessing T-> data, add T to the stack and traverse the left subtree. When the left subtree is returned, the top element of the stack should be T, and the stack is output, then, traverse the right subtree of T in sequence.
[Cpp]View plaincopy
- /* First-order traversal (non-recursion)
- Idea: After accessing T-> data, T is added to the stack and the left subtree is traversed. When the left subtree is returned, the top element of the stack should be T, and the stack is output, then, traverse the right subtree of T in sequence.
- */
- Void PreOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- // Loop when stack is not empty or p is not empty
- While (p |! Stack. empty ()){
- If (p! = NULL ){
- // Save to stack
- Stack. push (p );
- // Access the root node
- Printf ("% c", p-> data );
- // Traverse the left subtree
- P = p-> lchild;
- }
- Else {
- // Stack rollback
- P = stack. top ();
- Stack. pop ();
- // Access the right subtree
- P = p-> rchild;
- }
- } // While
- }
<2> ordinal Traversal
[Idea]: T is the root pointer of the tree to be traversed. The central traversal requires that the root be accessed after the left subtree is traversed, and then the right subtree be traversed.
First, T is added to the stack and the left subtree is traversed. When the left subtree is returned, the top element of the stack should be T, and the stack is output. Access T-> data, traverse the right subtree of T in the middle order.
[Cpp]View plaincopy
- Void InOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- // Loop when stack is not empty or p is not empty
- While (p |! Stack. empty ()){
- If (p! = NULL ){
- // Save to stack
- Stack. push (p );
- // Traverse the left subtree
- P = p-> lchild;
- }
- Else {
- // Return to the stack and access the root node
- P = stack. top ();
- Printf ("% c", p-> data );
- Stack. pop ();
- // Access the right subtree
- P = p-> rchild;
- }
- } // While
- }
<3> post-order traversal
[Idea]: T is the root pointer of the tree to be traversed. Post-order traversal requires that the root be accessed after the left and right subtree is traversed. You need to determine whether the left and right subtree of the root node have been traversed.
[Cpp]View plaincopy
- // Post-order traversal (non-recursion)
- Typedef struct BiTNodePost {
- BiTree biTree;
- Char tag;
- } BiTNodePost, * BiTreePost;
- Void PostOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- BiTreePost BT;
- // Loop when stack is not empty or p is not empty
- While (p! = NULL |! Stack. empty ()){
- // Traverse the left subtree
- While (p! = NULL ){
- BT = (BiTreePost) malloc (sizeof (BiTNodePost ));
- BT-> biTree = p;
- // Access the left subtree
- BT-> tag = 'l ';
- Stack. push (BT );
- P = p-> lchild;
- }
- // Access the root node after the left and right subtree access is completed
- While (! Stack. empty () & (stack. top ()-> tag = 'R '){
- BT = stack. top ();
- // Stack rollback
- Stack. pop ();
- BT-> biTree;
- Printf ("% c", BT-> biTree-> data );
- }
- // Traverse the right subtree
- If (! Stack. empty ()){
- BT = stack. top ();
- // Access the right subtree
- BT-> tag = 'R ';
- P = BT-> biTree;
- P = p-> rchild;
- }
- } // While
- }
<4> hierarchical Traversal
[Idea]: access each node layer by layer in the order of top-down and left-right. A queue is required in the process of layered traversal.
[Cpp]View plaincopy
- // Hierarchical Traversal
- Void LevelOrder (BiTree T ){
- BiTree p = T;
- // Queue
- Queue Queue;
- // Enter the root node
- Queue. push (p );
- // The queue is not empty.
- While (! Queue. empty ()){
- // The opponent element leaves the queue
- P = queue. front ();
- // Access the node pointed to by p
- Printf ("% c", p-> data );
- // Exit the queue
- Queue. pop ();
- // Leave the left subtree empty.
- If (p-> lchild! = NULL ){
- Queue. push (p-> lchild );
- }
- // Enter the right subtree if it is not empty.
- If (p-> rchild! = NULL ){
- Queue. push (p-> rchild );
- }
- }
- }
Test cases:
Input:
ABC ## DE # G ## F ###
Output:
Code:
[Cpp]View plaincopy
- # Include
- # Include
- # Include
- Using namespace std;
- // Binary Tree node
- Typedef struct BiTNode {
- // Data
- Char data;
- // Left and right child pointer
- Struct BiTNode * lchild, * rchild;
- } BiTNode, * BiTree;
- // Create a binary tree in sequence
- Int CreateBiTree (BiTree & T ){
- Char data;
- // Enter the value of the node in the binary tree in the First Order (one character). '#' indicates the empty tree.
- Scanf ("% c", & data );
- If (data = '#'){
- T = NULL;
- }
- Else {
- T = (BiTree) malloc (sizeof (BiTNode ));
- // Generate the root node
- T-> data = data;
- // Construct the left subtree
- CreateBiTree (T-> lchild );
- // Construct the right subtree
- CreateBiTree (T-> rchild );
- }
- Return 0;
- }
- // Output
- Void Visit (BiTree T ){
- If (T-> data! = '#'){
- Printf ("% c", T-> data );
- }
- }
- // First-order traversal
- Void PreOrder (BiTree T ){
- If (T! = NULL ){
- // Access the root node
- Visit (T );
- // Access the left subnode
- PreOrder (T-> lchild );
- // Access the right subnode
- PreOrder (T-> rchild );
- }
- }
- // Sequential Traversal
- Void InOrder (BiTree T ){
- If (T! = NULL ){
- // Access the left subnode
- InOrder (T-> lchild );
- // Access the root node
- Visit (T );
- // Access the right subnode
- InOrder (T-> rchild );
- }
- }
- // Post-order traversal
- Void PostOrder (BiTree T ){
- If (T! = NULL ){
- // Access the left subnode
- PostOrder (T-> lchild );
- // Access the right subnode
- PostOrder (T-> rchild );
- // Access the root node
- Visit (T );
- }
- }
- /* First-order traversal (non-recursion)
- Idea: After accessing T-> data, T is added to the stack and the left subtree is traversed. When the left subtree is returned, the top element of the stack should be T, and the stack is output, then, traverse the right subtree of T in sequence.
- */
- Void PreOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- // Loop when stack is not empty or p is not empty
- While (p |! Stack. empty ()){
- If (p! = NULL ){
- // Save to stack
- Stack. push (p );
- // Access the root node
- Printf ("% c", p-> data );
- // Traverse the left subtree
- P = p-> lchild;
- }
- Else {
- // Stack rollback
- P = stack. top ();
- Stack. pop ();
- // Access the right subtree
- P = p-> rchild;
- }
- } // While
- }
- /* Sequential traversal (non-recursion)
- Idea: T is the root pointer of the tree to be traversed. The central traversal requires that after the left subtree is traversed, the root is accessed and then the right subtree is traversed.
- First, T is added to the stack and the left subtree is traversed. When the left subtree is returned, the top element of the stack should be T, and the stack is output. Access T-> data, traverse the right subtree of T in the middle order.
- */
- Void InOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- // Loop when stack is not empty or p is not empty
- While (p |! Stack. empty ()){
- If (p! = NULL ){
- // Save to stack
- Stack. push (p );
- // Traverse the left subtree
- P = p-> lchild;
- }
- Else {
- // Return to the stack and access the root node
- P = stack. top ();
- Printf ("% c", p-> data );
- Stack. pop ();
- // Access the right subtree
- P = p-> rchild;
- }
- } // While
- }
- // Post-order traversal (non-recursion)
- Typedef struct BiTNodePost {
- BiTree biTree;
- Char tag;
- } BiTNodePost, * BiTreePost;
- Void PostOrder2 (BiTree T ){
- Stack Stack;
- // P is the traversal pointer
- BiTree p = T;
- BiTreePost BT;
- // Loop when stack is not empty or p is not empty
- While (p! = NULL |! Stack. empty ()){
- // Traverse the left subtree
- While (p! = NULL ){
- BT = (BiTreePost) malloc (sizeof (BiTNodePost ));
- BT-> biTree = p;
- // Access the left subtree
- BT-> tag = 'l ';
- Stack. push (BT );
- P = p-> lchild;
- }
- // Access the root node after the left and right subtree access is completed
- While (! Stack. empty () & (stack. top ()-> tag = 'R '){
- BT = stack. top ();
- // Stack rollback
- Stack. pop ();
- BT-> biTree;
- Printf ("% c", BT-> biTree-> data );
- }
- // Traverse the right subtree
- If (! Stack. empty ()){
- BT = stack. top ();
- // Access the right subtree
- BT-> tag = 'R ';
- P = BT-> biTree;
- P = p-> rchild;
- }
- } // While
- }
- // Hierarchical Traversal
- Void LevelOrder (BiTree T ){
- BiTree p = T;
- // Queue
- Queue Queue;
- // Enter the root node
- Queue. push (p );
- // The queue is not empty.
- While (! Queue. empty ()){
- // The opponent element leaves the queue
- P = queue. front ();
- // Access the node pointed to by p
- Printf ("% c", p-> data );
- // Exit the queue
- Queue. pop ();
- // Leave the left subtree empty.
- If (p-> lchild! = NULL ){
- Queue. push (p-> lchild );
- }
- // Enter the right subtree if it is not empty.
- If (p-> rchild! = NULL ){
- Queue. push (p-> rchild );
- }
- }
- }
- Int main ()
- {
- BiTree T;
- CreateBiTree (T );
- Printf ("sequential traversal: \ n ");
- PreOrder (T );
- Printf ("\ n ");
- Printf ("first-order traversal (non-recursive): \ n ");
- PreOrder2 (T );
- Printf ("\ n ");
- Printf ("sequential traversal: \ n ");
- InOrder (T );
- Printf ("\ n ");
- Printf ("sequential traversal (non-recursive): \ n ");
- InOrder2 (T );
- Printf ("\ n ");
- Printf ("sequential traversal: \ n ");
- PostOrder (T );
- Printf ("\ n ");
- Printf ("post-order traversal (non-recursive): \ n ");
- PostOrder2 (T );
- Printf ("\ n ");
- Printf ("hierarchical traversal: \ n ");
- LevelOrder (T );
- Printf ("\ n ");
- Return 0;
- }