逐层打印二叉树节点数据

来源:互联网 发布:js button disabled 编辑:程序博客网 时间:2024/05/16 14:10
/************************************************************************/
/*
/* example input: ABD###CE#F###
/* bi-tree built:
/*                 A
/*                / \
/*               B   C
/*              /   /
/*             D   E
/*                  \
/*                   F
/* 


/* level traverse: A B C D E F
/*
/************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <process.h>


#define OK        1
#define ERROR     0
#define OVERFLOW  -2


typedef int Status;
typedef int TElemType;


/* 二叉树的链式存储结构 */
typedef struct BiTNode {
    TElemType data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;


typedef BiTNode* QElemType;


/* 单链队列的存储结构 */
typedef struct QNode{
    QElemType data;
    struct QNode *next;
} QNode, *QueuePtr;


typedef struct {
    QueuePtr front;
    QueuePtr rear;
} LinkQueue;


/* 树的操作函数 */
Status PrintElement(TElemType e);
BiTree CreateBiTree(BiTree T);
Status LevelTraverse(BiTree T, Status (*Visit)(TElemType e));


/* 队列操作函数 */
Status InitQueue(LinkQueue &Q);
Status QueueEmpty(LinkQueue Q);
Status EnQueue(LinkQueue &Q, QElemType e);
Status DeQueue(LinkQueue &Q, QElemType &e);


BiTree T = NULL;


void main() 
{
    T = CreateBiTree(T);
    printf("\nlevel traverse: ");
    LevelTraverse(T, PrintElement);



Status PrintElement(TElemType e)
{
    printf("%c ", e);
    return OK;
}


BiTree CreateBiTree(BiTree T)
{
    char ch;
    ch = getchar();
    if('#' == ch)
        T = NULL;
    else {
        if(!(T = (BiTNode *)malloc(sizeof(BiTNode))))
            exit(OVERFLOW);
        T->data = ch;
        T->lchild = CreateBiTree(T->lchild);
        T->rchild = CreateBiTree(T->rchild);
    }
    return T;
}


Status LevelTraverse(BiTree T, Status (*Visit)(TElemType))
{
    LinkQueue Q; QElemType e;
    InitQueue(Q);
    EnQueue(Q, T);
    while (!QueueEmpty(Q)) {
        DeQueue(Q, e);
        if (e) {
            Visit(e->data);
            EnQueue(Q, e->lchild);
            EnQueue(Q, e->rchild);
        }
    }
    return OK;
}


Status InitQueue(LinkQueue &Q) {
    //构造一个空队列Q
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    if (!Q.front) exit(OVERFLOW);
    Q.front->next = NULL;
    return OK;
}


Status DestroyQueue(LinkQueue &Q) {
    //销毁队列Q
    while (Q.front) {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}


Status QueueEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear && Q.front->next == NULL) return true;
    else return false;
}


Status EnQueue(LinkQueue &Q, QElemType e) {
    //插入元素e为Q的新的队尾元素
    struct QNode *p = (QueuePtr)malloc(sizeof(QNode));
    if (!p) exit(OVERFLOW);
    p->data = e; p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return OK;
}


Status DeQueue(LinkQueue &Q, QElemType &e) {
    //若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK;
    //否则返回ERROR
    struct QNode *p = NULL;
    if (Q.front == Q.rear) return ERROR;
    p = Q.front->next;
    e = p->data;
    Q.front->next = p->next;
    if (Q.rear == p) Q.rear = Q.front;
    free(p);
    return OK;
}
0 0