您的位置:首页 > 其它

实验报告 二叉树

2016-11-30 18:44 295 查看
一、二叉树的遍历

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 20
#define OK 1
#define ERROR 0
#define NULL 0
#define OVERFLOW 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree CreateBiTree(BiTree T)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
T = NULL;
else
{
T = (BiTNode *)malloc(sizeof(BiTNode));
if (!T)
return (OVERFLOW);
T->data = ch;
T->lchild = CreateBiTree(T->lchild);
T->rchild = CreateBiTree(T->rchild);
}
return T;
}

void LevelOrder(BiTree T)
{
BiTree Queue[MAX], b;
int front, rear;
front = rear = 0;
if (T)
{
Queue[rear++] = T;
while (front != rear)
{
b = Queue[front++];
printf("%2c", b->data);
if (b->lchild != NULL)
Queue[rear++] = b->lchild;
if (b->rchild != NULL)
Queue[rear++] = b->rchild;
}
}
}

void PreOrder(BiTree T)
{
if (T)
{
printf("%2c", T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}

void InOrder(BiTree T)
{
if (T)
{
PreOrder(T->lchild);
printf("%2c", T->data);
PreOrder(T->rchild);
}
}

void PostOrder(BiTree T)
{
if (T)
{
PreOrder(T->lchild);
PreOrder(T->rchild);
printf("%2c", T->data);
}
}

int main()
{
BiTree B;
printf("\nInput a sequence of char:");
B = CreateBiTree(B);

printf("\nPreOrder:");
PreOrder(B);
printf("\nInOrder:");
InOrder(B);
printf("\nPostOrder:");
PostOrder(B);
printf("\nLevelOrder:");
LevelOrder(B);

return 0;
}


 

二、二叉树的统计

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 20
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

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

void LevelOrder(BiTree T)
{
BiTree Queue[MAX], b;
int front, rear;
front = rear = 0;
if (T)
{
Queue[rear++] = T;
while (front != rear)
{
b = Queue[front++];
printf("%2c", b->data);
if (b->lchild != NULL)
Queue[rear++] = b->lchild;
if (b->rchild != NULL)
Queue[rear++] = b->rchild;
}
}
}

int leafcount(BiTree T)
{
if (T->lchild == NULL && T->rchild == NULL)
return 1;
if (T->lchild == NULL)
return leafcount(T->rchild);
if (T->rchild == NULL)
return leafcount(T->lchild);
return leafcount(T->lchild) + leafcount(T->rchild);
}

int depth(BiTree T)
{
if (T->lchild == NULL && T->rchild == NULL)
return 1;
int dep1 = 0;
if (T->rchild != NULL)
dep1 = depth(T->rchild);
int dep2 = 0;
if (T->lchild != NULL)
dep2 = depth(T->lchild);
return dep1 > dep2 ? (dep1 + 1) : (dep2 + 1);
}

int main()
{
BiTree T = NULL;
int m;
printf("\n请输入二叉树的结点序列:");
T = CreateBiTree(T);
m = leafcount(T);
printf("二叉树的叶子数量: %d\n", m);
printf("深度为: %d\n", depth(T));
LevelOrder(T);
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WLDXZY