Binary tree traversal Method

Source: Internet
Author: User

When I learned about Binary Trees today, I saw the first, last, and middle order traversal methods of Binary Trees. However, the Recursive Implementation of the binary tree traversal method is very simple, and the implementation of the iterative version is a little complicated. You can use the various traversal methods and implementation versions of the binary tree to review the previous implementation methods.

It is quite difficult to implement recursion among the three traversal methods, and the code is quite simple.

In the iterative versions implemented by the stack in the three traversal methods, the implementation of the first and middle order is relatively difficult, but the implementation of the latter method is complicated, the implementation method here is different from the previous versions in the first and middle order.

In addition, the complexity of O (1) space is implemented in the first order. The implementation in the middle order is relatively easy, and the latter order is relatively complicated.

The following is the complete test code. The main function inputs the method for generating Binary Trees:

Enter the number of nodes, and then enter the tree structure 1, 2, 3, #, 4 where # indicates null, then 1, 2, 3, #, 4 indicates the following structure:

       1     /       2     3     \       4

For another example, enter the number of seven nodes. Input 1, 2, 3, 4, 5, 6, and 7 indicates the tree structure is as follows:

                     1                   /                    2    3                /  \  /                 4   5  6   7

Code:

The suffix _ r is Recursive Implementation, the suffix _ s is stack iteration implementation, and the suffix _ m is O (1) space complexity iteration implementation.

The _ m iteration implementation thread is insecure because the tree structure is changed temporarily.

