树的遍历 ALDS1_7_C: Tree Walk

来源:互联网 发布:mac os x怎么更新 编辑:程序博客网 时间:2024/05/29 07:10

递归版

#include <cstdio>#define MAX 10000#define NIL -1using namespace std;struct node{    int parent;    int left;    int right;};node binaryTree[MAX];int n;//前序遍历void preParse(int u){    if(u == NIL)        return;    printf(" %d",u);    preParse(binaryTree[u].left);    preParse(binaryTree[u].right);}//中序遍历void inParse(int u){    if(u==NIL)        return;    inParse(binaryTree[u].left);    printf(" %d",u);    inParse(binaryTree[u].right);}//后序遍历void postParse(int u){    if(u==NIL)        return;    postParse(binaryTree[u].left);    postParse(binaryTree[u].right);    printf(" %d",u);}//层次遍历void levelOrder(){    queue<node*> q;    q.push(root);    node* tem;    while(!q.empty()){        tem=q.front();        printf("%d ",tem->key);        q.pop();        if(tem->left)            q.push(tem->left);        if(tem->right)            q.push(tem->right);    }}int main(){    int i;    int value;    int left;    int right;    int root;    scanf("%d",&n);    for(i=0;i<n;i++)        binaryTree[i].parent=NIL;    for(i=0;i<n;i++){        scanf("%d %d %d",&value,&left,&right);        binaryTree[value].left=left;        binaryTree[value].right=right;        if(left!=NIL)            binaryTree[left].parent=value;        if(right!=NIL)            binaryTree[right].parent=value;    }    for(i=0;i<n;i++){        if(binaryTree[i].parent==NIL)            root=i;    }    printf("Preorder\n");    preParse(root);    printf("\n");    printf("Inorder\n");    inParse(root);    printf("\n");    printf("Postorder\n");    postParse(root);    printf("\n");}

非递归版

inline void preorder(node* p){    stack<node*> w;    do{        while(p){            printf("%d ",p->key);            w.push(p);            p=p->left;        }        if(!w.empty()){            p=w.top();            w.pop();            p=p->right;        }    }while(p||!w.empty());}inline void inorder(node* p){    stack<node*> w;    do{        while(p){            w.push(p);            p=p->left;        }        if(!w.empty()){            p=w.top();            printf("%d ",p->key);            w.pop();            p=p->right;        }    }while(p||!w.empty());}//只能遍历一次,不推荐使用inline void postorder(node* p){    stack<node*> w;    do{        while(p){            w.push(p);            p=p->left;        }        if(!w.empty()){            p=w.top();            if(p->right&&!p->right->visited)                p=p->right;            else{                printf("%d ",p->key);                p->visited=true;                w.pop();                p=NULL;            }        }    }while(p||!w.empty());}//推荐使用inline void postorder(node* p){    stack<node*> w;    node* pre=NULL;    w.push(p);    while(!w.empty()){        p=w.top();        if(p){            if((!p->left)&&(!p->right)||(pre&&(pre==p->left||pre==p->right))){                printf("%d ",p->key);                w.pop();                pre=p;            }            else{                if(p->right)                    w.push(p->right);                if(p->left)                    w.push(p->left);            }        }    }}
原创粉丝点击