"Introduction to Algorithm" the implementation of non-recursive traversal of binary tree before and after

Source: Internet
Author: User

Recursive traversal of binary tree is simple, and the code is concise, but not recursive traversal is not so simple, we need to use another data structure---stack to implement. The traversal of the binary tree can be divided into pre-order, sequence and order three, they are in accordance with the root node in the traversal of the location of the division, the pre-sequence traversal is the root node is traversed first, the sequence of root nodes in the left and right leaves between the nodes are traversed, the order is the root node is finally traversed. In three kinds of non-recursive traversal, the pre-order and the middle order are not too duplicated, and the sequential traversal is relatively difficult.


first, pre-sequence traversal

Here we have the pre-order traversal in the order of " root - left -right". This is done in the order of recursion-non-recursion, followed by several.

1, the realization of recursion:

void Travel_3 (node* r) {if (r! = NULL) {cout << r->key << '; Travel_3 (r->left); Travel_3 (r->right);}}


2, non-recursive implementation:

Non-recursive implementation is definitely need to use the loop structure, the pre-sequence traversal and the first root after the left and then the right, so we must first need to traverse the left node, but after the left node also need us to continue to traverse the right node, and the right node may also have left dial hand tree need to traverse. So, here we need a stack to save the nodes we have traversed, after traversing left and right nodes, then the stack of the nodes saved in the stack, and then traverse the node of the tree, and then the right subtree node into the loop, so that the lower nodes will be near the top of the stack, will be first out of the stack, This makes it possible to implement a non-recursive pre-order traversal. The steps are as follows:


① outputs the value of the node and then the node into the stack


② then determine whether the left child node of the node is empty, if not empty, then continue the loop ①, if it is empty, it proves that the node value on this line has been traversed, then out of the loop out of the stack operation, as long as the stack is not empty, the node out of the stack, and let the node equal to his right child node,


③ until all nodes are out of the stack and loop to the node is empty, the traversal ends


void Travel_4 (node* r) {if (r! = null) {stack<node*> s;while (r! = NULL | |!s.empty ()) {while (r! = null) {cout < < R->key << '; S.push (r); r = R->left;} if (!s.empty ()) {R = S.top (); S.pop (); r = R->right;}}}}


second, middle sequence traversal

The middle sequence traversal is somewhat similar to the pre-order traversal, just in the order of "left-root-right" traversal.


1, the realization of recursion:


void Travel_1 (node* r) {if (r! = NULL) {travel_1 (r->left); cout << r->key << '; Travel_1 (r->right);}}



2, non-recursive implementation:


The non-recursive implementation of the sequential traversal of the preceding sequence traversal of the non-recursive implementation of the same, the difference is only the output node key value position has changed, no longer is the left child node before the output, but after the stack output the value of the node, which will start from the leftmost node output, and then the root node and the right node.


① the node directly into the stack


② then determines whether the left child node of the node is empty, if it is not empty, then continue the loop ①, if it is empty, it proves that the line is already on the stack, then jump out of the loop to the stack operation, as long as the stack is not empty, then the node out of the stack, the value of the output node, and let the node equals his right child node, continue the ① number loop


③ until all nodes are out of the stack and loop to the node is empty, the traversal ends


void Travel_2 (node* r) {if (r! = null) {stack<node*> s;while (r! = NULL | |!s.empty ()) {while (r! = null) {S.push (R) ; r = R->left;} if (!s.empty ()) {R = S.top (); S.pop (); cout << R->key << '; r = R->right;}}}}



Third, post-sequential traversal

The recursive implementation of the post-post traversal is no different from the previous, but the non-recursive implementation is more complex.


1, the realization of recursion:


void Travel_5 (node* r) {if (r! = NULL) {travel_5 (r->left); Travel_5 (r->right); cout << r->key << ';}}


2, non-recursive implementation:


We still need to use the stack here, but we need to make sure that the root node must be traversed after the left and right child nodes have been traversed. How can this be ensured? You can do this as follows:


① get a node, first put it into the stack, then it must be in comparison to the bottom of the place, compared to the stack late


② If this node has no left and right child nodes, then we can output it directly


③ If the node has a left or right child node or both, then we can then put its left and the child nodes into the stack


④ at the time of the stack, we will find that the left and right child nodes are always out of the stack before the root node, that is, before they are traversed, but there is still a problem.


⑤ we need to determine whether there is only the left node or the right node, or both, in the stack. If we find that both the left and right child nodes are empty, it can be output directly, or it may be output if the previous node that is out of the stack is either its left node or it is a child node. Otherwise, it means that the node is a new node, and we need to put its left and right child nodes into the stack first.



void Travel_6 (node* r) {if (r==null) {return;} stack<node*> s;node* current_pointer;node* Last_pointer; Record the previous loop pointer S.push (r); First, the root node is stacked while (!s.empty ()) {current_pointer = S.top ();//Each loop is updated with the current pointer to the new stack top if (current_pointer->left = = NULL && Amp Current_pointer->right = = NULL) | | (Last_pointer! = null&& (Last_pointer = = current_pointer->left| | last_pointer = current_pointer->right) ) {cout << current_pointer->key << "; S.pop (); last_pointer = Current_pointer;} else {if (current_pointer->right! = NULL) {s.push (current_pointer->right);} if (current_pointer->left! = NULL) {s.push (current_pointer->left);}}}


Full code:


struct Node {int key;node* parent;node* left;node* right;node (): Key (0), parent (), left (null), right (null) {}node (int k): Key (k), parent (), left (null), right (null) {}~node () {key = 0;}}; Class Bintree {public:node* Root;bintree (): root () {}~bintree () {///Do some cleanup work to prevent memory leaks delete_tree (root);} void Delete_tree (node* p) {if (P! = NULL) {node* right = p->right;node* left = P->left;delete_tree (left);d elete P;de Lete_tree (right);p = Null;right = Null;left = NULL;}} /* Middle sequence traversal, recursive */void Travel_1 (node* R) {if (r! = NULL) {travel_1 (r->left); cout << r->key << '; travel_ 1 (r->right);}} void Travel_1 () {travel_1 (root); cout << Endl;} /* Middle sequence traversal, non-recursive mode */void travel_2 (node* R) {if (r! = null) {stack<node*> s;while (r! = NULL | |!s.empty ()) {while (R!) = NULL) {S.push (r); r = R->left;} if (!s.empty ()) {R = S.top (); S.pop (); cout << R->key << '; r = R->right;}}}} void Travel_2 () {travel_2 (root); cout << Endl;} /* pre-sequence traversal, recursive */void travel_3 (node* r) {if(r! = NULL) {cout << r->key << '; Travel_3 (r->left); Travel_3 (r->right);}} void Travel_3 () {travel_3 (root); cout << Endl;} /* * Pre-sequence traversal, non-recursive mode */void travel_4 (node* R) {if (r! = null) {stack<node*> s;while (r! = NULL | |!s.empty ()) {while (R!) = NULL) {cout << r->key << '; S.push (r); r = R->left;} if (!s.empty ()) {R = S.top (); S.pop (); r = R->right;}}}} void Travel_4 () {travel_4 (root); cout << Endl;} /* * post-mode, recursive */void travel_5 (node* R) {if (r! = NULL) {travel_5 (r->left); Travel_5 (r->right); cout << R->key << ';}} void Travel_5 () {travel_5 (root); cout << Endl;} /* Post mode, non-recursive */void travel_6 (node* R) {if (r==null) {return;} stack<node*> s;node* current_pointer;node* Last_pointer; Record the previous loop pointer S.push (r); First, the root node is stacked while (!s.empty ()) {current_pointer = S.top ();//Each loop is updated with the current pointer to the new stack top if (current_pointer->left = = NULL && Amp Current_pointer->right = = NULL) | | (Last_pointer! = null&& (Last_pointer = = Current_pointer->left| | Last_pointer = = current_pointer->right)) {cout << current_pointer->key << "; S.pop (); last_pointer = Current_pointer;} else {if (current_pointer->right! = NULL) {s.push (current_pointer->right);} if (current_pointer->left! = NULL) {s.push (current_pointer->left);}}} void Travel_6 () {travel_6 (root); cout << Endl;}};












"Introduction to Algorithm" the implementation of non-recursive traversal of binary tree before and after

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.