二叉树的7种遍历(必背)

测试数据:天勤2019 142页

代码:

1~3:递归先序、中序、后序:

这三种没什么难度,放在这凑个数

void preOrder(Node* p){
    if(p){
        visit(p);
        preOrder(p->l);
        preOrder(p->r);
    }
}

void inOrder(Node* p){
    if(p){
        inOrder(p->l);
        visit(p);
        inOrder(p->r);
    }
}

void postOrder(Node* p){
    if(p){
        postOrder(p->l);
        postOrder(p->r);
        visit(p);
    }
}

非递归栈实现:

4.非递归先序:

void preOrderNonRecursion(Node* root){
    Node *stack[MaxSize];
    int top=-1;
    stack[++top]=root;
    while(top>=0){    //若栈非空,一直循环 
        Node *nd=stack[top--];
        visit(nd);
        if(nd->r)    //注意先右后左 
            stack[++top]=nd->r;
        if(nd->l)
            stack[++top]=nd->l;
    }
}

5.非递归中序:

void inOrderNonRecursion(Node* p){
    Node *stack[MaxSize];
    int top=-1;
    while(p || top>=0){    //只有当p节点为空,栈也为空的时候,退出循环 
        while(p) {
            stack[++top]=p;
            p=p->l;
        }
        if(top>=0){
            p=stack[top--];
            visit(p);
            p=p->r;
        }
    }
}

6.非递归后序:(硬编码非抽象实现的两个栈真的很麻瓜啊……)

void postOrderNonRecursion(Node* p){
    Node *stack1[MaxSize];
    Node *stack2[MaxSize];
    int top1=-1;
    int top2=-1;
    stack1[++top1]=p;
    while(top1>=0){    //若栈非空,一直循环 
        p=stack1[top1--];
        stack2[++top2] =p;
        if(p->l)    //注意先左后右 
            stack1[++top1]=p->l;
        if(p->r)
            stack1[++top1]=p->r;
    }
    while(top2>=0){
        p=stack2[top2--];
        visit(p);
    }
} 

 7.层序遍历:

void levelOrder(Node* root){
    Node* queue[MaxSize];
    int front=0,rear=0;
    rear=(rear+1)%MaxSize;
    queue[rear]=root;
    while(front!=rear){    //保证非空 
        front=(front+1)%MaxSize;
        Node* nd=queue[front];
        visit(nd);
        if(nd->l){
            rear=(rear+1)%MaxSize;
            queue[rear]=nd->l;
        }
        if(nd->r){
            rear=(rear+1)%MaxSize;
            queue[rear]=nd->r;
        }
    }
}

完整代码:

#include<iostream>
#define ElemType char
#define MaxSize 100

using namespace std;

typedef struct Node{
    struct Node* l=NULL; 
    struct Node* r=NULL; 
    ElemType data;
    Node(ElemType data=0):data(data){
    }
}Node;

Node* buildSampleTree(){//样例树形结构来自天勤 2019 版 142 页 
    Node* nd=new Node('A');
    nd->l= new Node('B');
    nd->r= new Node('G');
    nd->l->l= new Node('C');
    nd->l->r= new Node('D');
    nd->r->l= new Node('H');
    nd->l->r->l= new Node('E');
    nd->l->r->r= new Node('F');
    return nd;
}

void visit(Node* p){
    cout<<p->data<<' ';
}

void preOrder(Node* p){
    if(p){
        visit(p);
        preOrder(p->l);
        preOrder(p->r);
    }
}

void inOrder(Node* p){
    if(p){
        inOrder(p->l);
        visit(p);
        inOrder(p->r);
    }
}

void postOrder(Node* p){
    if(p){
        postOrder(p->l);
        postOrder(p->r);
        visit(p);
    }
}

void preOrderNonRecursion(Node* root){
    Node *stack[MaxSize];
    int top=-1;
    stack[++top]=root;
    while(top>=0){    //若栈非空,一直循环 
        Node *nd=stack[top--];
        visit(nd);
        if(nd->r)    //注意先右后左 
            stack[++top]=nd->r;
        if(nd->l)
            stack[++top]=nd->l;
    }
}

void inOrderNonRecursion(Node* p){
    Node *stack[MaxSize];
    int top=-1;
    while(p || top>=0){    //只有当p节点为空,栈也为空的时候,退出循环 
        while(p) {
            stack[++top]=p;
            p=p->l;
        }
        if(top>=0){
            p=stack[top--];
            visit(p);
            p=p->r;
        }
    }
}

void postOrderNonRecursion(Node* p){
    Node *stack1[MaxSize];
    Node *stack2[MaxSize];
    int top1=-1;
    int top2=-1;
    stack1[++top1]=p;
    while(top1>=0){    //若栈非空,一直循环 
        p=stack1[top1--];
        stack2[++top2] =p;
        if(p->l)    //注意先左后右 
            stack1[++top1]=p->l;
        if(p->r)
            stack1[++top1]=p->r;
    }
    while(top2>=0){
        p=stack2[top2--];
        visit(p);
    }
} 

void levelOrder(Node* root){
    Node* queue[MaxSize];
    int front=0,rear=0;
    rear=(rear+1)%MaxSize;
    queue[rear]=root;
    while(front!=rear){    //保证非空 
        front=(front+1)%MaxSize;
        Node* nd=queue[front];
        visit(nd);
        if(nd->l){
            rear=(rear+1)%MaxSize;
            queue[rear]=nd->l;
        }
        if(nd->r){
            rear=(rear+1)%MaxSize;
            queue[rear]=nd->r;
        }
    }
}

int main(){
    Node *root=buildSampleTree();
    //recursion 
    preOrder(root);
    puts("");
    inOrder(root);
    puts("");
    postOrder(root);
    puts("");
    //implement by stack    
    preOrderNonRecursion(root);
    puts("");
    inOrderNonRecursion(root);
    puts("");
    postOrderNonRecursion(root);
    puts("");
    levelOrder(root);
}
View Code

 

posted @ 2018-04-16 20:55  TQCAI  阅读(417)  评论(0编辑  收藏  举报