#define NULL 0;
#define maxsize 32
#include
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild, *rchild;
}bitree;
bitree *Q[maxsize];
bitree *CREATREE() //新建一棵二叉树
{
char ch;
int front,rear;
bitree *root,*s;
root="NULL";
front="1";rear=0;
ch="getchar"();
while(ch!='#')
{
s="NULL";
if(ch!='@')
{
s="malloc"(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1) root="s";
else
{
if(s&&Q[front])
if(rear%2==0) Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1) front++;
}
ch="getchar"();
}
return root;
}
INORDER(bitree *t) //中序遍历二叉树
{
if (t)
{
INORDER(t->lchild);
printf("\t%c\n",t->data);
INORDER(t->rchild);
}
}
int MAX(int a,int b) //大小比较
{
if (a>b) return (a);
else return (b);
}
int HIGH(bitree *t) //求高度的子函数
{
if (t==0) return (0);
else
return (1+MAX(HIGH(t->lchild),HIGH(t->rchild)));//谁大取谁再加根节点
}
void main()
{
bitree *root;
printf("please input the leaves of the tree:\n");
root="CREATREE"();
printf("zhong xu bian li shi:\n");
INORDER(root);
printf("high=%d\n",HIGH(root));
}
文章评论(0条评论)
登录后参与讨论