这是数据结构里面的二叉树的创建和遍历,也是按照书本的思路打的,如果大家对于我的代码有什么好的建议,可以留言告诉我,或者如果你也是大一学这个,可以参考一下我写的。
#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 10000
typedef struct TNode *Position;
typedef Position BinTree;
typedef Position Queue;
struct TNode
{
int date;
Position DL[10000];
BinTree left, right;
int rear, front;
int MaxSize;
};
Queue CreateQueue(int MaxSize); //创建堆栈(用于记录位置)
BinTree CreateBinTree(); //创建二叉树
bool AddQ(Queue Q, BinTree BT);
BinTree DeleteQ(Queue Q); //记录上一个结点位置
bool Isempty(Queue Q);
int GetHeight(BinTree BT); //计算高度
void PreorderPrintLeaves(BinTree BT); //叶节点
void PreorderTraversal(BinTree BT); //先序遍历
void InorderTraversal(BinTree BT); //中序遍历
void PostorderTraversal(BinTree BT); //后续遍历
void LevelorderTraversal( BinTree BT );
void display();
int main()
{
int i=1, c, n=-1;
BinTree T;
display();
while(1)
{
if(n==0)
continue;
//system("pause"); 在这里暂停
scanf("%d", &n);
if( n==5 )
break;
switch(n)
{
case 1:
{
printf("输入树的表,结点下的左或右有空位就用'0'表示, 输入完后在末尾加6:");
T = CreateBinTree();
if(T)
{
printf("\n创建成功!\n");
c = 1;
i++; //创建成功条件
}
else
{
c=0;
printf("\n创建失败!\n");
continue;
}
}break;
case 2:
{
if(c==1)
{
printf("\n先序遍历输出为:");
PreorderTraversal(T);
printf("\n\n中序遍历输出为:");
InorderTraversal(T);
printf("\n\n后序遍历输出为:");
PostorderTraversal(T);
printf("\n\n层序遍历输出为:");
LevelorderTraversal(T);
}
else
{
printf("\n还未创建新的树, 请重新输入:\n");
continue;
}
}break;
case 3:
{
if(c==1)
printf("树的高度为:%d\n", GetHeight(T));
else
{
printf("\n还未创建新的树, 请重新输入:\n");
continue;
}
}break;
case 4:
{
if(c==1)
{
printf("树的所有叶节点是:");
PreorderPrintLeaves(T);
}
else
{
printf("\n还未创建新的树, 请重新输入:\n");
continue;
}
}break;
default:
{
printf("数字无效,重新输入:");
continue;
}break;
}
display();
}
return 0;
}
Queue CreateQueue(int MaxSize)
{
Queue Q = (Queue)malloc(sizeof(struct TNode));
Q->rear = Q->front = 0;
Q->MaxSize = MaxSize;
return Q;
}
BinTree CreateBinTree()//二叉树创建
{
char DATE;
BinTree BT, T;
Queue Q = CreateQueue(MAXSIZE);
// getchar();
scanf("\n%c", &DATE);
if(DATE != '0')
{
BT = (BinTree)malloc(sizeof(struct TNode));
BT->date = DATE;
BT->left = BT->right = NULL;
AddQ(Q, BT);
}
else return NULL;
while(!Isempty(Q))
{
T = DeleteQ(Q);
scanf("%c", &DATE);
if(DATE=='\n'||DATE==6)
break;
if(DATE=='0') T->left = NULL;
else
{
T->left = (BinTree)malloc(sizeof(struct TNode));
T->left->date = DATE;
T->left->left = T->left->right = NULL;
AddQ(Q, T->left);
}
if(DATE=='\n'||DATE==6)
break;
scanf("%c", &DATE);
if(DATE=='0') T->right = NULL;
else
{
T->right = (BinTree)malloc(sizeof(struct TNode));
T->right->date = DATE;
T->right->left = T->right->right = NULL;
AddQ(Q, T->right);
}
}
return BT;
}
bool AddQ(Queue Q, BinTree BT)
{
Q->rear=(Q->rear+1) % Q->MaxSize;
Q->DL[Q->rear] = BT;
}
BinTree DeleteQ(Queue Q)
{
Q->front = (Q->front+1) % Q->MaxSize;
return Q->DL[Q->front];
}
bool Isempty(Queue Q)
{
return (Q->rear==Q->front);
}
int GetHeight(BinTree BT)//计算树的高度
{
int HL, HR, MaxH;
if(BT)
{
HL = GetHeight(BT->left);
HR = GetHeight(BT->right);
MaxH = HL>HR ? HL:HR;
return ( MaxH + 1 );
}
else
return 0;
}
void PreorderPrintLeaves(BinTree BT)//显示叶节点
{
if(BT)
{
if( !BT->left && !BT->right )
printf("%c ", BT->date);
PreorderPrintLeaves(BT->left);
PreorderPrintLeaves(BT->right);
}
}
void PreorderTraversal(BinTree BT)//先序遍历
{
if(BT)
{
printf(" %c", BT->date);
PreorderTraversal(BT->left);
PreorderTraversal(BT->right);
}
}
void InorderTraversal(BinTree BT)//中序遍历
{
if(BT)
{
InorderTraversal(BT->left);
printf(" %c", BT->date);
InorderTraversal(BT->right);
}
}
void PostorderTraversal(BinTree BT)//后续遍历
{
if(BT)
{
PostorderTraversal(BT->left);
PostorderTraversal(BT->right);
printf(" %c", BT->date);
}
}
void LevelorderTraversal( BinTree BT )
{
BinTree Queue[1001], T;
int i=0, j=0;
Queue[i++] = BT;
while(j<i)
{
T = Queue[j++];
printf("%c ", T->date);
if(T->left)
Queue[i++] = T->left;
if(T->right)
Queue[i++] = T->right;
}
}
void display() //显示菜单
{
printf("\n\n");
printf(" |.............二叉树创建..............|\n");
printf(" | 1.创建 二叉树 |\n");
printf(" | 2.输出 二叉树 |\n");
printf(" | 3.显示树的高度 |\n");
printf(" | 4.显示所有叶结 |\n");
printf(" | 5.结 束 |\n");
printf(" |.....................................|\n");
printf("\n\n");
printf("选择菜单:");
}