二叉树遍历

本文介绍了一种使用C++实现二叉树的递归与非递归遍历的方法,并提供了一个具体的例子来演示如何创建一棵二叉树并进行不同类型的遍历。包括先序、中序和后序遍历,每种遍历都提供了递归和非递归两种实现方式。
const int PREORDER=0;
const int INEORDER=1;
const int POSTEORDER=2;
const bool RECURSIVE=true;
const bool NONRECURSIVE=false;

class MyBinaryTree{
public:
    class Node{
    public:
        char data;
        Node *lchild,*rchild,*parent;
        Node(char c){
            data = c;
            lchild = rchild = parent = NULL;
        }
    };

    class MyStack{
        vector<Node *> st;
        int pt;
    public:
        MyStack(){
            st = *(new vector<Node *>());
            pt = -1;
        }

        void pop(){
            if(pt==-1) return;
            st.pop_back();
            pt--;            
        }

        void push(Node *p){
            pt++;
            st.push_back(p);
        }

        Node * top(){
            if(pt==-1)return NULL;
            else return st[pt];
        }

        bool isEmpty(){
            if(pt==-1)return true;
            else return false;
        }
        
    };

    MyBinaryTree(char * str,int n){
        root = NULL;
        root = createTree(str,n,1);
    }

    void traverse(int order,bool recursive){
        if(recursive){
            switch (order){
                case PREORDER: 
                    cout<<endl<<"此乃递归先序"<<endl;
                    recPreorder(root);
                    break;
                case INEORDER:
                    cout<<endl<<"此乃递归中序"<<endl;
                    recInorder(root);
                    break;
                case POSTEORDER: 
                    cout<<endl<<"此乃递归后序"<<endl;
                    recPostorder(root);
                    break;
                default:
                    cout<<endl<<"此乃递归先序"<<endl;
                    recPreorder(root);
                break;
            }
        }else{
            switch (order){
                case PREORDER: 
                    cout<<endl<<"此乃非递归先序"<<endl;
                    nonRecPreorder(root);
                    break;
                case INEORDER:
                    cout<<endl<<"此乃非递归中序"<<endl;
                    nonRecInorder(root);
                    break;
                case POSTEORDER: 
                    cout<<endl<<"此乃非递归后序"<<endl;
                    nonRecPostorder(root);
                    break;
                default:
                    cout<<endl<<"此乃非递归先序"<<endl;
                    nonRecPreorder(root);
                break;
            }        
        }
    }

    void test(){
        
    }

    ~MyBinaryTree(){
        if(root)deleteTree(root);
    }
private:
    Node * root;
    Node * createTree(char * str,int n,int i){
        if(i<=n){
            if(str[i]=='?')return NULL;
            Node * cur =  new Node(str[i]);
            if(!root)root=cur;
            cur->lchild = createTree(str,n,2*i);
            if(cur->lchild)cur->lchild->parent = cur;
            cur->rchild = createTree(str,n,2*i+1);
            if(cur->rchild)cur->rchild->parent = cur;
            return cur;
        }else{
            return NULL;
        }
    }

    void deleteTree(Node * r){
        if(r){
            deleteTree(r->lchild);
            deleteTree(r->rchild);
            delete r;
        }
    }
    void recPreorder(Node * r){
        if(r){
            cout<<r->data<<"  ";
            recPreorder(r->lchild);
            recPreorder(r->rchild);
        }
    }

    void recInorder(Node * r){
        if(r){
            recInorder(r->lchild);
            cout<<r->data<<"  ";
            recInorder(r->rchild);
        }    
    }

    void recPostorder(Node * r){
        if(r){
            recPostorder(r->lchild);
            recPostorder(r->rchild);
            cout<<r->data<<"  ";
        }    
    }

    void nonRecPreorder(Node * r){
        Node * p=r;
        MyStack st;
        while(p || !st.isEmpty()){
            while(p){
                cout<<p->data<<"  ";
                st.push(p);
                p=p->lchild;
            }
            if(!st.isEmpty()){
                p=st.top();
                st.pop();
                p=p->rchild;
            }
        }
    }

    void nonRecInorder(Node * r){
        Node * p =r;
        MyStack st;
        while(p || !st.isEmpty()){
            while(p){
                st.push(p);
                p=p->lchild;
            }
            while(!st.isEmpty()){
                p = st.top();
                cout<<p->data<<"  ";
                p=p->rchild;
                st.pop();
                if(p){
                    break;
                }
            }
        }
    }

    void nonRecPostorder(Node * r){
        Node *p = r,*pre =NULL;
        MyStack st;
        st.push(p);
        while(!st.isEmpty()){
            p=st.top();
            if((p->lchild==NULL && p->rchild==NULL) || (pre!=NULL && (pre==p->lchild || pre==p->rchild))){
                cout<<p->data<<"  ";
                st.pop();
                pre = p;
            }else{
                if(p->rchild)st.push(p->rchild);
                if(p->lchild)st.push(p->lchild);
            }
        }    
    }
};


void getRandom(){
      srand((int)time(0));
      cout<<rand();
}

int main(){
    char * a = "?ABCDEFGH?IJ?????????K";
    int n = 21;
    MyBinaryTree * bt = new MyBinaryTree(a,n);
    bt->traverse(POSTEORDER,RECURSIVE);
    bt->traverse(POSTEORDER,NONRECURSIVE);
    bt->test();
    delete bt;
    cout<<"(ˉ▽ ̄~) ~~";

}

 

转载于:https://www.cnblogs.com/kateblog/p/5512049.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值