C++树的建立,前中后序层次遍历

//这个demo是测试树的建立,遍历(递归和非递归的前中后序遍历已经层次遍历)
#include<iostream>
#include<stack>//调用栈
#include<queue>
#include<vector>
using namespace std;

vector<int> prelist;
vector<int> inlist;
vector<int> postlist;

struct TreeNode{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x):val(x),left(NULL),right(NULL){}
};

int findA(vector<int> res,int val){
    for(int i=0;i<res.size();i++){
        if(res[i]==val)
            return i;
    }
    return -1;
}

void Test(TreeNode* &root,vector<int>& pre,vector<int>& ino,int lp,int rp,int li,int ri){
    if(lp>rp||li>ri)
        return ;
    root = new TreeNode(pre[lp]);
    int pos = findA(ino,pre[lp]);
    Test(root->left,pre,ino,lp+1,lp+pos-li,li,pos-1);
    Test(root->right,pre,ino,lp+pos-li+1,rp,pos+1,ri);
}

TreeNode* buildTree(vector<int>& pre,vector<int>& in){
    if(pre.size()<0)
        return NULL;
    TreeNode* root = NULL;
    Test(root,pre,in,0,pre.size()-1,0,in.size()-1);
    return root;
}

//下面这三块是递归遍历

void preOrder(TreeNode* root){
    if(root!=NULL){
        prelist.push_back(root->val);
        preOrder(root->left);
        preOrder(root->right);
    }
}

void inOrder(TreeNode* root){
    if(root!=NULL){
        inOrder(root->left);
        inlist.push_back(root->val);
        inOrder(root->right);
    }
}

void postOrder(TreeNode* root){
    if(root!=NULL){
        postOrder(root->left);
        postOrder(root->right);
        postlist.push_back(root->val);
    }
}

//下面三块是非递归操作(非递归是要借助栈来实现的)
vector<int> NoRecursionInPreOrder(TreeNode* root){
    vector<int> res;
    if(root==NULL)
        return res;
    stack<TreeNode*> s;
    TreeNode* p = root;
    while(!s.empty()||p){
        while(p){
            res.push_back(p->val);
            s.push(p);
            p = p->left;
        }
        p = s.top();
        s.pop();
        p = p->right;
    }
    return res;
}

vector<int> NoRecursionInInOrder(TreeNode* root){
    vector<int> res;
    if(root==NULL)
        return res;
    stack<TreeNode*> s;
    TreeNode* p = root;
    while(!s.empty()||p){
        while(p){
            s.push(p);
            p = p->left;
        }
        p = s.top();
        res.push_back(p->val);
        s.pop();
        p = p->right;
    }
    return res;
}

vector<int> NoRecursionInPostOrder(TreeNode* root){
    vector<int> res;
    if(root==NULL)
        return res;
    stack<TreeNode*> s;
    TreeNode* p = NULL;
    while(!s.empty()||root!=NULL){
        while(root!=NULL){
            s.push(root);
            root = root->left;
        }
        root = s.top();
        s.pop();
        if(root->right==NULL||root->right==p){
            res.push_back(root->val);
            p = root;
            root = NULL;
        }
        else{
            s.push(root);
            root = root->right;
        }
    }
    return res;
}

//下面这个是层次遍历
vector<vector<int>> levelOrder(TreeNode* root){
    vector<vector<int>> res;
    if(root==NULL)
        return res;
    queue<TreeNode*> q;
    q.push(root);
    while(!q.empty()){
        int n = q.size();
        vector<int> t;
        for(int i=0;i<n;i++){
            TreeNode* f = q.front();
            t.push_back(f->val);
            q.pop();
            if(f->left)
                q.push(f->left);
            if(f->right)
                q.push(f->right);
        }
        res.push_back(t);
    }
    return res;
}

int main(){
    vector<int> pre{3,9,20,15,7};
    vector<int> ino{9,3,15,20,7};
    TreeNode* root = NULL;
    root = buildTree(pre,ino);//用先序和中序建树
    
    //普通建树(暴力建树)
    TreeNode* root1 = new TreeNode(3);
    root1->left = new TreeNode(9);
    root1->right = new TreeNode(20);
    TreeNode* test = root1;
    test = test->right;
    test->left = new TreeNode(15);
//    test->right = new TreeNode(7);
    
    
    preOrder(root);
    inOrder(root);
    postOrder(root);
    cout<<"递归:"<<endl;
    cout<<"先序:";
    for(auto c:prelist)
        cout<<c<<" ";
    cout<<endl;
    cout<<"中序:";
    for(auto c:inlist)
        cout<<c<<" ";
    cout<<endl;
    cout<<"后序:";
    for(auto c:postlist)
        cout<<c<<" ";
    cout<<endl;
    
    cout<<"非递归:"<<endl;
    cout<<"先序:";
    for(auto c:NoRecursionInPreOrder(root))
        cout<<c<<" ";
    cout<<endl;
    cout<<"中序:";
    for(auto c:NoRecursionInInOrder(root))
        cout<<c<<" ";
    cout<<endl;
    cout<<"后序:";
    for(auto c:NoRecursionInPostOrder(root))
        cout<<c<<" ";
    cout<<endl;
    
    cout<<"层次遍历:"<<endl;
    vector<vector<int>> res = levelOrder(root1);
    for(int i=0;i<res.size();i++){
        for(int j=0;j<res[i].size();j++){
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值