要求编写建立二叉树的函数和层次遍历二叉树的函数
函数接口定义: 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);
}
}
}