zoukankan      html  css  js  c++  java
  • 二叉树的遍历(前、中、后序及层次遍历,递归和非递归实现)

    一、二叉树的前序遍历:

    //递归前序遍历 
    void preorder(BTNode *root){
        if(root){
            printf("%c ",root->data);
            preorder(root->lchild);
            preorder(root->rchild);
        }
    }
    
    //非递归前序遍历 
    void pre(BTNode *root){
        stack <BTNode*>s;    
        if(root)
            s.push(root);
        BTNode *p;
        while(!s.empty()){
            p=s.top();
            s.pop();
            printf("%c ",p->data);
            if(p->rchild!=NULL)
                s.push(p->rchild);
            if(p->lchild!=NULL)
                s.push(p->lchild);
        }
    }

    二、二叉树的中序遍历:

    //递归中序遍历 
    void inorder(BTNode *root){
        if(root){
            inorder(root->lchild);
            printf("%c ",root->data);
            inorder(root->rchild);
        }
    }
    
    //非递归中序遍历
    void in(BTNode *root){
        stack<BTNode*> s;
        if(root){
            BTNode *p=root;
            while(!s.empty()||p){
                while(p){
                    s.push(p);
                    p=p->lchild;
                }
                if(!s.empty()){
                    p=s.top();
                    s.pop();
                    printf("%c ",p->data);
                    p=p->rchild;                
                }
            }
        }
    }

    三、二叉树的后序遍历:

    //递归后序遍历
    void postorder(BTNode *root){
        if(root!=NULL){
            postorder(root->lchild);
            postorder(root->rchild);
            printf("%c ",root->data);
        }
    } 
    
    //非递归后序遍历
    void post(BTNode *root){
        stack<BTNode*> s;
        if(root!=NULL){
            BTNode *p=root;
            do{
                while(p!=NULL){
                    s.push(p);
                    p=p->lchild;
                }
                int flag=1;
                BTNode *q=NULL;
                while(!s.empty()&&flag){
                    p=s.top();
                    if(p->rchild==q){
                        printf("%c ",p->data);
                        s.pop();
                        q=p;
                    }else{
                        p=p->rchild;
                        flag=0;                    
                    }
                }
            }while(!s.empty());
            printf("
    ");
        }
    } 

    四、二叉树的层次遍历:

    //层次遍历 
    void levelorder(BTNode *root){
        if(root!=NULL){
            queue<BTNode*> q;
            q.push(root);
            BTNode *p;
            while(!q.empty()){
                p=q.front();
                q.pop();
                printf("%c ",p->data);
                if(p->lchild!=NULL)
                    q.push(p->lchild);
                if(p->rchild!=NULL)
                    q.push(p->rchild);
            }
        }
    }
  • 相关阅读:
    python模块--time模块
    python模块--如何相互调用自己写的模块
    Animating Views Using Scenes and Transitions
    fragment 切换
    android textview 设置text 字体
    android intent 5.1
    android EditView ime
    animation of android (4)
    animation of android (3)
    animation of android (2)
  • 原文地址:https://www.cnblogs.com/hekuiFlye/p/9565904.html
Copyright © 2011-2022 走看看