C语言二叉树排序的一道题目

来源:百度知道 编辑:UC知道 时间:2024/05/09 02:58:36
九、(15分)二叉树排序方法如下:
1)将第一个数据放在树根。
2)将随后读入的数据与树根中的数据相比较,若比树根大,则置于右子树,反之则置于左子树,建成一棵二叉树;
3)利用中序遍历打印排序结果。
试用C语言编写二叉树的排序程序,并分析其算法复杂性。

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 100
typedef char elemtype;
typedef struct node
{ elemtype data;
struct node* lchild;
struct node* rchild;
}BTNode;

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 100
typedef char elemtype;

int BTWidth(BTNode *b)
{ struct
{ int lno;
BTNode *p;
}Qu[Maxsize];
int front,rear;
int lnum,max,i,n;
front=rear=0;
if(b!=NULL)
{ rear++;
Qu[rear].p=b;
Qu[rear].lno=1;
while(rear!=front)
{ front++;
b=Qu[front].p;
lnum=Qu[front].lno;
if(b->lchild!=NULL)
{ rear++;
Qu[rear].p=b->lchild;
Qu[rear].lno=lnum+1;
}
if(b->rchild!=NULL)
{ rear++;
Qu[rear].p=b->rchild;
Qu[rear].lno=lnum+1;
}
}
max=0;
lnum=1;
i=0;
while(i<