#include <iostream>#include <stack>#include <vector>using namespace std;struct TreeNode {int label;TreeNode *right;TreeNode *left;TreeNode(int temp):label(temp) {}};class Solution {public:void inordertraversal_r(TreeNode *root) {if(root == NULL) {return;}inordertraversal_r(root->left);cout<<root->label<<" ";inordertraversal_r(root->right);}void preordertraversal_r(TreeNode *root) {if(root == NULL) {return;}cout<<root->label<<" ";preordertraversal_r(root->left);preordertraversal_r(root->right);}void postordertraversal_r(TreeNode *root) {if(root == NULL) {return;}postordertraversal_r(root->left);postordertraversal_r(root->right);cout<<root->label<<" ";}void inordertraversal_s(TreeNode *root) {stack<TreeNode*> s;TreeNode *cur = root;while(!s.empty() || cur != NULL) {while(cur != NULL) {s.push(cur);cur = cur->left;}cur = s.top();s.pop();cout<<cur->label<<" ";cur = cur->right;}}void preordertraversal_s(TreeNode *root) {stack<TreeNode*> s;TreeNode *cur = root;while(!s.empty() || cur != NULL) {while(cur != NULL) {cout<<cur->label<<" ";s.push(cur);cur = cur->left;}cur = s.top();s.pop();cur = cur->right;}}//change thinking , it‘s too hardvoid postordertraversal_s(TreeNode *root) {//the original method/*We use a prev variable to keep track of the previously-traversed node. Let’s assume curr is the current node that’s on top of the stack. When prev is curr‘s parent, we are traversing down the tree. In this case, we try to traverse to curr‘s left child if available (ie, push left child to the stack). If it is not available, we look at curr‘s right child. If both left and right child do not exist (ie, curr is a leaf node), we print curr‘s value and pop it off the stack.If prev is curr‘s left child, we are traversing up the tree from the left. We look at curr‘s right child. If it is available, then traverse down the right child (ie, push right child to the stack), otherwise print curr‘s value and pop it off the stack.If prev is curr‘s right child, we are traversing up the tree from the right. In this case, we print curr‘s value and pop it off the stack.*/if(root == NULL) {return;}stack<TreeNode*> s;s.push(root);TreeNode *cur = NULL;TreeNode *pre = NULL;while( !s.empty() ) {cur = s.top();//left down or right downif( pre == NULL || pre->left == cur || pre->right == cur) {if(cur->left != NULL) {s.push(cur->left);}else if(cur->right != NULL) {s.push(cur->right);}else {cout<<cur->label<<" ";s.pop();}}//left upelse if(cur->left == pre) {if(cur->right != NULL) {s.push(cur->right);}else {cout<<cur->label<<" ";s.pop();}}//right upelse if(cur->right == pre) {cout<<cur->label<<" ";s.pop();}pre = cur;}/* the easy methodstack<TreeNode*> s;s.push(root);TreeNode *cur = NULL;TreeNode *pre = NULL;while( !s.empty() ) {cur = s.top();if(pre == NULL || pre->left == cur || pre->right == cur) {if(cur->left != NULL) {s.push(cur->left);}else if(cur->right != NULL) {s.push(cur->right);}}else if(cur->left == pre) {if(cur->right != NULL) {s.push(cur->right);}}else {cout<<cur->label<<" ";s.pop();}pre = cur;}*/}void inordertraversal_m(TreeNode *root) {TreeNode *cur = root;TreeNode *pre = NULL;while(cur != NULL) {if(cur->left == NULL) {cout<<cur->label<<" ";cur = cur->right;}else {pre = cur->left;while( pre->right != NULL && pre->right != cur ) {pre = pre->right;}if(pre->right == NULL) {pre->right = cur;cur = cur->left;}else {pre->right = NULL;cout<<cur->label<<" ";cur = cur->right;}}}}void preordertraversal_m(TreeNode *root) {TreeNode *cur = root;TreeNode *pre = NULL;while(cur != NULL) {if(cur->left == NULL) {cout<<cur->label<<" ";cur = cur->right;}else {pre = cur->left;while( pre->right != NULL && pre->right != cur ) {pre = pre->right;}if(pre->right == NULL) {pre->right = cur;cout<<cur->label<<" ";cur = cur->left;}else {pre->right = NULL;cur = cur->right;}}}}//this is the most difficult algorithmvoid reverse_out(TreeNode *from,TreeNode *to) {//first reverse from->to//reverseTreeNode *cur = from;TreeNode *post = cur->right;while(cur != to) {TreeNode *tmp = post->right;post->right = cur;cur = post;post = tmp;}//already reverse,output listTreeNode *traversal = cur;while( cur != from ) {cout<<cur->label<<" ";cur = cur->right;}cout<<cur->label<<" ";//reverse originalcur = to;post = cur->right;while(cur != from) {TreeNode *tmp = post->right;post->right = cur;cur = post;post = tmp;}//restore to‘s rightto->right = NULL;}void postordertraversal_m(TreeNode *root) {TreeNode *newroot = new TreeNode(0);newroot->left = root;newroot->right = NULL;TreeNode *cur = newroot;TreeNode *pre = NULL;while(cur != NULL) {if(cur->left == NULL) {    cur = cur->right;}else {pre = cur->left;while(pre->right != NULL && pre->right != cur) {pre = pre->right;}if(pre->right == NULL) {pre->right = cur;cur = cur->left;} else {pre->right = NULL;reverse_out(cur->left,pre);cur = cur->right;}}}}};int main(void) {int nodecount;while( cin>>nodecount && nodecount != 0 ) {vector<TreeNode*> treenodes;while(nodecount) {nodecount--;int nodelabel;cin>>nodelabel;if( ((char)nodelabel) == ‘#‘ ) {treenodes.push_back( NULL );}else {treenodes.push_back( new TreeNode(nodelabel) );}}//construct the treefor(int i = 0;i < treenodes.size();i++) {if( treenodes[i] == NULL ) {continue;}else {//left child node 2n+1,right child node 2n+2if( 2*i + 1 < treenodes.size() ) {treenodes[i]->left = treenodes[2*i + 1];}else {treenodes[i]->left = NULL;}if( 2*i + 2 < treenodes.size() ) {treenodes[i]->right = treenodes[2*i + 2];}else {treenodes[i]->right = NULL;}}}Solution s;cout<<"inorder recursion  :";s.inordertraversal_m(treenodes[0]);cout<<endl;cout<<"inorder stack      :";s.inordertraversal_s(treenodes[0]);cout<<endl;cout<<"inorder morris     :";s.inordertraversal_r(treenodes[0]);cout<<endl;cout<<"preorder recursion :";s.preordertraversal_m(treenodes[0]);cout<<endl;cout<<"preorder stack     :";s.preordertraversal_s(treenodes[0]);cout<<endl;cout<<"preorder morris    :";s.preordertraversal_r(treenodes[0]);cout<<endl;cout<<"postorder recursion:";s.postordertraversal_m(treenodes[0]);cout<<endl;cout<<"postorder stack    :";s.postordertraversal_s(treenodes[0]);cout<<endl;cout<<"postorder morris   :";s.postordertraversal_r(treenodes[0]);cout<<endl;for(int i = 0;i < treenodes.size();i++) {delete treenodes[i];}}}

A diagram of the preliminary sequence and middle sequence of MORRIS:

Diagram of the Post-order ideas of moriis:

Binary tree traversal Method

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.