[C++] 数据结构与算法----2.8 实现森林的层序遍历算法 →→→→→进入此内容的聊天室

来自 , 2021-03-16, 写在 C++, 查看 114 次.
URL http://www.code666.cn/view/f3e52c30
  1. #include <iostream>
  2. using namespace std;
  3. typedef char TElemType;
  4. typedef struct TNode
  5. {
  6.         TElemType data;
  7.         TNode*parent,*fc,*ns;
  8. }FNode,*tree,*forest;
  9. typedef forest QElemType;
  10. typedef struct QNode
  11. {
  12.         QElemType data; QNode *next;
  13. }*LQueuePtr;
  14. struct LQueue{ LQueuePtr front,rear;};
  15. void QueueInit(LQueue &Q);//队初始化
  16. void Enqueue(LQueue &Q,QElemType e);//入队
  17. bool Dequeue(LQueue &Q,QElemType &e);//出队
  18. void ForestLists(tree T);
  19. void TreeLists(tree T);
  20. void CreateForest(forest &T, char s[], int &i);
  21. void CreateForest(forest &T, char s[]);
  22. void ForestLevelOrder(forest F, void visit(TElemType));//森林的层序遍历输出
  23. void visit(TElemType data);
  24. int main()
  25. {
  26.         forest F;
  27.         char s[]="BC#D##F##";
  28.         CreateForest(F,s);
  29.         cout<<"该森林为:"<<endl;
  30.         ForestLists(F);
  31.         cout<<endl<<"该森林的层序遍历输出为:";
  32.         ForestLevelOrder( F,visit);
  33.         cout<<endl;
  34.         return 0;
  35. }
  36. void QueueInit(LQueue &Q)
  37. {
  38.         Q.front=new QNode;
  39.         Q.front->next=NULL;
  40.         Q.rear=Q.front;
  41. }
  42. void Enqueue(LQueue &Q,QElemType e)
  43. {
  44.         LQueuePtr p;
  45.         p=new QNode;p->data=e;p->next=NULL;
  46.         Q.rear->next=p;Q.rear=p;
  47. }
  48. bool Dequeue(LQueue &Q,QElemType &e)
  49. {
  50.         LQueuePtr p;
  51.         if(Q.front==Q.rear) return false;
  52.         p=Q.front;Q.front=p->next;e=Q.front->data;
  53.         delete p;return true;  
  54. }
  55. void CreateForest(forest &T, char s[], int &i)
  56. {  
  57.         i++;
  58.         if (s[i] == '#') { T = NULL; return; }
  59.         T = new TNode;
  60.         T->data = s[i];      //建立根节点
  61.         CreateForest(T->fc, s, i);
  62.         CreateForest(T->ns, s, i);
  63. }
  64. void CreateForest(forest &T, char s[])
  65. {  
  66.         int i = -1;
  67.         CreateForest(T, s, i);
  68. }
  69. void TreeLists(tree T)
  70. {
  71.         tree p;
  72.         if (!T) { cout << "#"; return; }
  73.         cout<<T->data;
  74.         p = T->fc;    
  75.         if (!p)return;
  76.         cout << "(";
  77.         while(p){
  78.                 TreeLists(p);
  79.                 p = p->ns;
  80.                 if (p)cout << ",";
  81.         }
  82.         cout << ")";
  83. }
  84. void ForestLists(tree T)
  85. {                    
  86.         forest p = T;
  87.         cout << "(";
  88.         while (p) {
  89.                 TreeLists(p);
  90.                 p = p->ns;
  91.                 if (p)cout << ",";
  92.         }
  93.         cout << ")";
  94. }
  95. void visit(TElemType data)
  96.  {
  97.         cout << data;
  98. }
  99. void ForestLevelOrder(forest F, void visit(TElemType))
  100. {
  101.         LQueue q;forest x,y;
  102.         if(!F)return ;
  103.         QueueInit(q);
  104.         for (F; F; F = F->ns)
  105.         {
  106.                 Enqueue(q,F);
  107.         }
  108.         while(Dequeue(q,x))
  109.         {
  110.                 visit(x->data);
  111.                 for(y=x->fc;y;y=y->ns)          Enqueue(q,y);
  112.         }
  113. }

回复 "数据结构与算法----2.8 实现森林的层序遍历算法"

这儿你可以回复上面这条便签

captcha