鱼C论坛

 找回密码
 立即注册
查看: 2362|回复: 5

[已解决]数据结构有关的问题--森林.(数组访问还有一些问题)

[复制链接]
发表于 2019-12-9 10:47:36 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 MFwxy 于 2019-12-11 10:52 编辑

大问题已经解决了,大佬看一看附件就好啦
数组访问的[2][6]明明应该为NULL,但实际上却是[3][0]的'K'。这是为啥啊?

森林的层序遍历,
TForst.h:
  1. #pragma once
  2. #include<iostream>
  3. #define maxn 100
  4. #define ElemenType char

  5. using namespace std;

  6. struct FSNode
  7. {
  8.         ElemenType data = NULL;
  9.         FSNode* firstchild = NULL;
  10.         FSNode* nextsibling = NULL;
  11. };


  12. class TForest
  13. {
  14. public:
  15.         TForest();
  16.         virtual ~TForest();
  17.         //输入数据
  18.         void Tree_in();
  19.         //各种遍历
  20.         void H_BL(FSNode* H);
  21.         void L_BL();
  22.         void CreatLay(FSNode* L, int layer, int num);
  23.         void T_BL(FSNode* T);
  24.         void NodeFound(FSNode* F, ElemenType n);                        //找到改造后的二叉树中的元素
  25.         void NodeFoundF(FSNode* F, ElemenType n, ElemenType m);
  26.         void Mchild(FSNode* F, ElemenType m);
  27.         void MaxH(FSNode* L, int layer);
  28.         void RenewTree(FSNode* T);
  29.         void CouNode(FSNode* T);
  30.         void CouLeaves(FSNode* T);
  31.         void Getdegree(FSNode* T, int layer, int dg);
  32.         void HHTri(FSNode* H, int h);
  33.         void GYB(FSNode* H);

  34.         //题目用函数
  35.         void choose();
  36.         void no0();
  37.         void no1();
  38.         void no2();
  39.         void no3();
  40.         void no4();
  41.         void no5();
  42.         void no6();
  43.         void no7();

  44. private:
  45.         FSNode* Tdata;
  46. };
复制代码


