Binary tree non-recursive front-middle-post-order traversal universal template

For non-recursive traversal of binary trees, the data structure of the stack is mainly used to simulate recursion. This algorithm uses the command structure to mark each node to control whether to print. The advantage of this is that the code applies three traversal methods in front, middle and back order. When marked When the string is go, it visits its child nodes. If it is print, the value of the current node is printed, and the stacking order is different in the first, middle, and last order.

Preorder traversal

#include<string>
#include<iostream>
#include<stack>
using namespace std;

//二叉树结点结构体
struct TreeNode {
    
    
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {
    
    }
};

//控制是否打印结构体,使用它可保证此代码适用三种遍历
struct command{
    
    
    string s;
    TreeNode *node;
    command(string s, TreeNode *node): s(s), node(node){
    
    }
};

void preorderTraversal(TreeNode* root) {
    
    
    if(root == NULL)
        return;
    stack<command> st;
    st.push(command("go", root));
    while(!st.empty()){
    
    
        command com = st.top();
        st.pop();
        if(com.s == "print")
            cout<< com.node->val<< " ";
        else{
    
    
            //先将右子树入栈,先访问的就是当前节点,然后左子树
            if(com.node->right)
                st.push(command("go", com.node->right));
            if(com.node->left)
                st.push(command("go", com.node->left));
            st.push(command("print", com.node));
        }
    }
}

In-order traversal

The middle-order traversal is basically the same as the above. You only need to change the stacking order. First, the right subtree is pushed into the stack, and then the current node mark is changed to print and pushed onto the stack, and finally the left subtree is pushed

if(com.node->right)
	st.push(command("go", com.node->right));
st.push(command("print", com.node));
if(com.node->left)
	st.push(command("go", com.node->left));

After modifying the order, the middle order is traversed

#include<string>
#include<iostream>
#include<stack>
using namespace std;

//二叉树结点结构体
struct TreeNode {
    
    
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {
    
    }
};

//控制是否打印结构体,使用它可保证此代码适用三种遍历
struct command{
    
    
    string s;
    TreeNode *node;
    command(string s, TreeNode *node): s(s), node(node){
    
    }
};

void preorderTraversal(TreeNode* root) {
    
    
    if(root == NULL)
        return;
    stack<command> st;
    st.push(command("go", root));
    while(!st.empty()){
    
    
        command com = st.top();
        st.pop();
        if(com.s == "print")
            cout<< com.node->val<< " ";
        else{
    
    
            //先将右子树入栈,再把当前节点标记改为print并入栈,最后左子树入栈
            if(com.node->right)
				st.push(command("go", com.node->right));
			st.push(command("print", com.node));
			if(com.node->left)
				st.push(command("go", com.node->left));
        }
    }
}

In-order traversal

Post-order traversal also changes the order

st.push(command("print", com.node));
if(com.node->right)
    st.push(command("go", com.node->right));
if(com.node->left)
    st.push(command("go", com.node->left));

After modifying the order, it is the post-order traversal

#include<string>
#include<iostream>
#include<stack>
using namespace std;

//二叉树结点结构体
struct TreeNode {
    
    
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {
    
    }
};

//控制是否打印结构体,使用它可保证此代码适用三种遍历
struct command{
    
    
    string s;
    TreeNode *node;
    command(string s, TreeNode *node): s(s), node(node){
    
    }
};

void preorderTraversal(TreeNode* root) {
    
    
    if(root == NULL)
        return;
    stack<command> st;
    st.push(command("go", root));
    while(!st.empty()){
    
    
        command com = st.top();
        st.pop();
        if(com.s == "print")
            cout<< com.node->val<< " ";
        else{
    
    
			st.push(command("print", com.node));
			if(com.node->right)
			    st.push(command("go", com.node->right));
			if(com.node->left)
			    st.push(command("go", com.node->left));
        }
    }
}

Guess you like

Origin blog.csdn.net/UCB001/article/details/106919807