#include "stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
typedef struct node{
char data;
struct node *lchild, *rchild, *praret;
}binode, *biTree;
biTree creat(){
char c;
scanf("%c",&c);
biTree p,t;
if (c != '*')
{
t = (biTree)malloc(sizeof(binode));
t->data = c;
p = creat();
t->lchild = creat();
if (p)
p->praret = t;
p = creat();
t->rchild = creat();
if (p)
p->praret = t;
}
else
return NULL;
return t;
}
void show(biTree bt){
if (bt!=NULL)
{
printf("%c\t", bt->data);
show(bt->lchild);
show(bt->rchild);
}
}
int biheight(biTree bt){
int lchilddep, rchilddep;
if (bt == NULL)
return 0;
else
lchilddep = biheight(bt->lchild);
rchilddep = biheight(bt->rchild);
return (lchilddep>rchilddep) ? (lchilddep + 1) : (rchilddep + 1);
}
int count(biTree bt){
int num1, num2;
if (bt == NULL)
return 0;
else
num1 = count(bt->lchild);
num2 = count(bt->rchild);
return (num1 + num2 + 1);
}
biTree find(biTree bt,char c){ //按值查找二叉树结点
if (bt=NULL)
return NULL;
if (bt->data == c)
return bt;
find(bt->lchild, c);
find(bt->rchild, c);
}
void main(){
biTree L, L2;
int x, y;
L = creat();
printf("前序遍历为:");
show(L);
printf("\n");
x = biheight(L);
printf("二叉树的高度为:%d", x);
printf("\n");
y = count(L);
printf("二叉树的节点个数为:%d", y);
printf("\n");
char c;
printf("请输入要查找的元素");
scanf("%c", &c);
getchar();
L2 = find(L, c);
if (L2->lchild!=NULL)
printf("左子树值为%c\n", L2->lchild->data);
else
printf("无左子树\n");
if (L2->rchild)
printf("右子树值为%c\n", L2->rchild->data);
else
printf("无右子树\n");
printf("父树值为%c\n", L2->praret->data);
getch();
}
慕斯709654