[非常的紧急求助] 关于二叉树的编程
<P>题目是 定义一个抽象的数据类型 用C++编程 二叉树的建立 遍历 叶子节点个数 节点总数 高度 清除二叉树.我需要有头文件主函数... 就是直接可以运行起来的!<BR>这是我写的程序 老师说错的 在定义a的时候定义成字符窜 才对 可是我不会啊 还有别的错误 好象是和C混淆了[em08] 帮我改改拉.......<BR>#include<iostream.h> </P><P> #include<math.h> <BR> typedef struct BT{ <BR> char date; <BR> BT *lchild,*rchild; <BR> } BT ,*tree ; <BR> void create(tree &n) { <BR> char a; <BR> cin>>a; <BR> if(<a href="mailto:a=='@')n=NULL" target="_blank" >a=='@')n=NULL</A>; <BR> else { <BR> n=new BT; <BR> n->date=a; <BR> create(n->lchild); <BR> create(n->rchild); } <BR> } <BR> void read1(tree &n){//先序 <BR> if(n) <BR> { <BR> <BR> cout<<n->date<<" "; <BR> read1(n->lchild); <BR> read1(n->rchild); <BR> } <BR> } <BR> void read2(tree &n){//中序 <BR> if(n) { <BR> read2(n->lchild); <BR> cout<<n->date<<" "; <BR> read2(n->rchild); <BR> } <BR> } <BR> void read3(tree &n){//后序 <BR> if(n) { <BR> read3(n->lchild); <BR> read3(n->rchild); <BR> cout<<n->date<<" "; <BR> } <BR> } <BR> int countleafs(tree &n) { <BR> if(n->date==0) <BR> return 0; <BR> else if(n->lchild&&n->rchild==0) <BR> return 1; <BR> return countleafs(n->lchild)+countleafs(n->rchild); <BR> } <BR> int count(tree &n)<BR> {<BR> if(n->date==0) return 0;<BR> else return 1+count(n->lchild)+count(n->rchild);<BR> }<BR> int Height(tree &n)<BR> {<BR> if(n->date==0) return 0;<BR> else{<BR> int a=Height(n->lchild);<BR> int b=Height(n->rchild);<BR> return(a>b)? a+1:b+1;<BR> }<BR> }<BR> int Destroy(tree &n);<BR> {<BR> if(n->date==0)return TRUE<BR> else return n=NULL<BR> }<BR> main() { <BR> tree n; <BR> create(n); <BR> read1(n); <BR> cout<<endl; <BR> read2(n); <BR> cout<<endl; <BR> read3(n); <BR> cout<<endl; <BR> countleafs(n);<BR> count(n);<BR> Height(n);<BR> Destroy(n);<BR> cout<<endl; <BR> return 0; <BR> }<BR></P>
页:
[1]
