#include \
#include \#include \
typedef char TElemType; //定义结点数据为字符型 typedef int Status; #define ERROR 0 #define OK 1
//定义函数类型为 int 型
typedef struct BiTNode{ TElemType
struct BiTNode struct BiTNode
//定义结构体
data; // 结点数值 *lchild; // 左孩子指
针 *rchild; // 右孩子指
//下一结点指针
针 struct BiTNode *next;
}BiTNode, *BiTree;
Status NumJudge(char ch[20]){ //限制输入数据必须为大于零的整形 char ch1[20]; int num;
while(1){
scanf(\
num=atoi(ch); // 将字符串转换为整型 itoa(num,ch1,10); //将整型转换为字符串型 if(strcmp(ch,ch1)==0&&num>0)break; else{printf(\请输入一个大于零的整数 : \
}
return num; }//NumJudge
Status InitBiTree(BiTree &T){
//构造空二叉树 T
if(!(T=(BiTree)malloc(sizeof(BiTNode))))exit(ERROR); 若申请空间失败则退出 // T->next=NULL;
printf(\空二叉树构建成功 !\\n\\n\return OK; }//InitBiTree
Status DestroyTree(BiTree &T,BiTree t){
//销毁二叉树 if(T){
free(T);T=NULL;
printf(\二叉树销毁成功 !\\n\
}
if(t){
DestroyTree(T,t->lchild); DestroyTree(T,t->rchild); free(t);
}
return OK; }//DestroyTree
Status ClearBiTree(BiTree &T,int sum,int &i){
//清空二叉树 if(T){
ClearBiTree(T->lchild,sum,i); ClearBiTree(T->rchild,sum,i); free(T); i++;
}
if(i==sum){printf(\二叉树清空成功 !\\n\
}//ClearBiTree
Status CreateBiTree(BiTree &T,int i,int j,TElemType ch){ //按先序次序输入二叉树中结点的值 (一
个字符 ), 空格字符表示该结点为空 //构造二叉链表示的二叉树 T TElemType ch1; int k; char str[20];
if(i==0){printf(\按先序顺序建立二叉树 :请按提示输入相应的数据 ( 一个字符 ),若提 示结点数值为空, \\n 请输入空格 \\n\\n\
printf(\请输入树根 : \请输入 %c 的左
孩子 : \请输入 %c 的右孩子 : \while(1){
fflush(stdin); for(k=0;k<20;k++){
str[k]=getchar(); if(str[k]=='\\n')break;
}
//限制输入数据必须为字符型 ,否则重新输入
if(k==0)printf(\请输入一个字符后再按 Enter 键: \if(k>1)printf(\您只能输入一个字符 : \
}
ch1=str[0]; if(ch1!=' '){
// 获取输入的准确字符型数据
// 输入空格则为根结点为空
if(ch1==' '){T=NULL;return ERROR;}
if(!(T=(BiTree)malloc(sizeof(BiTNode)))) exit(ERROR); T->data=ch1; // 生成根结点 ch=T->data; i++;
CreateBiTree(T->lchild,i,j,ch); j=i;j++;
CreateBiTree(T->rchild,i,j,ch);
}
// 构造左子树
// 构造右子树
i=0;j=0; return OK;
}//CreateBitree
Status TreeDepth(BiTree T,int l,int &h){
//若二叉树存在 ,返回其深度 if(T){
l=l+1; if(l>h)h=l;
TreeDepth(T->lchild,l,h); TreeDepth(T->rchild,l,h);
}
return h; }//TreeDepth
Status GetRootElem(BiTree T){
//获取根结点值
printf(\该二叉树的根结点值为 : %c\\n\\n\return OK; }//GetRootElem
Status SaveElem(BiTree T,BiTree *Q,int i){
〃根据完全二叉树中,若本节点位置序号为i,则其左孩子结点为 //保存二叉树的有效结点至指针数组 if(T){
Q[i]=T;
SaveElem(T->lchild,Q,2*i); SaveElem(T->rchild,Q,2*i+1);
}
2i,右孩子为2i+1的方法
Q 特定的位置
return OK; }//SaveElem
Status Lev_Traverse(BiTree T,int h){
//按层次从上到下 ,每层从左到右的顺序显示树状二叉树 if(T==NULL){printf(\二叉树目前为空树 \\n\\n\BiTree *Q;
if(!(Q=(BiTree *)malloc(int(pow(2,h)+1) * sizeof(BiTNode))))exit(ERROR); int i,j,n=1,k=h;
for(i=1;i<=int(pow(2,h)+1);i++){
Q[i]=NULL;} SaveElem(T,Q,n);
// 将目前有效结点按照满二叉树的序号存储
printf(\提示 :规定下图中的有效结点的位置序号从 1 开始按从上到下 ,从左到右的顺序 依次递增 \\n\
for(i=1;i<=(pow(2,h)+1);i++){ // 树形显示二叉树
if(int(pow(2,h))%i==0){
printf(\
}
k--;
}
if(Q[i])printf(\
for(j=0;j } printf(\ i=0;j=0; return OK; }//Lev_Trav erse Status FirstPrint(BiTree T,int i){ //按先序次序 (递归 )访问二叉树 if(i==0)printf(\先序 (递归 )遍历结果如下 :\\n\ i++;printf(\访问 T FirstPrint(T->lchild,i); // 递归遍历左子树 FirstPrint(T->rchild,i); // 递归遍历右子树 } i=0; return OK; }//FirstPrintBiTree Status MiddlePrint(BiTree T,int i){ //按中序次序 (递归 )访问二叉树 if(i==0)printf(\中序 (递归 )遍历结果如下 :\\n\ i++; MiddlePrint(T->lchild,i); //递归遍历左子树 printf(\访问 T MiddlePrint(T->rchild,i); //递归遍历右子树 } i=0; return OK; }//MiddlePrint