数据结构实验6-树

avatar 2016年12月05日11:42:56 0 4647 views
博主分享免费Java教学视频,B站账号:Java刘哥
 
  1. /*
  2. 编写算法函数void levelorder(tree t)实现树的层次遍历。
  3. */
  4. #include "tree.h"
  5. void levelorder(tree t)    /* t为指向树根结点的指针*/
  6. {
  7.     tree queue[100];
  8.     int f,r,i; //f,r分别为队头和队尾指针
  9.     tree p;
  10.     f=0; r=1; queue[0]=t;
  11.     while(f<r)
  12.     {
  13.         p=queue[f] ;f++;printf("%c",p->data);  //访问队头元素
  14.         for(i=0;i<m;i++) //将刚被访问的元素的所有子女结点依次进队 
  15.         {
  16.             if(p->child[i])
  17.             {
  18.                 queue[r]=p->child[i];
  19.                 r++;
  20.             }
  21.         }
  22.     }
  23. }
  24.  int main()
  25.  {
  26.    tree t;
  27.    printf("please input the preorder sequence of the tree:\n");
  28.    t=createtree();
  29.    printf("\nthe levelorder is:");
  30.    levelorder(t);
  31.    return 0;
  32.  }
 
  1. /*
  2. 假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PreOrder1(tree root),实现树的前序遍历算法。
  3. */
  4. #include "tree.h"
  5. void  PreOrder1(tree root)
  6. {
  7.     tree stack[MAXLEN];
  8.     int top=-1,i;
  9.     while(top!=-1||root)
  10.     {
  11.         if(root)                        //子树不为空
  12.         {
  13.             printf("%c",root->data);
  14.             for(i=m-1;i>0;i--)
  15.             {
  16.                 if(root->child[i]!=NULL)    //将不为空的子结点进栈
  17.                     stack[++top]=root->child[i];
  18.             }
  19.             root=root->child[0];        //从第一个子结点开始遍历
  20.         }
  21.         else                            //子树为空,出栈
  22.         {
  23.             root=stack[top--];
  24.         }
  25.     }
  26. }
  27. int main ()
  28. {
  29.         tree root;
  30.         printf("please input the preorder sequence of the tree:\n");
  31.         root =createtree();
  32.         printf("前序序列是:\n");
  33.         PreOrder1(root);
  34.         return 0;
  35. }
 
  1. /*
  2.    假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PostOrder1(tree t),实现树的后序遍历算法。
  3. */
  4. #include "tree.h"
  5. int PostOrder1(tree root)
  6. {
  7.     tree treeStack[MAXLEN];     //待处理结点
  8.     tree overStack[MAXLEN];     //已处理完的结点
  9.     int top=-1,top1=-1,i;
  10.     if(root)
  11.         treeStack[++top]=root; //根结点进栈
  12.     while(top!=-1)
  13.     {
  14.         root=treeStack[top--];  //取一个待处理结点
  15.         for(i=0;i<m;i++)        //将root的所有子结点进栈
  16.         {
  17.             if(root->child[i])
  18.                 treeStack[++top]=root->child[i];
  19.         }
  20.         overStack[++top1]=root; //处理完root,将root入overStack
  21.     }
  22.     while(top1!=-1)             //输出
  23.         printf("%c",overStack[top1--]->data);
  24. }
  25. int main ()
  26. {
  27.     tree root;
  28.     printf("please input the preorder sequence of the tree:\n");
  29.     root =createtree();
  30.     printf("后序序列是:\n");
  31.     PostOrder1(root);
  32.     return 0;
  33. }
 
  1. /*
  2. 假设树采用指针方式的孩子表示法表示,试编写一个函数int equal(tree t1, tree t2),
  3. 判断两棵给定的树是否等价(两棵树等价当且仅当其根结点的值相等且其对应的子树均相互等价)。
  4. */
  5. #include "tree.h"
  6. #define TRUE  1
  7. #define FALSE 0
  8. int equal(tree t1,tree t2)
  9. {
  10.     int flag=TRUE,i;
  11.     if(!t1&&!t2)                //若两树为空,则相等
  12.         return TRUE;
  13.     else if(!t1&&t2||t1&&!t2)   //若有一树为空,则不相等
  14.         return FALSE;
  15.     else if(t1->data!=t2->data)
  16.         return FALSE;
  17.     else
  18.     {
  19.         for(i=0;i<m;i++)
  20.         {
  21.             if(t1->child[i]==t2->child[i])
  22.                 flag=flag&&equal(t1->child[i],t2->child[i]);
  23.         }
  24.         return flag;
  25.     }
  26. }
  27. int main ()
  28. {
  29.     tree t1,t2;
  30.     printf("please input the preorder sequence of the tree:\n");
  31.     t1=createtree();
  32.     getchar();
  33.     printf("please input the preorder sequence of the tree:\n");
  34.     t2=createtree();
  35.     if ( equal(t1,t2) == TRUE)
  36.     {
  37.         printf ("两树相等\n");
  38.     }
  39.     else
  40.     {
  41.         printf ("两树不相等\n");
  42.     }
  43.     return 0;
  44. }
 
  1. /*
  2. 假设树采用指针方式的孩子表示法存储结构,试编写一个函数tree Ct(char s[]),
  3. 根据输入的树的括号表示字符串s,生成树的存储结构。例如,若要建立教材图6.4所示的树,
  4. 应输入A(B(E,F),C,D(G(I,J,K),H))。(说明,tree.h中定义的常量m表示树的最
  5. 大度,请根据建树的需要自行修改m的值)
  6.  
  7. */
  8. #include "tree.h"
  9. /*请将本函数补充完整,并进行测试*/
  10. tree Ct(char s[MAXLEN])
  11. {
  12.    tree stack[MAXLEN],parent=NULL,child=NULL;
  13.    int n=0,top=0,done=0,i;
  14.    while(done==0&&s[n]!='\0')   //未完成并且字符串不为空
  15.    {
  16.        if(s[n]=='(')            //遇到左括号,则其前面的结点成为父结点
  17.        {
  18.            n++;
  19.            parent=child;
  20.            stack[top++]=parent; //将父结点进栈
  21.        }
  22.        else if(s[n]==')')       //遇到右括号,父结点出栈
  23.        {
  24.            n++;
  25.            parent=stack[--top];
  26.            if(top==0)           //若栈顶无元素,则结束
  27.             done=1;
  28.            else
  29.             parent=stack[top-1];//换下一个父结点收集子结点
  30.        }
  31.        else if(s[n]==',')
  32.         n++;
  33.        else
  34.        {
  35.            child=(tree)malloc(sizeof(node));
  36.            child->data=s[n];
  37.            for(i=0;i<m;i++)
  38.             child->child[i]=NULL;
  39.            if(parent!=NULL)      //若父结点不为空,则将其挂到父结点上
  40.            {
  41.                for(i=0;i<m;i++)
  42.                 if(parent->child[i]==NULL)
  43.                 {
  44.                     parent->child[i]=child;
  45.                     break;
  46.                 }
  47.            }
  48.            n++;
  49.        }
  50.    }
  51.    return parent;
  52. }
  53. int main ()
  54. {
  55.     char s[MAXLEN];
  56.     tree root = NULL;
  57.     printf ("请用树的括号表示法输入一棵树:\n");
  58.     scanf ("%s",s);
  59.     root = Ct(s);
  60.     preorder(root);  /*前序遍历树*/
  61.     return 0;
  62. }
本文地址:http://liuyanzhao.com/3586.html 转载请注明
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0