鱼C论坛

 找回密码
 立即注册
查看: 1626|回复: 8

[已解决]求大佬们救救孩子,数据结构作业就要交了,很急很急

[复制链接]
发表于 2021-10-17 12:40:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
实验内容
1、按层序次序输入二叉树中结点的值(字符型或整型),构造顺序存储的二叉树T;
2、判断二叉树是否为空,输出二叉树的深度和根结点的值;
3、分别按层次序列、先序、中序、后序方法遍历二叉树,并打印。


看到帖子的大佬们:
有时间的话能不能瞅一眼孩子的代码,实在是不会了都看一早上了
没时间的话可不可以直接写个答案(不要写太复杂的,我看不懂TAT),我慢慢观摩

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. #include<stdlib.h>

  5. // 定义二叉树二叉列表存储结构 结构体 BiTNode
  6. typedef struct BiTNode{   
  7.         char data;
  8.         struct BiTNode *lchild, *rchild;
  9. }BiTNode,*BiTree;

  10. //创建一棵二叉树
  11. //按书上写的敲下来的,看不太懂
  12. BiTree CreateBiTree(BiTree T){
  13.         char ch;
  14.         printf("请输入节点:\n");
  15.         scanf("%c",&ch);
  16.         fflush(stdin);    //清空输入缓冲区
  17.         if (ch=='\0')
  18.         {
  19.                 T=NULL;
  20.         }
  21.         else{
  22.                 if(!(BiTree*)malloc(sizeof(BiTNode))) exit(0);
  23.                 //判断内存分配空间是否成功,给T指针分配连续的BiTree类型所占字节数的内存空间
  24.                 T->data = ch;
  25.                 T->lchild = CreateBiTree(T->lchild);
  26.                 T->rchild = CreateBiTree(T->rchild);
  27.         }
  28.         return(T);
  29. }
  30. //如何实现输出根结点?
  31. //遍历二叉树
  32. void level(BiTNode *T)
  33. {
  34.         //层次遍历二叉树
  35.         int front, rear,maxSize;
  36.         BiTNode *que[maxSize];
  37.         front = rear = 0;
  38.         BiTNode *p;
  39.         rear = (rear+1) % maxSize;
  40.         que[rear] = T;
  41.         while(front != rear)
  42.         {
  43.                 p = que[front];
  44.                 //Visit(p); //我该怎么定义这个visit函数呢?
  45.                 if(p->lchild != NULL)
  46.                 {
  47.                         rear = (rear+1) % maxSize;
  48.                         que[rear] = p->lchild;
  49.                 }
  50.                 if(p->rchild != NULL)
  51.                 {
  52.                         rear = (rear+1) % maxSize;
  53.                         que[rear] = p->rchild;
  54.                 }
  55.         }

  56. }

  57. //遍历二叉树
  58. //void LevelOrder(BiTree *T) {
  59. //        //层次序列遍历
  60. //    InitQueue(Q); //初始化一个队列Q
  61. //    BiTree *p; //p用来跟踪队头元素
  62. //    EnQueue(Q,T); //根节点入队
  63. //    while(!IsEmpty(Q)) {
  64. //        DeQueue(Q,p);
  65. //        visit(p);
  66. //        if(p->lchild!=null) {
  67. //            EnQueue(Q,p->lchild);
  68. //        }
  69. //        if(p->rchild!=null) {
  70. //            EnQueue(Q,p->rchild);
  71. //        }
  72. //        }
  73. //}

  74. //先序遍历
  75. void  PreOrderTraverse(BiTree T){
  76.         if(T){
  77.                 printf("%c",T->data);                        // 打印 当前节点存储的数据
  78.                 PreOrderTraverse(T->lchild);        // 遍历左节点树
  79.                 PreOrderTraverse(T->rchild);        // 遍历右节点树
  80.         }
  81. }

  82. //中序遍历
  83. void InOrderTraverse(BiTree T){
  84.         if(T){
  85.                 InOrderTraverse(T->lchild);
  86.                 printf("%c",T->data);
  87.                 InOrderTraverse(T->rchild);
  88.         }
  89. }

  90. //后序遍历
  91. void PostOrderTraverse(BiTree T){
  92.         if(T){
  93.                 PostOrderTraverse(T->lchild);
  94.                 PostOrderTraverse(T->rchild);
  95.                 printf("%c",T->data);
  96.         }
  97. }

  98. //返回二叉树的深度
  99. int GetDepth(BiTree T)
  100. {
  101.         int l,r,c;
  102.         l=r=c=0;
  103.         if(T) {
  104.                 l=GetDepth(T->lchild);
  105.                 r=GetDepth(T->rchild);
  106.                 c=(l>r?l:r);
  107.                 return c+1;
  108.         }
  109.         else {
  110.                 return 0;
  111.         }
  112. }


  113. int main()
  114. {
  115.         BiTree T;
  116.         T=CreateBiTree(T);
  117.         //判断二叉树是否为空
  118.         if(T)                // todo 这一行有问题
  119.                 printf("二叉树不为空\n");
  120.         else
  121.                 printf("二叉树为空\n");
  122.         printf("二叉树的深度是:\n");
  123.         printf("%d\n",GetDepth(T));
  124.         if(T)
  125.                 printf("%c\n",T->data); //输出二叉树根结点的值  todo 但是不需要这个代码 下面会打印的
  126.         printf("输出按层次序列遍历二叉树:\n");       
  127.         level(T);
  128.         printf("输出按先序遍历二叉树:\n");       
  129.         PreOrderTraverse(T);
  130.         printf("输出按中序遍历二叉树:\n");       
  131.         InOrderTraverse(T);
  132.         printf("输出按后序遍历二叉树:\n");       
  133.         PostOrderTraverse(T);
  134.         return 0;
  135. }
