二叉树非递归建立和层次遍历

要求编写建立二叉树的函数和层次遍历二叉树的函数

函数接口定义: BinTree CreateBinTree_NRecursion(); void LevelOrder(BinTree
bt);

BinTree CreateBinTree_NRecursion() 实现对二叉树的非递归建立 void
LevelOrder(BinTree bt) 层次遍历二叉树bt

#include <stdio.h>
#include <stdlib.h>

typedef char DataType;
typedef struct BTreeNode
{
    DataType data;
    struct BTreeNode *leftchild;
    struct BTreeNode *rightchild;
}BinTreeNode;
typedef BinTreeNode *BinTree;

typedef BinTreeNode *DataTypeQueue;
struct Node
{
    DataTypeQueue data;
    struct Node    *next;
};
typedef struct Node *PNode;
struct Queue
{
    PNode        f;
    PNode        r;
};
typedef struct Queue *LinkQueue;

LinkQueue  SetNullQueue_Link()
{
    LinkQueue lqueue;
    lqueue = (LinkQueue)malloc(sizeof(struct Queue));
    if (lqueue != NULL)
    {
        lqueue->f = NULL;
        lqueue->r = NULL;
    }
    else
        printf("Alloc failure! \n");
    return  lqueue;
}

int IsNullQueue_Link(LinkQueue lqueue)
{
    return (lqueue->f == NULL);
}

void EnQueue_link(LinkQueue lqueue, DataTypeQueue x)
{
    PNode  p;
    p = (PNode)malloc(sizeof(struct Node));
    if (p == NULL)
        printf("Alloc failure!");
    else{
        p->data = x;
        p->next = NULL;
        if (lqueue->f == NULL)
        {
            lqueue->f = p;
            lqueue->r = p;
        }
        else
        {
            lqueue->r->next = p;
            lqueue->r = p;
        }
    }
}
void DeQueue_link(LinkQueue lqueue)
{
    struct Node  * p;
    if (lqueue->f == NULL)
        printf("It is empty queue!\n ");
    else
    {
        p = lqueue->f;
        lqueue->f = lqueue->f->next;
        free(p);
    }
}
DataTypeQueue  FrontQueue_link(LinkQueue lqueue)
{
    if (lqueue->f == NULL)
    {
        printf("It is empty queue!\n");
        return 0;
    }
    else
        return (lqueue->f->data);
}
BinTree CreateBinTree_NRecursion()
{
    /* 请在这里填写答案 */
}
void LevelOrder(BinTree bt)
{
    /* 请在这里填写答案 */
}


int main()
{
    BinTreeNode *bt;
    bt = CreateBinTree_NRecursion();
    LevelOrder(bt);
    return 0;
}


非递归建立二叉树函数

BinTree CreateBinTree_NRecursion() {
    LinkQueue lqueue = SetNullQueue_Link();
    BinTree bt = NULL, p = NULL;
    DataType ch;
    int scanfResult;
    scanfResult = scanf(" %c", &ch);
    if (scanfResult == 1 && ch != '#' && ch != '@') {
        bt = (BinTreeNode *)malloc(sizeof(BinTreeNode));
        bt->data = ch;
        bt->leftchild = bt->rightchild = NULL;
        EnQueue_link(lqueue, bt);
    }
    while (!IsNullQueue_Link(lqueue)) //循环创建
    {
        p = FrontQueue_link(lqueue);
        DeQueue_link(lqueue);
        scanfResult = scanf(" %c", &ch);
        if (scanfResult == 1 && ch != '#' && ch != '@') //左
        {
            p->leftchild = (BinTreeNode *)malloc(sizeof(BinTreeNode));
            p->leftchild->data = ch;
            p->leftchild->leftchild = p->leftchild->rightchild = NULL;
            EnQueue_link(lqueue, p->leftchild);
        } else if (ch == '#' || ch == '@') 
        {
            p->leftchild = NULL;
        }
        scanfResult = scanf(" %c", &ch); 
        if (scanfResult == 1 && ch != '#' && ch != '@')//右
        {
            p->rightchild = (BinTreeNode *)malloc(sizeof(BinTreeNode));
            p->rightchild->data = ch;
            p->rightchild->leftchild = p->rightchild->rightchild = NULL;
            EnQueue_link(lqueue, p->rightchild);
        } else if (ch == '#' || ch == '@') {
            p->rightchild = NULL;
        }
    }

    return bt;
}

层次遍历

void LevelOrder(BinTree bt) {
    LinkQueue lqueue = SetNullQueue_Link();
    BinTree p;

    if (bt != NULL) {
        EnQueue_link(lqueue, bt);
    }
    while (!IsNullQueue_Link(lqueue)) 
    {
        p = FrontQueue_link(lqueue);
        DeQueue_link(lqueue);
        printf("%c ", p->data);
        if (p->leftchild != NULL) {
            EnQueue_link(lqueue, p->leftchild);
        }
        if (p->rightchild != NULL) {
            EnQueue_link(lqueue, p->rightchild);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值