#include<stdio.h>;
#include<stdlib.h>;
#define null 0
#define len sizeof(struct tree)
struct tree
{
int data;
struct tree *left,*right;
};
struct tree *head;
struct tree *creat()
{
struct tree *p,*q,*h,*i;
int n=0,x;
scanf("%d",&x);
while (x!=0)
{
n++;
if (n==1) {
h=(struct tree*)malloc(len);
h->data=x;
h->left=null;
h->right=null;
};
if (n!=1)
{
p=(struct tree*)malloc(len);
p->data=x;
p->left=null;
p->right=null;
q=h;
if (x>q->data) i=q->right;
else i=q->left;
while (i!=null)
{
q=i;
if (x>q->data) i=q->right;
else i=q->left;
};
if (x>q->data) q->right=p;
else q->left=p;
};
scanf("%d",&x);
};
return h;
}
void print(struct tree *p)
{
if (p!=null) {
print(p->left);
printf("%d ",p->data);
print(p->right);
}
}
int depth(nodetype *root) {
if(root==NULL)
return 0;
else {
h1=depth(root->lch);
h2=depth(root->rch);
return max(h1,h2)+1;
}
}
int count(nodetype *root) {
if(root==NULL)
return 0;
else {
if(root->lch==NULL&&root->rch==NULL)
return 1;
else{
h1=count(root->lch);
h2=count(root->rch);
return h1+h2;
}
}
}
main()
{ int active=10,active2=10,n;
while(active!=0){
printf("1、二叉树操作\n");
printf("2、树操作\n");
printf("0、退出\n");
printf("请选择菜单项:");
scanf("%d",&active);
switch(active){
case 1:
printf("1、建立二叉树\n");
printf("2、叶子结点数\n");
printf("3、二叉树的深度\n");
printf("0、返回\n");
printf("请选择菜单项:");
scanf("%d",&active2);
switch(active2){
case 1:
head=creat();
print(head);
break;
case 2:
printf("%d",count(head));break;
case 3:
printf("%d",depth(head));break;
case 0: break;
}
break;
case 2:
printf("1、建立树\n");
printf("2、结点数\n");
printf("0、返回\n");
printf("请选择菜单项:");
scanf("%d",&active2);
switch(active2){
case 1: sortShuoxue(L);break;
case 2: sortChengxu(L);break;
case 0: break;
}
break;
}
}
}
能不能把这个改成C++语言????
急用,希望各路高手来帮帮小弟!