复制代码
最佳答案
2021-10-17 13:22:37
这是前中后序的,你自己看一下算法,自己加一个层次遍历 的进去就可以了
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*二叉树链式存储的头文件*/
  4. typedef char datatype;         /*结点属性值的类型*/
  5. typedef struct node{           /*二叉树结点的类型*/
  6.   datatype  data;
  7.   struct node  *lchild,*rchild;
  8. }bintnode,*bintree;


  9. bintree createbintree()
  10. {/*按照前序遍历的顺序建立一棵给定的二叉树*/
  11. char ch;
  12. bintree t;
  13. if ((ch=getchar())=='#')
  14.       t=NULL;
  15. else {
  16.           t=(bintnode *)malloc(sizeof(bintnode));
  17.           t->data=ch;
  18.           t->lchild=createbintree();
  19.           t->rchild=createbintree();
  20.        }
  21.     return t;
  22. }


  23. void preorder(bintree t)
  24.   { /*前序遍历二叉树的递归算法*/
  25.     if (t) {
  26.              printf("%c",t->data);       /*根左右*/
  27.                          preorder(t->lchild);
  28.                  preorder(t->rchild);
  29.              }
  30.      }


  31. void inorder(bintree t)
  32.   { /*中序遍历二叉树的递归算法*/
  33.     if (t) {
  34.                          inorder(t->lchild);    /*左根右*/
  35.                          printf("%c",t->data);
  36.                  inorder(t->rchild);
  37.              }
  38.      }


  39. void postorder(bintree t)
  40.   { /*后序遍历二叉树的递归算法*/
  41.     if (t) {
  42.              postorder(t->lchild);         /*左右根*/
  43.                  postorder(t->rchild);
  44.              printf("%c",t->data);
  45.               }
  46.      }


  47. void main()
  48.   { bintree root;    /*自定义*/
  49.    
  50.     printf("请输入二叉树的结点值:");  root=createbintree();   /*上面的输入二叉树的函数建二叉树*/
  51.    
  52.     printf("二叉树的前序遍历结果为:");  preorder(root);
  53.    
  54.     printf("\n二叉树的中序遍历结果为:");  inorder(root);
  55.    
  56.     printf("\n二叉树的后序遍历结果为:");  postorder(root);
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-17 13:14:59 | 显示全部楼层
这种作业好像上学期我都做了那时候我也像你一样急啊哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-17 13:22:37 | 显示全部楼层    本楼为最佳答案   
这是前中后序的,你自己看一下算法,自己加一个层次遍历 的进去就可以了
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*二叉树链式存储的头文件*/
  4. typedef char datatype;         /*结点属性值的类型*/
  5. typedef struct node{           /*二叉树结点的类型*/
  6.   datatype  data;
  7.   struct node  *lchild,*rchild;
  8. }bintnode,*bintree;


  9. bintree createbintree()
  10. {/*按照前序遍历的顺序建立一棵给定的二叉树*/
  11. char ch;
  12. bintree t;
  13. if ((ch=getchar())=='#')
  14.       t=NULL;
  15. else {
  16.           t=(bintnode *)malloc(sizeof(bintnode));
  17.           t->data=ch;
  18.           t->lchild=createbintree();
  19.           t->rchild=createbintree();
  20.        }
  21.     return t;
  22. }


  23. void preorder(bintree t)
  24.   { /*前序遍历二叉树的递归算法*/
  25.     if (t) {
  26.              printf("%c",t->data);       /*根左右*/
  27.                          preorder(t->lchild);
  28.                  preorder(t->rchild);
  29.              }
  30.      }


  31. void inorder(bintree t)
  32.   { /*中序遍历二叉树的递归算法*/
  33.     if (t) {
  34.                          inorder(t->lchild);    /*左根右*/
  35.                          printf("%c",t->data);
  36.                  inorder(t->rchild);
  37.              }
  38.      }


  39. void postorder(bintree t)
  40.   { /*后序遍历二叉树的递归算法*/
  41.     if (t) {
  42.              postorder(t->lchild);         /*左右根*/
  43.                  postorder(t->rchild);
  44.              printf("%c",t->data);
  45.               }
  46.      }


  47. void main()
  48.   { bintree root;    /*自定义*/
  49.    
  50.     printf("请输入二叉树的结点值:");  root=createbintree();   /*上面的输入二叉树的函数建二叉树*/
  51.    
  52.     printf("二叉树的前序遍历结果为:");  preorder(root);
  53.    
  54.     printf("\n二叉树的中序遍历结果为:");  inorder(root);
  55.    
  56.     printf("\n二叉树的后序遍历结果为:");  postorder(root);
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-17 13:25:04 | 显示全部楼层
你加油 好像我没看到你代码抱歉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-17 15:12:14 | 显示全部楼层
Gacy 发表于 2021-10-17 13:22
这是前中后序的,你自己看一下算法,自己加一个层次遍历 的进去就可以了

太太太感谢了,但我还想知道层次遍历以及输出根结点该怎么表示,书上没看到有相关代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-17 15:13:34 | 显示全部楼层
要不我找找??上学期的事了,不知道还有没有
基础的书上肯定有啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-17 15:17:44 | 显示全部楼层
Gacy 发表于 2021-10-17 15:13
要不我找找??上学期的事了,不知道还有没有
基础的书上肯定有啊

好的,我确实翻了好多遍二叉树相关的页码,没有有前中后序的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-17 15:21:45 | 显示全部楼层
你看看对你有没有用
  1. void levelorder(tree t)    /*树的层次遍历,t为指向树根结点的指针*/
  2.     {  tree queue[100];        /*存放等待访问的结点队列*/
  3.        int f,r,i;            /*f、r分别为队头、队尾指针*/
  4.        tree p;
  5.        f=0; r=1; queue[0]=t;
  6.        while(f<r)      /*队列不为空*/
  7.         {p=queue[f]; f++; printf("%c",p->data);  /*访问队头元素*/
  8.          for(i=0;i<m;i++)   /*将刚被访问的元素的所有子女结点依次进队*/
  9.            if(p->child[i])
  10.             {queue[r]=p->child[i];  r++;}
  11.          }
  12.      }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-17 15:28:18 | 显示全部楼层
小菜鸡溜溜达 发表于 2021-10-17 15:17
好的,我确实翻了好多遍二叉树相关的页码,没有有前中后序的代码

我怎么印象中书上是有的呢拜拜了我先看教资去了有空再来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 02:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表