码迷,mamicode.com
首页 > 编程语言 > 详细

第4章第1节练习题9 反向层次遍历算法

时间:2016-08-19 08:39:10      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:

问题描述

试给出二叉树的自下而上,从右到左的层次遍历算法

算法思想

一般的二叉树层次遍历算法是自上向下,从左到右的遍历,这里的遍历顺序恰好相反,而运用栈先进后出的特点实现顺序的反转。
因此可以考虑在原来的层次遍历算法的基础上加上栈的基本操作。在出队的同时将各节点入栈,在所有的节点全部入栈后,依次出栈访问便可解决该问题。

算法描述

void InLevelOrder(BiTNode* T){
    BiTNode *p=T;
    SqQueue Q;
    InitQueue(&Q);
    SqStack S;
    InitStack(&S);
    EnQueue(&Q,T);  
    while(IsEmptyQueue(&Q)!=0){
        p=DeQueue(&Q);
        Push(&S,p);
        if(p->lchild!=NULL){
            EnQueue(&Q,p->lchild);
        }
        if(p->rchild!=NULL){
            EnQueue(&Q,p->rchild);
        }
    }
    //将所有节点出栈访问
    while(IsEmptyStack(&S)!=0){
        p=Pop(&S);
        printf("%c",p->data);
    }
    printf("\n");
}

具体代码见附件。


附件

//AB#DG###CE##F##
#include<stdio.h>
#include<stdlib.h>

#define MaxSize 20
typedef char ElemType;

//------------------------------------------

typedef struct BiTNode{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;

BiTree CreatBiTree(BiTNode*);
void InLevelOrder(BiTNode*);

//------------------------------------------

typedef struct{
    BiTNode* data[MaxSize];
    int front, rear;
}SqQueue;

void InitQueue(SqQueue*);
void EnQueue(SqQueue*, BiTNode*);
BiTNode* DeQueue(SqQueue*);
int IsEmptyQueue(SqQueue*);

//------------------------------------------

typedef struct{
    BiTNode* data[MaxSize];
    int top;
}SqStack;

void InitStack(SqStack*);
void Push(SqStack*,BiTNode*);
BiTNode* Pop(SqStack*);
int IsEmptyStack(SqStack*);

//------------------------------------------

int main(int argc, char* argv[]){
    BiTNode* T=(BiTNode*)malloc(sizeof(BiTNode));
    T=CreatBiTree(T);
    InLevelOrder(T);
    return 0;
}

BiTNode* CreatBiTree(BiTNode* T){
    ElemType x;
    scanf("%c",&x);
    if(x==‘#‘){
        return T;
    }
    T=(BiTNode*)malloc(sizeof(BiTNode));
    T->data=x;
    T->lchild=CreatBiTree(T->lchild);
    T->rchild=CreatBiTree(T->rchild);
    return T;
}

//------------------------------------------

void InLevelOrder(BiTNode* T){
    BiTNode *p=T;
    SqQueue Q;
    InitQueue(&Q);
    SqStack S;
    InitStack(&S);
    EnQueue(&Q,T);  
    while(IsEmptyQueue(&Q)!=0){
        p=DeQueue(&Q);
        Push(&S,p);
        if(p->lchild!=NULL){
            EnQueue(&Q,p->lchild);
        }
        if(p->rchild!=NULL){
            EnQueue(&Q,p->rchild);
        }
    }
    while(IsEmptyStack(&S)!=0){
        p=Pop(&S);
        printf("%c",p->data);
    }
    printf("\n");
}

//------------------------------------------

void InitQueue(SqQueue* Q){
    Q->front=0;
    Q->rear=0;
}

void EnQueue(SqQueue* Q, BiTNode* T){
    if((Q->rear+1)%MaxSize==Q->front){
        return;
    }
    Q->data[Q->rear++]=T;
}

BiTNode* DeQueue(SqQueue* Q){
    if(Q->front==Q->rear){
        return NULL;
    }
    return Q->data[Q->front++];
}

int IsEmptyQueue(SqQueue* Q){
    if(Q->front==Q->rear){
        return 0;
    }
    return -1;
}

//------------------------------------------

void InitStack(SqStack* S){
    S->top=-1;
}

void Push(SqStack* S, BiTNode* T){
    if(S->top==MaxSize-1){
        return;
    }
    S->data[++S->top]=T;
}

BiTNode* Pop(SqStack* S){
    if(S->top==-1){
        return NULL;
    }
    return S->data[S->top--];
}

int IsEmptyStack(SqStack* S){
    if(S->top==-1){
        return 0;
    }
    return -1;
}

第4章第1节练习题9 反向层次遍历算法

标签:

原文地址:http://blog.csdn.net/u013595419/article/details/52247225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!