实验六二叉树的参数计算实验学时:2实验类型:(综合型) 一、;、高度、叶子结点个数算法实现;;、实验条件VisualC++、;、高度、叶子结点个数算法的基本思想;。四、,写出二叉链表结构类型的具体定义。(1)求结点个数、高度、叶子结点个数、交换二叉树左右子树以及复制一棵二叉树的递归算法的基本思想及算法实现;(2)求结点个数、叶子结点个数的非递归算法的基本思想及算法实现;五、。。。【参考程序】#include<>#include<>#include<>#defineMaxSize20typedefintElemType;#defineOK1intcount;typedefstructBiTNode{ ElemTypedata; structBiTNode*lchild,*rchild;}BiTNode,*BiTree;//建立二叉树(按先序序列生成二叉树,#表示空节点)voidCreateBiTree(BiTree*T){ charch; scanf("%c",&ch); getchar();/*回车键(每次输入一个字符后,需敲回车键)*/ if(ch=='#') { printf("不产生子树。\n"); *T=NULL; } else { if(!(*T=(BiTNode*)malloc(sizeof(BiTNode)))) { printf("分配空间失败"); return; }//生成一个新节点(*T)->data=ch; printf("产生左右子树。\n"); CreateBiTree(&((*T)->lchild)); CreateBiTree(&((*T)->rchild)); }}intCount_Tree(BiTreet)//计算二叉树的结点个数。{ intlcount,rcount; if(t==NULL)return0; lcount=Count_Tree(t->lchild);//求左子树的结点个数rcount=Count_Tree(t->rchild);//求右子树的结点个数 returnlcount+rcount+1;}intNCount_Tree(BiTreet)//非递归算法计算二叉树的结点个数。{}intHeight(BiTreet)//计算二叉树的高度{ inth1,h2; if(t==NULL)return0; else { h1=Height(t->lchild);//求左子树的高度 h2=Height(t->rchild); if(h1>h2)returnh1+1;//求右子树的高度 returnh2+1; }}voidCountleaf(BiTreet,int*count)//计算二叉树的叶子结点的个数{ if(t==NULL)*count=0; if(t->lchil
二叉树的参数计算 来自淘豆网www.taodocs.com转载请标明出处.