Recursive and non-recursive traversal of Binary Trees:

Source: Internet
Author: User

Recursive and non-recursive traversal of Binary Trees:

Recursive and non-recursive traversal of Binary Trees:

# Include <fstream>
# Include <iostream>

Using namespace std;

/*
Non-recursive implementations of post-order traversal are the most difficult of the three traversal methods. In the post-order traversal, make sure that both the left and right children are accessed and the left and right children are in the right
Access to the root node only after access, which brings difficulties to process control. The following two ideas are described.
First Thought: For any node P, put it into the stack, and then search down along the left subtree until the node with no left child is found, then the node appears at the top of the stack,
However, the child on the right cannot be accessed from the stack. Therefore, the right subtree is processed according to the same rules.
When the node appears at the top of the stack, it can be pulled out of the stack and accessed. This ensures the correct access order. It can be seen that in this process, each node appears twice
On the top of the stack, you can access it only when it appears at the top of the stack for the second time. Therefore, you need to set another variable to identify whether the node appears at the top of the stack for the first time.
The second approach is to ensure that the root node can be accessed only after the access from the left and right children. Therefore, for any node P, it is first written into the stack. If P does not exist,
You can directly access the node. If P exists in the Left or Right child, but both the left and right children have been accessed, you can also directly access the node. If the two
In this case, P's right child and left child are added to the stack in sequence to ensure that the left child is accessed in front of the right child each time the top element of the stack is obtained, both left and right are rooted
It is accessed at the beginning of the hour.
*/

# Define queue_len 100.

Struct node
{
Char c;
Struct node * lch, * rch;
Bool flag;
Node (): flag (0 ){}
Void get_c ()
{
Printf ("% c", c );
}
};

Node * set_tree (); // build
Void pre_order (node * tree); // first-order traversal
Void in_order (node * tree); // returns the ordinal traversal.
Void post_order (node * tree); // post-order traversal
Void level_order (node * tree); // layered Traversal
Void nr_pre_order (node * tree); // non-recursive first-order traversal
Void nr_in_order (node * tree); // non-recursive sequential Traversal
Void nr_post_order_1 (node * tree); // non-recursive post-order traversal
Void nr_post_order_2 (node * tree); // non-recursive post-order traversal

Int main ()
{
// Freopen ("D: \ input. in", "r", stdin );
// Freopen ("D: \ output. out", "w", stdout );
Node * tree = set_tree ();
Printf ("sequential traversal :");
Pre_order (tree );
Puts ("");
Printf ("sequential traversal :");
In_order (tree );
Puts ("");
Printf ("sequential traversal :");
Post_order (tree );
Puts ("");
Printf ("hierarchical traversal :");
Level_order (tree );
Puts ("");
Printf ("sequential traversal :");
Nr_pre_order (tree );
Puts ("");
Printf ("sequential traversal :");
Nr_in_order (tree );
Puts ("");
Printf ("sequential traversal :");
Nr_post_order_1 (tree );
Puts ("");
Printf ("sequential traversal :");
Nr_post_order_2 (tree );
Puts ("");
Return 0;
}
Node * set_tree ()
{
Node * p, * s;
Node * gen = new node;
Gen-> c = 'a ';

Gen-> lch = new node;
P = gen-> lch;
P-> c = 'B ';
P-> rch = NULL;
P-> lch = new node;
P = p-> lch;
P-> c = 'D ';
P-> lch = NULL;
P-> rch = new node;
P = p-> rch;
P-> c = 'G ';
P-> lch = NULL;
P-> rch = NULL;

Gen-> rch = new node;
P = gen-> rch;
P-> c = 'C ';
P-> lch = new node;
S = p-> lch;
S-> c = 'E ';
S-> lch = NULL;
S-> rch = NULL;
P-> rch = new node;
S = p-> rch;
S-> c = 'F ';
S-> lch = NULL;
S-> rch = NULL;

Return gen;
}
Void pre_order (node * tree)
{
If (tree! = NULL)
{
Tree-> get_c ();
Pre_order (tree-> lch );
Pre_order (tree-> rch );
}
}
Void in_order (node * tree)
{
If (tree! = NULL)
{
In_order (tree-> lch );
Tree-> get_c ();
In_order (tree-> rch );
}
}
Void post_order (node * tree)
{
If (tree! = NULL)
{
Post_order (tree-> lch );
Post_order (tree-> rch );
Tree-> get_c ();
}
}
Void level_order (node * tree)
{
Node * queue_1 [queue_len];
Int front, rear;

If (tree = NULL) return;
Front =-1;
Rear = 0;
Queue_1 [rear] = tree;
While (front! = Rear)
{
Front ++;
Queue_1 [front]-> get_c ();

If (queue_1 [front]-> lch! = NULL)
{
Rear ++;
Queue_1 [rear] = queue_1 [front]-> lch;
}
If (queue_1 [front]-> rch! = NULL)
{
Rear ++;
Queue_1 [rear] = queue_1 [front]-> rch;
}
}
}
Void nr_pre_order (node * tree)
{
Node * stack_1 [queue_len];
Int top =-1;

If (tree = NULL) return;
Node * p = tree;
While (p! = NULL | top! =-1)
{
While (p! = NULL)
{
P-> get_c ();
Stack_1 [++ top] = p;
P = p-> lch;
}
If (top =-1) return;
P = stack_1 [top --]-> rch;
}
}
Void nr_in_order (node * tree)
{
Node * stack_1 [queue_len];
Int top =-1;

If (tree = NULL) return;
Node * p = tree;
While (p! = NULL | top! =-1)
{
While (p! = NULL)
{
Stack_1 [++ top] = p;
P = p-> lch;
}
If (top =-1) return;
Stack_1 [top]-> get_c ();
P = stack_1 [top --]-> rch;
}
}
Void nr_post_order_1 (node * tree)
{
Node * stack_1 [queue_len];
Int top =-1;

If (tree = NULL) return;
Node * p = tree;
While (p! = NULL | top! =-1)
{
While (p! = NULL)
{
Stack_1 [++ top] = p;
P = p-> lch;
}
If (top =-1) return;
P = stack_1 [top];
If (p-> flag = 0) {p-> flag = 1; p = p-> rch ;}
Else {p-> get_c (), top --; p = NULL ;}
}
}
Void nr_post_order_2 (node * tree)
{
Node * stack_1 [queue_len];
Int top = 0;

If (tree = NULL) return;
Node * p = tree;
While (top! =-1)
{
P = stack_1 [top];
If (p-> lch = NULL | p-> lch-> flag = 0) & (p-> rch = NULL | p-> rch-> flag = 0 ))
{
P-> get_c ();
P-> flag = 0;
Top --;
}
Else
{
If (p-> rch! = NULL)
{
Stack_1 [++ top] = p-> rch;
}
If (p-> lch! = NULL)
{
Stack_1 [++ top] = p-> lch;
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.