TForest.cpp:
  1. #include "TForest.h"

  2. ElemenType Laysort2[10][10];
  3. int Layer = 0;
  4. int MaxHeight = 0, degree = 0;
  5. int num = 0;
  6. bool flag = false;
  7. int nNode = 0;
  8. int nLeaves = 0;

  9. TForest::~TForest()
  10. {

  11. }

  12. TForest::TForest()//要完成"读树"以及"转化"成二叉树
  13. {
  14.         Tdata = new FSNode;
  15.         Tdata->data = NULL;
  16. }

  17. void TForest::H_BL(FSNode* H)
  18. {
  19.         cout << H->data << " ";
  20.         if (H->firstchild != NULL)
  21.         {
  22.                 H_BL(H->firstchild);
  23.         }
  24.         if (H->nextsibling != NULL)
  25.         {
  26.                 H_BL(H->nextsibling);
  27.         }
  28. }

  29. void TForest::L_BL()//记得输入当前的层数,以及最大的层数;完成之后需要重置层数和最大的层数
  30. {               
  31.         int h = 0;
  32.         int n = 0;
  33.         while (h < MaxHeight)
  34.         {
  35.                 //if(Laysort2[h][n]!='#')
  36.                 cout << Laysort2[h][n]<<" ";
  37.                 n++;
  38.                 if (Laysort2[h][n] == NULL)
  39.                 {
  40.                         n = 0;
  41.                         h++;
  42.                 }
  43.                 if (Laysort2[h][0] == NULL)
  44.                 {
  45.                         break;
  46.                 }
  47.         }
  48. }

  49. void TForest::CreatLay(FSNode* L, int layer, int num)//记得输入当前的层数,以及最大的层数;完成之后需要重置层数和最大的层数
  50. {
  51.         while (Laysort2[layer][num] != NULL)
  52.         {
  53.                 num+=1;
  54.         }

  55.         Laysort2[layer][num] = L->data;

  56.         if (L->nextsibling != NULL)
  57.         {
  58.                 CreatLay(L->nextsibling, layer, num + 1);
  59.         }
  60.         if (L->firstchild != NULL)
  61.         {
  62.                 CreatLay(L->firstchild, layer + 1, 0);
  63.         }
  64. }

  65. void TForest::MaxH(FSNode* L, int layer)//读得最大的深度,然后用于创建存储每一层节点的数组
  66. {
  67.         if (L->nextsibling != NULL)
  68.         {
  69.                 MaxH(L->nextsibling, layer);
  70.         }
  71.         if (L->firstchild != NULL)
  72.         {
  73.                 MaxH(L->firstchild, layer + 1);
  74.         }
  75.         if (layer > MaxHeight)
  76.         {
  77.                 MaxHeight = layer;
  78.         }
  79. }


  80. void TForest::T_BL(FSNode* T)
  81. {
  82.         if (T->firstchild != NULL)
  83.         {
  84.                 T_BL(T->firstchild);
  85.         }
  86.         if (T->nextsibling != NULL)
  87.         {
  88.                 T_BL(T->nextsibling);
  89.         }
  90.         cout << T->data << " ";
  91. }

  92. void TForest::NodeFound(FSNode* F, ElemenType n)
  93. {
  94.         if (F->firstchild != NULL)
  95.         {
  96.                 NodeFound(F->firstchild, n);
  97.         }
  98.         if (F->nextsibling != NULL)
  99.         {
  100.                 NodeFound(F->nextsibling, n);
  101.         }
  102.         if (F->data == n)
  103.         {
  104.                 flag = true;
  105.                 return;
  106.         }

  107. }

  108. void TForest::NodeFoundF(FSNode* F, ElemenType n, ElemenType m)
  109. {
  110.                 if (F->firstchild != NULL)
  111.                 {
  112.                         NodeFoundF(F->firstchild, n, m);
  113.                 }
  114.                 if (F->nextsibling != NULL)
  115.                 {
  116.                         NodeFoundF(F->nextsibling, n, m);
  117.                 }
  118.                 if (F->data == n)
  119.                 {
  120.                         if (F->firstchild == NULL)
  121.                         {
  122.                                 F->firstchild = new FSNode;
  123.                                 F->firstchild->data = m;
  124.                         }
  125.                         else//反向中序遍历,找到F->firstchild的空的兄弟节点,植入新的子节点
  126.                         {
  127.                                 Mchild(F->firstchild, m);
  128.                         }                       
  129.                 }

  130.        

  131. }

  132. void TForest::Tree_in()
  133. {
  134.         FSNode* c = NULL;
  135.         int i = 0, j = 0;
  136.         char a, b;
  137.         cout << "input the father and son:(enter '#' and '#' to end)" << endl;
  138.         do
  139.         {
  140.                 cin >> a;
  141.                 cin >> b;
  142.                 if (a == '#' || '#' == b)
  143.                         break;
  144.                 //此时c没有数据,第一次手动录入
  145.                 if (Tdata->data == NULL)
  146.                 {
  147.                         Tdata->data = a;
  148.                         c = Tdata;
  149.                 }       
  150.                 //第一步查找父节点a是否存在,若存在则将b加入到左子树根节点的右节点最右方(如果有的话)
  151.                 //若不存在,则在当前根的右子树中加入一个a。最后将b加于a左子树根结点处
  152.                 NodeFound(c, a);
  153.                 if (flag)//遍历;查找到a就直接接子节点,并且跳过加载a的环节
  154.                 {
  155.                         NodeFoundF(c, a, b);
  156.                 }       
  157.                 else
  158.                 {//没找到a就加载a,并接入子节点.问题是要是c都没有。。。。
  159.                         Mchild(c, a);
  160.                         NodeFoundF(c, a, b);
  161.                 }
  162.                 flag = false;
  163.         } while (a != '#' && b != '#');
  164.         //数组创建完成之后直接变化成右兄弟左孩子的二叉树
  165. }

  166. void TForest::Mchild(FSNode* F, ElemenType m)
  167. {
  168.         if (F->nextsibling != NULL)
  169.         {
  170.                 Mchild(F->nextsibling, m);                       
  171.         }
  172.         if (F->nextsibling == NULL)
  173.         {
  174.                 F->nextsibling = new FSNode;
  175.                 F->nextsibling->data = m;
  176.                 return;
  177.         }
  178. }

  179. void TForest::RenewTree(FSNode* T)
  180. {
  181.         if (T->firstchild != NULL)
  182.         {
  183.                 RenewTree(T->firstchild);
  184.         }
  185.         if (T->nextsibling != NULL)
  186.         {
  187.                 RenewTree(T->nextsibling);
  188.         }
  189.         delete T;
  190.         T = NULL;
  191. }

  192. void TForest::CouNode(FSNode* T)
  193. {
  194.         if (T->firstchild != NULL)
  195.         {
  196.                 CouNode(T->firstchild);
  197.         }
  198.         if (T->nextsibling != NULL)
  199.         {
  200.                 CouNode(T->nextsibling);
  201.         }
  202.         if (T->data != NULL)
  203.         {
  204.                 nNode++;
  205.         }
  206. }

  207. void TForest::CouLeaves(FSNode* T)
  208. {
  209.         if (T->firstchild != NULL)
  210.         {
  211.                 CouLeaves(T->firstchild);
  212.         }
  213.         if (T->nextsibling != NULL)
  214.         {
  215.                 CouLeaves(T->nextsibling);
  216.         }
  217.         if (T->firstchild == NULL)
  218.         {
  219.                 nLeaves++;
  220.         }
  221. }

  222. void TForest::Getdegree(FSNode* T,int layer,int dg)
  223. {
  224.         if (T->firstchild != NULL)
  225.         {
  226.                 Getdegree(T->firstchild, layer + 1, 0);
  227.         }
  228.         if (T->nextsibling != NULL && layer != 0)
  229.         {
  230.                 Getdegree(T->nextsibling, layer, dg + 1);//
  231.         }
  232.         else if (T->nextsibling != NULL && layer == 0)
  233.         {
  234.                 Getdegree(T->nextsibling, layer, 0);
  235.         }
  236.         if (T->nextsibling == NULL)
  237.         {
  238.                 dg++;
  239.         }       
  240.         if (dg>degree)
  241.         {
  242.                 degree=dg;
  243.         }
  244. }

  245. void TForest::HHTri(FSNode* H,int h)
  246. {
  247.         cout <<" ( "<< H->data <<","<<h<<" ) ";
  248.         if (H->firstchild != NULL)
  249.         {
  250.                 HHTri(H->firstchild,h+1);
  251.         }
  252.         if (H->nextsibling != NULL)
  253.         {
  254.                 HHTri(H->nextsibling,h);
  255.         }
  256. }

  257. void TForest::GYB(FSNode* H)
  258. {
  259.         cout << H->data ;
  260.         if (H->firstchild != NULL)
  261.         {
  262.                 cout << "(";
  263.                 GYB(H->firstchild);
  264.                 cout << ")";
  265.         }
  266.         if (H->nextsibling != NULL)
  267.         {
  268.                 cout << ",";
  269.                 GYB(H->nextsibling);
  270.         }
  271. }

  272. /***********************************************************************************************************/


  273. void TForest::choose()
  274. {
  275.         int aa;
  276. label1:        cout << "Please choose the issue you want to solve:(enter -1 to exit)" << endl;
  277.         cin >> aa;
  278.         switch (aa)
  279.         {
  280.         case 0:
  281.                 no0();
  282.                 break;
  283.         case 1:
  284.                 no1();
  285.                 break;
  286.         case 2:
  287.                 no2();
  288.                 break;
  289.         case 3:
  290.                 no3();
  291.                 break;
  292.         case 4:
  293.                 no4();
  294.                 break;
  295.         case 5:
  296.                 no5();
  297.                 break;
  298.         case 6:
  299.                 no6();
  300.                 break;
  301.         case 7:
  302.                 no7();
  303.                 break;
  304.         case -1:
  305.                 return;
  306.         default:
  307.                 cout << "Wrong number!\n" << "Please choose again!" << endl;       
  308.                 break;
  309.         }
  310.         goto label1;
  311. }

  312. void TForest::no1()
  313. {
  314.         MaxHeight = 0;
  315.         cout << "前序遍历:" << endl;
  316.         H_BL(Tdata);
  317.         cout << "\n" << "层序遍历:" << endl;
  318.         Layer = 1;
  319.         MaxH(Tdata, Layer);
  320.         Layer = 0;
  321.         CreatLay(Tdata, Layer, 0);
  322.         L_BL();
  323.         cout << "\n" << "后序遍历:" << endl;
  324.         T_BL(Tdata);
  325.         cout << endl;
  326. }

  327. void TForest::no2()
  328. {
  329.         MaxHeight = 0;
  330.         Layer = 1;
  331.         MaxH(Tdata, Layer);
  332.         cout << "H: " << MaxHeight << " \n";
  333. }

  334. void TForest::no3()
  335. {
  336.         nNode = 0;
  337.         CouNode(Tdata);
  338.         cout << "The number of node: " << nNode << endl;
  339. }

  340. void TForest::no4()
  341. {
  342.         nLeaves = 0;
  343.         CouLeaves(Tdata);
  344.         cout << "The number of leaves: " << nLeaves << endl;
  345. }

  346. void TForest::no5()
  347. {
  348.         degree = 0;
  349.         Getdegree(Tdata, 0, 0);
  350.         cout << "Degree: " << degree << endl;
  351. }

  352. void TForest::no6()
  353. {
  354.         cout << "前序遍历以及对应高度:" << endl;
  355.         HHTri(Tdata, 1);
  356.         cout << endl;
  357. }

  358. void TForest::no7()
  359. {
  360.         cout << "广义表:" << endl;
  361.         GYB(Tdata);
  362.         cout << endl;
  363. }

  364. void TForest::no0()//有BUG,最后来实现
  365. {
  366.         RenewTree(Tdata);
  367.         Tdata->data = NULL;
  368.         Tdata->firstchild = NULL;
  369.         Tdata->nextsibling = NULL;
  370.         Tree_in();
  371. }
