#include <iostream>
#include <stdlib.h>
#include<malloc.h>
typedef struct tree{
char data;
struct tree *lchild,*rchild;
}binode,*bitree;
///////////////////////////////
void creat(bitree &t)
{ char c;
cin>>c;
// scanf("%c",&c);
if(c=='r') t=NULL;
else
{if(!(t=(bitree)malloc(sizeof(tree)))) exit(0);
t->data=c;
creat(t->lchild);
creat(t->rchild);
}///////////else
}///////////////////////creat
////////////////////////////////////////////////////////////////////////////////////////////////////////
void print(bitree &t)
{
if(t)
{ cout<<t->data<<endl;
print(t->lchild);
print(t->rchild);
}
}///////////////////print
///////////////////////////////////////////////////////////////////////////////////////////////////
int root=0,lbranch=0,rbranch=0,leaf=0;/////////////////////SUM表示为结点总数///ROOT表示为根结点,RBRANCH和 LBRANCH分别表示为右左分支,///LEAF表示叶子结点
void find(bitree t)
{
if(t)
{
if(!(t->lchild)&&!(t->rchild)) {leaf++;find(t->lchild);}
if(t->lchild&&t->rchild) root++;
if(t->lchild) {lbranch++; find(t->lchild);}
if(t->rchild) {rbranch++; find(t->rchild);}
}/////////////////////////////while
cout<<"结点总数"<<rbranch+lbranch+1<<endl;
cout<<"叶子结点"<<leaf<<endl;
cout<<"根结点"<<root<<endl;
}////////////////find
int main(int argc, char *argv[])
{bitree t;
creat(t);
print(t);
find(t);
system("PAUSE");
return 0;
}
忙了那么久复算搞出来了,昨晚就建了个二叉树,然后输出。。。可是当中间的用于求各种结点的函数出现死循环时,找了半天也没出原因。第二天还是想着它错在哪,最糟糕的是居然忘了递归的最基本格式,把退出的if语句也忘了,感觉自己真的忘了好多原本已经属于自己的东西了,回首大学过去的一年里,还真算是虚度年华。。呜呜。。。还好,身边出现了很多很上进的朋友,尤其是这个学生大本营中,他们带给了我太多的信心和方向,努力了,学习我想学的东东。