史上最全二叉树的遍历方法

#include<iostream>
#include<sstream>
#include<set>
#include<stack>
#include<queue>
#include<vector>

using namespace std;

namespace tree{

struct TNode {
    TNode* left;
    TNode* right;
    int val;
    TNode(int val):left(nullptr),right(nullptr),val(val){}
};
/** * 先序遍历二叉树 **/
void pre_order(TNode* root) {
    if (root == nullptr) return;
    cout<<root->val<<",";
    pre_order(root->left);
    pre_order(root->right);
}
/** * 中序遍历二叉树 **/
void in_order(TNode* root) {
    if(root == nullptr) return;
    in_order(root->left);
    cout<<root->val<<",";
    in_order(root->right);
}
/** * 后序遍历二叉树 **/
void post_order(TNode* root) {
    if (root == nullptr) return;
    post_order(root->left);
    post_order(root->right);
    cout<<root->val<<",";
}

/** * 层序遍历 **/
void level_order_visit(vector<TNode*> q) {
    if (q.empty()) return;
    vector<TNode*> qNext;
    for (auto ele : q) {
        cout<<ele->val<<",";
        if (ele->left) {
            qNext.push_back(ele->left);
        }
        if (ele->right) {
            qNext.push_back(ele->right);
        }
    }
    level_order_visit(qNext);
}
/** * 层序遍历迭代版,支持分层打包 **/
void level_order_visit_iter(TNode* root) {
    queue<TNode*> q;
    q.push(root);
    q.push(nullptr);
    while (!q.empty()) {
        auto cur = q.front();
        q.pop();
        if (cur == nullptr) {
            if (q.empty() == false) q.push(nullptr);
        } else {
            cout<<cur->val<<",";
            if (cur->left) {
                q.push(cur->left);
            }
            if (cur->right) {
                q.push(cur->right);
            }
        }
    }
    cout<<endl;
}
void level_order_visit_iter_II(TNode* root) {
    set<TNode*> visited;
    queue<TNode*> q;
    q.push(root);
    while(!q.empty()) {
        TNode* tmp = q.front();
        cout<<tmp->val<<",";
        q.pop();
        if (tmp->left) {
            q.push(tmp->left);
        }
        if (tmp->right) {
            q.push(tmp->right);
        }
    }
}

void in_order_iter(TNode* root) {
    stack<TNode*> s;
    TNode* cur = root;
    while(!s.empty() || cur != nullptr) {
        while(cur != nullptr) {
            s.push(cur);
            cur = cur->left;
        }
        TNode* tmp = s.top();
        cout<<tmp->val<<",";
        s.pop();
        cur = tmp->right;
    }
}

void pre_order_iter(TNode* root) {
    stack<TNode*> s;
    TNode* cur = root;
    while(!s.empty() || cur != nullptr) {
        while(cur != nullptr) {
            s.push(cur);
            cout<<cur->val<<",";
            cur = cur->left;
        }
        TNode* tmp = s.top();
        s.pop();
        cur = tmp->right;
    }
}

void post_order_iter(TNode* root) {
    stack<TNode*> s;
    TNode* cur = root;
    TNode* pre = nullptr;
    while (!s.empty() || cur != nullptr) {
        while (cur != nullptr) {
            s.push(cur);
            cur = cur->left;
        }
        TNode* tmp = s.top();
        if (tmp->right == pre || tmp->right == nullptr) {
            cout<<tmp->val<<",";
            pre = tmp;
            s.pop();
            cur = nullptr;
        } else {
            cur = tmp->right; //写错的原因是思路不清晰
        }
    }
}


/** * morris 先序遍历算法 **/
void morris_pre_order(TNode* root) {
    TNode* cur = root;
    while(cur) {
        if (cur->left == nullptr) {
            cout<<cur->val<<",";
            cur = cur->right;
            return;
        }
        TNode* curInner = cur->left;
        while (curInner->right != nullptr && curInner->right != cur) {
            curInner = curInner->right;
        }
        if(curInner->right == nullptr) {
            curInner->right = cur;
            cur = cur->left;
            cout<<cur->val<<",";
        } else {
            curInner->right = nullptr;
            cur = cur->right;
        }
    }
}
/** * morris 中序遍历算法 **/
void morris_in_order(TNode* root) {
    TNode* cur = root;
    while(cur) {
        if (cur->left == nullptr) {
            cout<<cur->val<<",";
            cur = cur->right;
            return;
        }
        TNode* curInner = cur->left;
        while (curInner->right != nullptr && curInner->right != cur) {
            curInner = curInner->right;
        }
        if(curInner->right == nullptr) {
            curInner->right = cur;
            cur = cur->left;
        } else {
            curInner->right = nullptr;
            cout<<cur->val<<",";
            cur = cur->right;
        }
    }
}
/** * morris 后续遍历算法 **/
void revert_link(TNode* beg, TNode* end) {
    TNode* head = beg->right;
    TNode* cur = beg;
    while (cur != end) {
        TNode* head_tmp = head->right;
        head->right = cur;
        cur = head;
        head = head_tmp;
    }
    beg->right = nullptr;
}

void visit_link(TNode* beg, TNode* end) {
    revert_link(beg, end);
    TNode* cur = end;
    while (cur != nullptr) {
        cout<<cur->val<<",";
        cur = cur->right;
    }
    revert_link(end, beg);
}

void morris_post_order(TNode* root) {
    TNode* tmp_root = new TNode(0);
    tmp_root->left = root;
    TNode* cur = tmp_root;
    while(cur != nullptr) {
        if (cur->left == nullptr) {
            cur = cur->right;
            continue;
        }
        TNode* tmp = cur->left;
        while (tmp->right != nullptr && tmp->right != cur) tmp = tmp->right;
        if (tmp->right == nullptr) {
            tmp->right = cur;
            cur = cur->left;
        } else {
            tmp->right = nullptr;
            TNode* beg = cur->left;
            TNode* end = tmp;
            visit_link(beg, end);
            cur = cur->right;
        }
    }
    delete tmp_root;
    tmp_root = nullptr;
}
/** * 构建二叉树 **/
int string2int(string& in) {
    stringstream ss(in);
    int ret;
    ss>>ret;
    return ret;
}
TNode* create_tree(vector<string>& tree) {
    queue<TNode*> q;
    TNode* root = new TNode(string2int(tree[0]));
    q.push(root);
    int cur_index = 1;
    while (q.empty() == false) {
        TNode* cur = q.front();
        q.pop();
        for (int i = 0; i < 2; ++i) {
            if (cur_index < tree.size()) {
                if (tree[cur_index] != "#") {
                    int digit = string2int(tree[cur_index]);
                    if (i == 0) {
                         cur->left = new TNode(digit);
                         q.push(cur->left);
                    } else {
                         cur->right = new TNode(digit);
                         q.push(cur->right);
                    }
                }
                 cur_index += 1;
            }

        }
    }
    return root;
}
}
int main() {
    int n = 0;
    cin>>n;
    vector<string> tree;
    for (int i = 0; i < n; ++i) {
        string s;
        cin>>s;
        tree.push_back(s);
    }
    tree::TNode* root = tree::create_tree(tree);
    tree::pre_order(root);    
    cout<<endl;
    tree::pre_order_iter(root);    
    cout<<endl;
    tree::post_order(root);    
    cout<<endl;
    tree::post_order_iter(root);    
    cout<<endl;
    tree::in_order(root);
    cout<<endl;
    tree::in_order_iter(root);
    cout<<endl;
    vector<tree::TNode*> root_vec;
    root_vec.push_back(root);
    tree::level_order_visit(root_vec);
    cout<<endl;
    tree::level_order_visit_iter(root);
    cout<<endl;
    return 0;
}
点赞