各種二叉樹遍歷

C++實(shí)現(xiàn),二叉樹的遞歸、非遞歸版本的前序、中序、后序遍歷。

// 二叉樹結(jié)點(diǎn)
struct Node{
    int val;
    Node *left,*right;
    Node():left(nullptr),right(nullptr){}
    Node(int _v):val(_v),left(nullptr),right(nullptr){}
};

// 二叉搜索樹
struct Tree{
    Node *root; //根節(jié)點(diǎn)
    std::vector<int> p; //存儲遍歷的結(jié)果

    Tree():root(nullptr){}
    ~Tree(){
        release(root);
    }

    // 輸出p數(shù)組的內(nèi)容
    void print(){
        for(size_t i=0;i<p.size();++i){
            std::cout<<p[i]<<" ";
        }
        std::cout<<std::endl;
    }

    // 插入節(jié)點(diǎn)
    void insert(int val){
        if(root==nullptr){
            root = new Node(val);
            return ;
        }
        Node *cur = root;
        while(true){
            if(val<=cur->val){
                if(cur->left){
                    cur = cur->left;
                }else{
                    cur->left = new Node(val);
                    return ;
                }
            }else{
                if(cur->right){
                    cur = cur->right;
                }else{
                    cur->right = new Node(val);
                    return ;
                }
            }
        }
    }

    // 三種遞歸遍歷方式
    void _rpreorder(Node *r){
        if(r){
            p.push_back(r->val);
            _rpreorder(r->left);
            _rpreorder(r->right);
        }
    }
    void rpreorder(){
        p.clear();
        _rpreorder(root);
    }

    void _rinorder(Node *r){
        if(r){
            _rinorder(r->left);
            p.push_back(r->val);            
            _rinorder(r->right);
        }
    }
    void rinorder(){
        p.clear();
        _rinorder(root);
    }

    void _rpostorder(Node *r){
        if(r){
            _rpostorder(r->left);
            _rpostorder(r->right);
            p.push_back(r->val);            
        }
    }
    void rpostorder(){
        p.clear();
        _rpostorder(root);
    }

    // 三種非遞歸遍歷方式
    void preorder(){
        p.clear();
        if(root==nullptr) return ;
        std::stack<Node*> st;
        Node *cur = root;

        while(cur || !st.empty()){
            while(cur){
                p.push_back(cur->val);
                st.push(cur);
                cur = cur->left;
            }
            if(!st.empty()){
                cur = st.top();
                st.pop();
                cur = cur->right;
            }
        }
    }

    void inorder(){
        p.clear();
        if(root==nullptr) return ;
        std::stack<Node*> st;
        Node *cur = root;
        while(cur || !st.empty()){
            while(cur){
                st.push(cur);
                cur = cur->left;
            }
            if(!st.empty()){
                cur = st.top();
                st.pop();
                p.push_back(cur->val);
                cur = cur->right;
            }
        }
    }

    void postorder(){
        p.clear();
        if(root==nullptr) return ;
        Node *cur = root;
        Node *last = nullptr; //上一個(gè)輸出的節(jié)點(diǎn)
        std::stack<Node*> st;

        while(cur || !st.empty()){
            while(cur){
                st.push(cur);
                cur = cur->left;
            }
            cur = st.top();
            if(cur->right==nullptr || cur->right==last){
                p.push_back(cur->val);
                st.pop();
                last = cur;
                cur = nullptr;
            }else{
                cur = cur->right;
            }
        }

    }
    void release(Node *r){
        if(r==nullptr) return ;
        if(r->left) release(r->left);
        if(r->right) release(r->right);
        delete r;
        r = nullptr;
    }
};

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容