#include<stdio.h>
#include<stdlib.h>
#define MAXNUM 20
int count=0;
typedef struct btnode
{
int data;
struct btnode *lchild,*rchild;
}BTNode,*tree;
typedef struct
{
tree data[MAXNUM+1];
int front,rear;
}Queue;
BTNode *p[MAXNUM+1];
int k=0;
BTNode* Creat_Bt(void);
void Preorder_mid(BTNode *bt); //中序
void Preorder_first(BTNode *bt); //先序
void Preorder_last(BTNode *bt); //后序
void Max_value(BTNode *bt);
void InitQueue(Queue &q);
int EmptyQueue(Queue &q);
void InsqQueue(Queue &q,tree bt);
int OutsqQueue(Queue &q,tree &p);
int DisplayQueue(Queue &q);
void DestroyList_Queue(Queue &q);
void TravelOrder(BTNode* bt);
void PrintTree(BTNode * bt,int level); //90度旋转
void Leafs(BTNode *bt); //叶子结点数
void DestroyTree(BTNode * bt); //销毁
int main(void)
{
BTNode *b;
b=Creat_Bt();
printf("先序遍历:\n");
Preorder_first(b);
// printf("\n中序遍历:\n");
// Preorder_mid(b);
printf("\n后序遍历:\n");
Preorder_last(b);
// printf("\n层次遍历:\n");
// TravelOrder(b);
printf("\n");
// Max_value(b);
// printf("二叉树中结点的最大值为:%d \n",k);
PrintTree(b,1);
// printf("\nleafs:");
// Leafs(b);
// printf("\nleafs number:%d\n",count);
printf("DestroyTree:\n");
DestroyTree(b);
return 0;
}
void PrintTree(BTNode * bt,int level)
{
int i;
if(bt)
{
PrintTree(bt->rchild,level+1);
if(level!=1)
{
for(i=1;i<6*(level-1);i++) printf(" ");
printf("----%d\n",bt->data);
}
else
printf(" %d\n",bt->data);
PrintTree(bt->lchild,level+1);
}
}
BTNode* Creat_Bt(void)
{
int i,data,j;
BTNode *s,*t;
printf("输入结点序号和结点数值,以0和#结束:");
scanf("%d%d",&i,&data);
while(i!=0&&data!='#')
{
s=new BTNode;
s->data=data;
s->lchild=s->rchild=NULL;
p[i]=s;
if(i==1)
t=s;
else
{
j=i/2;
if(i%2==0)
p[j]->lchild=s;
else
p[j]->rchild=s;
}
printf("输入结点序号,结点数值:");
scanf("%d%d",&i,&data);
}
return t;
}
void Preorder_first(BTNode *bt)
{
if(bt)
{
printf("%d ",bt->data);
Preorder_first(bt->lchild);
Preorder_first(bt->rchild);
}
}
void Preorder_mid(BTNode *bt)
{
if(bt)
{
Preorder_mid(bt->lchild);
printf("%d ",bt->data);
Preorder_mid(bt->rchild);
}
}
void Preorder_last(BTNode *bt)
{
if(bt)
{
Preorder_last(bt->lchild);
Preorder_last(bt->rchild);
printf("%d ",bt->data);
}
}
void Max_value(BTNode *bt)
{
if(bt)
{
if(k<bt->data)
k=bt->data;
Max_value(bt->lchild);
Max_value(bt->rchild);
}
}
void InitQueue(Queue &q)
{
q.front=0;
q.rear=0;
}
int EmptyQueue(Queue &q)
{
if(q.rear==q.front)
return 1;
else
return 0;
}
void InsqQueue(Queue &q,tree bt)
{
if(q.rear==MAXNUM)
printf("overflow");
q.rear++;
q.data[q.rear]=bt;
}
int OutsqQueue(Queue &q,tree &p)
{
if(EmptyQueue(q))
{
printf("underflow");
return 0;
}
else
{
p=q.data[q.front+1];
q.front++;
}
if(EmptyQueue(q))
{
q.front=0;
q.rear=0;
}
return 1;
}
int DisplayQueue(Queue &q)
{
int i;
if(EmptyQueue(q))
return 1;
for(i=1;i<=q.rear;i++)
{
printf("%d ",q.data[q.front+1]);
q.front++;
}
return 1;
}
void DestroyList_Queue(Queue &q)
{
if(EmptyQueue(q)==0)
{
q.front=0;
q.rear=0;
}
}
void TravelOrder(BTNode *bt)
{
Queue q;
InitQueue(q);
BTNode *p,*p1,*p2;
p=bt;
if(p)
InsqQueue(q,p);
while(!EmptyQueue(q))
{
OutsqQueue(q,p);
printf("%d ",p->data);
p1=p->lchild;
p2=p->rchild;
if(p1)
InsqQueue(q,p1);
if(p2)
InsqQueue(q,p2);
}
}
void Leafs(BTNode * bt)
{
if(bt)
{
if(bt->lchild==NULL && bt->rchild==NULL)
{
printf("%3d",bt->data);
count++;
}
Leafs(bt->lchild);
Leafs(bt->rchild);
}
}
void DestroyTree(BTNode * bt)
{
if(bt&&bt->lchild)
{
DestroyTree(bt->lchild);
}
if(bt&&bt->rchild)
{
DestroyTree(bt->rchild);
}
printf("%3d",bt->data);
free(bt);
}
数据结构源码笔记(C语言描述)汇总: