/*
编写算法函数void levelorder(tree t)实现树的层次遍历。
*/#include"tree.h"voidlevelorder(tree t)/* t为指向树根结点的指针*/{
tree queue[MAXLEN];//队列思想int head =0, end =1;int i;
queue[head]= t;//将根节点加入队列while(head < end){for(i =0; i < m; i++)if(queue[head]->child[i])//判断其子节点是否含有节点可以判断,可以的话就逐层入队
queue[end++]= queue[head]->child[i];printf("%c ",queue[head++]->data);}}intmain(){
tree t;printf("please input the preorder sequence of the tree:\n");
t=createtree();printf("\nthe levelorder is:");levelorder(t);return0;}
Question two
/*
假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PreOrder1(tree root),实现树的前序遍历算法。
*/#include"tree.h"voidPreOrder1(tree root){
tree stack[MAXLEN];//本质上就是将递归的行为转化为栈,虽然递归的本质就是栈罢了int i;int top =-1;while(root || top !=-1){if(root){printf("%c ",root->data);for(i = m -1; i >0; i--){if(root->child[i]!=NULL)//遍历自己的所有子树{
stack[++top]= root->child[i];}}
root = root->child[0];//将栈更新为自己的第一颗子树}else{
root = stack[top--];//如果不满足就将当前的结果出栈即可}}}intmain(){
tree root;printf("please input the preorder sequence of the tree:\n");
root =createtree();printf("前序序列是:\n");PreOrder1(root);return0;}
Question three
/*
假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PostOrder1(tree t),实现树的后序遍历算法。
*/#include"tree.h"//6.3voidPostOrder1(tree root)//感觉是题目设计的问题,为什么不直接用void而用int{int i;
tree stacktree[MAXLEN];//表示进行处理的栈int top =-1;
tree fintree[MAXLEN];//表示已经处理完的栈int toped =-1;if(root) stacktree[++top]= root;//将根节点入栈while(top !=-1){
root = stacktree[top--];//取出栈顶的元素进行处理for(i =0; i < m;i ++)if(root->child[i]) stacktree[++top]= root->child[i];//如果可以就入栈
fintree[++toped]= root;//将已经处理完的根节点进入栈}while(toped !=-1)printf("%c ", fintree[toped--]->data);}intmain(){
tree root;printf("please input the preorder sequence of the tree:\n");
root =createtree();printf("后序序列是:\n");PostOrder1(root);return0;}
Question four
/*
假设树采用指针方式的孩子表示法表示,试编写一个函数int equal(tree t1, tree t2),
判断两棵给定的树是否等价(两棵树等价当且仅当其根结点的值相等且其对应的子树均相互等价)。
*/#include"tree.h"#defineTRUE1#defineFALSE0intequal(tree t1,tree t2){int flag = TRUE;int i;if(t1 ==NULL&& t2 ==NULL)return TRUE;//如果两个根目前都为空,直接可以将目前结果的比较值变为0else{if((t1 ==NULL&& t2 !=NULL)||(t1 !=NULL&& t2 ==NULL))return FALSE;//如果t1和t2不满足同时为空的消息,那么说明这次的比较为失败else{if(t1->data != t2->data)return FALSE;//如果该节点所指向的内容不同则是其原因else{for(i =0; i < m; i++) flag = flag&&equal(t1->child[i], t2->child[i]);//将flag看成是所有的结果的整合return flag;}}}}intmain(){
tree t1,t2;printf("please input the preorder sequence of the tree:\n");
t1=createtree();getchar();printf("please input the preorder sequence of the tree:\n");
t2=createtree();if(equal(t1,t2)== TRUE){printf("两树相等\n");}else{printf("两树不相等\n");}return0;}
Question five
/*
假设树采用指针方式的孩子表示法存储结构,试编写一个函数tree Ct(char s[]),
根据输入的树的括号表示字符串s,生成树的存储结构。例如,若要建立教材图6.4所示的树,
应输入A(B(E,F),C,D(G(I,J,K),H))。(说明,tree.h中定义的常量m表示树的最
大度,请根据建树的需要自行修改m的值)
*/#include"tree.h"/*请将本函数补充完整,并进行测试*/
tree Ct(char s[MAXLEN]){int length, i, j;int top =-1;
length =strlen(s);//调度字符串的长度
tree stack[MAXLEN],root =NULL,temp =NULL, n;//使用栈的思想int childtree[MAXLEN];//计算其第几个孩子for(i =0; i < length; i ++)//遍历所有的长度{if(s[i]==',')continue;//如果处理的是,的话就可以直接跳过;elseif(s[i]=='(')//如果这个结果是(话就将这个结果入栈,并表示目前的孩子节点为0{
stack[++top]= temp;
childtree[top]=0;}elseif(s[i]==')') top--;//如果是这个就出栈elseif(top !=-1)//如果不是最后一个的话就不是处理根节点{
n =(tree)malloc(sizeof(node));//建立空间
n->data = s[i];//将数据存入for(j =0; j < m;j++) n->child[j]=NULL;//将他的所有孩子节点都变初始化为空
temp = n;//状态更新为n的状态
stack[top]->child[childtree[top]++]= temp;//将这个节点所对应的节点状态读入;}else{
root =(tree)malloc(sizeof(node));//处理根节点
root->data = s[i];for(j =0; j < m; j++) root->child[j]=NULL;
temp = root;}}return root;}intmain(){char s[MAXLEN];
tree root =NULL;printf("请用树的括号表示法输入一棵树:\n");scanf("%s",s);
root =Ct(s);preorder(root);/*前序遍历树*/return0;//A(B(E,F),C,D(G(I,J,K),H))}