手撸二叉树三种遍历,递归以及非递归版本。

突然晚上想到二叉树的三种遍历写法,老夫就装大一小朋友写一写二叉树的递归和非递归版的前、中、后序遍历。

1、二叉树定义

struct node{
    char ch;
    
    node* leftchild;
    node* rightchild;

};

2、二叉树建立

按前序输入构建一棵二叉树,字符‘#’表示空子树

void createTree(node &T){

    char str;
    cin >> str;
    if(str == '#'){
        T = NULL;
    }
    else{
        T = new node;
        T->ch = str;
        createTree(T->leftchild);
        createTree(T0>rightchild); 
    }
    
}

3、二叉树的深度遍历

(1)前序遍历

          递归版本:

void PreVisit(node* T){

    if(T){
        cout << T->ch << endl;
        PreVisit(T->leftchild);
        PreVisit(T->rightchild);
    }

}

          非递归版本:

void PreVisit(node* T){

    if(T) return;
    node* cur = T;
    stack<node*> s;
    s.push(cur);

    while(!s.empty()){
        cur = s.top();
        cout << cur.ch;
        s.pop();
        if(cur->leftchild != NULL)
            s.push(cur->leftchild);
        if(cur->rightchild != NULL)
            s.push(cur->rightchild);
    }

}

(2)中序遍历

          递归版本:

void PreVisit(node* T){

    if(T){
        PreVisit(T->leftchild);
        cout << T->ch << endl;
        PreVisit(T->rightchild);
    }

}

          非递归版本:

void MidVisit(node* T){

    if(T==NULL)
        return;
    
    node* cur = T;
    stack<node*> s;
    s.push(cur);

    while(!s.empty()){

        while(s.top()->leftchild != NULL)
            s.push(s.top()->leftchild);

        while(!s.empty()){
            node* cur = s.top();
            cout << cur->ch;
            s.pop();
            if(s.rightchild != NULL){
                s.push(s.rightchild);
                break;
            }

        }
    }
}

(3)后序遍历

          递归版本:

void PreVisit(node* T){

    if(T){
        PreVisit(T->leftchild);
        PreVisit(T->rightchild);
        cout << T->ch << endl;
    }

}

          非递归版本:

void PostVisit(node* T){
    
    if(T == NULL)
        return;

    stack<node*> s;
    s.push(T);
    node* lastpop = NULL;

    while(!s.empty()){
        while(s.top()->leftchild != NULL)
            s.push(s.top()->leftchild;

        while(!s.empty()){

            if(lastpop == s.top()->rightchild || s.top()->rightchild == NULL){
                cout << s.top()->ch<<endl;
                lastpop = s.top();
                s.pop();
            }
            else if(s.top()->rightchild != NULL){
                s.push(s.top()->rightchild);
                break;
            }
        }
    }
}

4、二叉树层次遍历——用队列实现

void LevelVisit(node* T){

    if(T == NULL)
        return;

    queue<node*> q;
    q.push(T);

    while(!q.empty()){
        cout << q.front()->ch<<endl;
        if(q.front()->leftchild != NULL)
            q.push(q.front()->leftchild);
        if(q.front()->rightchild != NULL)
            q.push(q.front()->rightchild);
        q.pop();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值