复制代码


main.cpp:
  1. #include <iostream>
  2. #include"TForest.h"

  3. using namespace std;

  4. int main()
  5. {
  6.         TForest a;
  7.         a.Tree_in();
  8.         a.choose();
  9.         std::cout << "Hello World!\n";
  10. }
复制代码


用的是VS2019编译
输入内容:
A B
A C
A D
B E
B F
C G
D H
D I
D J
E K
# #
1
-1

大佬看一看附件就好啦
最佳答案
2019-12-11 13:01:30
qq: 1440332527

测试用的数据

测试用的数据

新建文件夹.zip

46.48 KB, 下载次数: 0

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-9 12:56:04 | 显示全部楼层
我也不知道为什么数据会被无故覆盖掉,因为我看不到你的电脑屏幕,看不到你编辑器中的代码,看不到你输入了什么,看不到哪一个数据被覆盖了,我也不知道这个程序是做什么的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-10 16:44:46 | 显示全部楼层
本帖最后由 MFwxy 于 2019-12-10 16:49 编辑
人造人 发表于 2019-12-9 12:56
我也不知道为什么数据会被无故覆盖掉,因为我看不到你的电脑屏幕,看不到你编辑器中的代码,看不到你输入了 ...


着实欠缺考虑,抱歉抱歉。
数据录入的问题已经解决了,现在正在着手遍历时有数据没有被遍历到的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-11 07:18:43 | 显示全部楼层
人造人 发表于 2019-12-9 12:56
我也不知道为什么数据会被无故覆盖掉,因为我看不到你的电脑屏幕,看不到你编辑器中的代码,看不到你输入了 ...

不好意思,,,下次我先想个一两天,再试个一两天。再问问题。基本自己解决了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-11 07:20:11 | 显示全部楼层
人造人 发表于 2019-12-9 12:56
我也不知道为什么数据会被无故覆盖掉,因为我看不到你的电脑屏幕,看不到你编辑器中的代码,看不到你输入了 ...

其中有个数组访问的问题,想请教一下大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-11 13:01:30 | 显示全部楼层    本楼为最佳答案   
qq: 1440332527
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-13 14:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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