编程将二叉树(子女右兄弟链)转换为树然后将树转换为二叉树(直接修改) 非递归实现

Posted wskit

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程将二叉树(子女右兄弟链)转换为树然后将树转换为二叉树(直接修改) 非递归实现相关的知识,希望对你有一定的参考价值。

将子女右兄弟链修改为普通树然后将普通树修改为子女右兄弟链,注意是在原树上直接进行修改,不是根据原树创建转换后的新树,为了操作方便,树指针域用vector表示

C++代码:

子女右兄弟链存在共享子树情形:

 

  1 #include "stdafx.h"
  2 #include <iostream>
  3 #include <stack>
  4 #include <vector>
  5 #include <string>
  6 #include <map>
  7 using namespace std;
  8 
  9 class Dnode           //二叉树和多叉树节点类
 10 {
 11 public:
 12     char data;       //树节点数据域
 13     vector<Dnode *> p;        //指针域vector
 14     Dnode(char d = \0) :data(d)
 15     {
 16         p.push_back(nullptr);
 17         p.push_back(nullptr);
 18     }
 19 };
 20 
 21 class dtreestacknode   //遍历树时记录回退路径的栈节点
 22 {
 23 public:
 24     Dnode *ptr;    //树节点指针
 25     int direction;   //回退方向
 26     dtreestacknode(int d, Dnode *p) :direction(d), ptr(p) {}
 27 };
 28 
 29 class info     //映射表中关键字对应的值类型
 30 {
 31 public:
 32     int count = 0;    //同一子树的引用计数
 33     Dnode *p = nullptr;  //子树附加头节点指针
 34     info(int c, Dnode *ptr) :count(c), p(ptr) {}
 35 };
 36 
 37 class infoshare
 38 {
 39 public:
 40     vector<int> left;
 41     vector<int> right;
 42     Dnode *share = nullptr;
 43     vector<int>::size_type position = 0;
 44 };
 45 
 46 Dnode *strtodtree();   //广义表转换为子女右兄弟链表示的二叉树,返回二叉树根节点指针
 47 int Searchd(Dnode *ptr, int d);  //选择二叉树下一路径方向的函数
 48 int Search(Dnode *ptr, int d);   //选择多叉树下一路径方向的函数
 49 void suboutput(Dnode *ptr);      //输出子女右兄弟链表示
 50 int Reversesearchd(Dnode *ptr, int d);   //反向遍历二叉树时选择下一路径的函数
 51 void creatsharelist(string &glist, map<string, infoshare> &sharelist);
 52 map<string, infoshare>::iterator Searchshare(int left, map<string, infoshare> &sharelist);
 53 
 54 int main()
 55 {
 56     Dnode *ptr = strtodtree();  //由广义表创建二叉树
 57     const Dnode *const dest = ptr;
 58     stack<dtreestacknode> dstack;   //记录遍历回退路径的栈
 59     stack<Dnode *> tstack;     //记录已遍历二叉树层次结构的栈
 60 
 61     map<Dnode *, info> share;  //映射表,保存子树第一个节点-对应info对象的键值对
 62     int d = 3;
 63 
 64     while (true)   //遍历子女右兄弟链二叉树建表
 65     {
 66         int interval;
 67         if ((interval = Reversesearchd(ptr, d)) == 0)
 68         {
 69             if (ptr == dest)
 70             {
 71                 if (d != 3)
 72                     dstack.pop();
 73                 break;
 74             }
 75             else
 76             {
 77                 if (d == 1)
 78                 {
 79                     dstack.pop();
 80                 }
 81                 ptr = dstack.top().ptr;
 82                 d = dstack.top().direction;
 83             }
 84         }
 85         else
 86         {
 87             Dnode *dir = nullptr;
 88             if (d == 3)
 89             {
 90                 if (ptr != dest)
 91                 {
 92                     if (interval == 2)
 93                     {
 94                         dir = ptr->p[1];
 95                         if (ptr->p[0] == nullptr)
 96                         {
 97                             d = 3;
 98                             ptr = dir;
 99                             continue;
100                         }
101                     }
102                     else
103                     {
104                         auto p = share.find(ptr->p[0]);
105                         if (p != share.end())
106                         {
107                             ++p->second.count;
108                             ptr = dstack.top().ptr;
109                             d = dstack.top().direction;
110                             continue;
111                         }
112                         else
113                         {
114                             info temp(1, ptr);
115                             share.insert(make_pair(ptr->p[0], temp));
116                             dir = ptr->p[0];
117                         }
118                     }
119                 }
120                 else
121                 {
122                     dir = ptr->p[0];
123                 }
124                 dtreestacknode temp(interval, ptr);
125                 dstack.push(temp);
126             }
127             else
128             {
129                 auto p = share.find(ptr->p[0]);
130                 if (p != share.end())
131                 {
132                     ++p->second.count;
133                     dstack.pop();
134                     ptr = dstack.top().ptr;
135                     d = dstack.top().direction;
136                     continue;
137                 }
138                 else
139                 {
140                     info temp(1, ptr);
141                     share.insert(make_pair(ptr->p[0], temp));
142                     dir = ptr->p[0];
143                     dstack.top().direction = interval;
144                 }
145             }
146             d = 3;
147             ptr = dir;
148         }
149     }
150 
151     {
152         auto m = share.begin();        //删除非共享子表对应的记录
153         while (m != share.end())
154         {
155             if (m->second.count == 1)
156                 m = share.erase(m);
157             else
158                 ++m;
159         }
160     }
161 
162     d = 3;
163     int flag = 0;   //重要变量,记录最近二叉树分支节点左链指针域是否被修改过
164     while (true)     //循环,将vector容器实现指针域的子女右兄弟链二叉树直接修改为普通树,不是另外新建等价地普通树
165     {
166         if (Reversesearchd(ptr, d) == 0)
167         {
168             if (ptr == dest)
169             {
170                 if (d == 3)
171                     ptr->p.clear();
172                 else
173                 {
174                     dstack.pop();
175                     if (!tstack.empty())
176                        tstack.pop();
177                 }
178                 break;
179             }
180             else
181             {
182                 if (d == 3)   ////////
183                 {
184                     if (!(dstack.top().ptr->p[0] != nullptr && dstack.top().direction == 1) || dstack.top().ptr->p[0] != ptr)
185                     {
186                         tstack.top()->p.push_back(ptr);
187                         if ((dstack.top().ptr->p[0] != nullptr && dstack.top().direction == 1) && dstack.top().ptr->p[0] != ptr)
188                             tstack.pop();
189                     }
190                     else
191                     {
192                         tstack.pop();
193                     }
194                     ptr->p.clear();
195                 }
196                 else
197                 {
198                     if (d == 1)
199                         tstack.pop();
200                     else
201                         ptr->p.clear();
202                     dstack.pop();
203                 }
204                 ptr = dstack.top().ptr;
205                 d = dstack.top().direction;
206             }
207         }
208         else
209         {
210             Dnode *interval = nullptr;
211             if (d == 3)
212             {
213                 if (ptr != dest)
214                 {
215                     if (!(dstack.top().ptr->p[0] != nullptr && dstack.top().direction == 1))
216                     {
217                         if (ptr->p[0] != nullptr)
218                         {
219                             auto p = share.find(ptr->p[0]);
220                             if (p != share.end())
221                             {
222                                 if (--p->second.count != 0)
223                                 {
224                                     dstack.top().ptr->p[1] = ptr->p[1];
225                                     delete ptr;
226                                     tstack.top()->p.push_back(p->second.p);
227 
228                                     if (dstack.top().ptr->p[1] != nullptr)
229                                     {
230                                         ptr = dstack.top().ptr->p[1];
231                                         d = 3;
232                                     }
233                                     else
234                                     {
235                                         ptr = dstack.top().ptr;
236                                         d = dstack.top().direction;
237                                     }
238                                 }
239                                 else
240                                 {
241                                     tstack.top()->p.push_back(ptr);
242                                     if (ptr->p[1] != nullptr)    ///////
243                                     {
244                                         dtreestacknode temp(2, ptr);
245                                         dstack.push(temp);
246                                         ptr = ptr->p[1];
247                                         d = 3;
248                                     }
249                                     else
250                                     {
251                                         flag = 0;
252                                         ptr->p.pop_back();
253                                         tstack.push(ptr);
254                                         dtreestacknode temp(1, ptr);
255                                         dstack.push(temp);
256                                         ptr = ptr->p[0];
257                                         d = 3;
258                                     }
259                                 }
260                                 continue;
261                             }
262                         }
263                         tstack.top()->p.push_back(ptr);
264                     }
265                     else
266                     {
267                         if (ptr->p[0] != nullptr)
268                         {
269                             auto p = share.find(ptr->p[0]);
270                             if (p != share.end())
271                             {
272                                 --p->second.count;
273                                 if (dstack.top().ptr->p[0] == ptr)
274                                 {
275                                     if (p->second.count != 0)
276                                     {
277                                         dstack.top().ptr->p[0] = p->second.p;
278                                         flag = 1;
279                                     }
280                                     else
281                                     {
282                                         if (flag==1)
283                                             dstack.top().ptr->p.push_back(p->second.p);
284                                     }
285                                 }
286                                 else
287                                 {
288                                     dstack.top().ptr->p.push_back(p->second.p);
289                                 }
290 
291                                 if (p->second.count != 0)
292                                 {
293                                     Dnode *temp = ptr->p[1];
294                                     delete ptr;
295 
296                                     if (temp != nullptr)
297                                     {
298                                         ptr = temp;
299                                         d = 3;
300                                     }
301                                     else
302                                     {
303                                         ptr = dstack.top().ptr;
304                                         d = dstack.top().direction;
305                                     }
306                                 }
307                                 else
308                                 {
309                                     if (ptr->p[1] != nullptr)  //////
310                                     {
311                                         dtreestacknode temp(2, ptr);
312                                         dstack.push(temp);
313                                         ptr = ptr->p[1];
314                                         d = 3;
315                                     }
316                                     else
317                                     {
318                                         flag = 0;
319                                         ptr->p.pop_back();
320                                         tstack.push(ptr);
321                                         dtreestacknode temp(1, ptr);
322                                         dstack.push(temp);
323                                         ptr = ptr->p[0];
324                                         d = 3;
325                                     }
326                                 }
327                                 continue;
328                             }
329                         }
330                         if (dstack.top().ptr->p[0] != ptr)
331                             tstack.top()->p.push_back(ptr);
332                     }
333 
334                     if (ptr->p[1] != nullptr)
335                     {
336                         dtreestacknode temp(2, ptr);
337                         dstack.push(temp);
338                         ptr = ptr->p[1];
339                         d = 3;
340                         continue;
341                     }
342                 }
343                 dtreestacknode temp(1, ptr);
344                 dstack.push(temp);
345             }
346             else
347             {
348                 dstack.top().direction = 1;
349             }
350             flag = 0;
351             ptr->p.pop_back();
352             interval = ptr->p[0];
353             tstack.push(ptr);
354 
355             ptr = interval;
356             d = 3;
357         }
358     }
359     cout << "已将子女右兄弟链表示转换为普通树" << endl;
360 
361     d = 0;
362     while (true)  //循环,将普通树直接修改为子女右兄弟链二叉树,非另外新建
363     {
364         if (Search(ptr, d) == 0)
365         {
366             if (d == 0)
367             {
368                 ptr->p.push_back(nullptr);
369                 ptr->p.push_back(nullptr);
370                 if (ptr == dest)
371                 {
372                     break;
373                 }
374                 else
375                 {
376                     if (dstack.top().direction != 1)
377                     {
378                         dstack.top().ptr->p[dstack.top().direction - 2]->p[1] = ptr;
379                     }
380                     ptr = dstack.top().ptr;
381                     d = dstack.top().direction;
382                 }
383             }
384             else
385             {
386                 if (ptr->p.size() == 1)
387                 {
388                     ptr->p.push_back(nullptr);
389                 }
390                 else
391                 {
392                     if (ptr->p.size() > 2)
393                     {
394                         while (ptr->p.size() > 2)
395                             ptr->p.pop_back();
396                     }
397                     ptr->p[1] = nullptr;
398                 }
399                 dstack.pop();
400                 if (ptr == dest)
401                     break;
402                 else
403                 {
404                     ptr = dstack.top().ptr;
405                     d = dstack.top().direction;
406                 }
407             }
408         }
409         else
410         {
411             Dnode *interval = nullptr;
412             if (d == 0)
413             {
414                 if (ptr != dest)
415                 {
416                     auto p = share.find(ptr->p[0]);
417                     if (p != share.end())
418                     {
419                         if (p->second.count == 0)
420                         {
421                             p->second.count = 1;
422                         }
423                         else
424                         {
425                             Dnode *temp = new Dnode();
426                             temp->p[0] = ptr->p[0];
427                             dstack.top().ptr->p[dstack.top().direction - 1] = temp;
428                             if (dstack.top().direction != 1)
429                             {
430                                 dstack.top().ptr->p[dstack.top().direction - 2]->p[1] = temp;
431                             }
432                             ptr = dstack.top().ptr;
433                             d = dstack.top().direction;
434                             continue;
435                         }
436                     }
437                     if (dstack.top().direction != 1)
438                     {
439                         dstack.top().ptr->p[dstack.top().direction - 2]->p[1] = ptr;
440                     }
441                 }
442                 dtreestacknode temp(Search(ptr, d), ptr);
443                 dstack.push(temp);
444                 interval = ptr->p[Search(ptr, d) - 1];
445             }
446             else
447             {
448                 dstack.top().direction = Search(ptr, d);
449                 interval = ptr->p[Search(ptr, d) - 1];
450             }
451             ptr = interval;
452             d = 0;
453         }
454     }
455     cout << "已将普通树转换为子女右兄弟链表示" << endl;
456     cout << "转换后的子女右兄弟莲表示对应的广义表为" << endl;
457     suboutput(ptr);   //输出最终转换结果
458     return 0;
459 }
460 
461 Dnode *strtodtree()
462 {
463     cout << "请输入广义表字符串形式" << endl;
464     string glist;
465     cin >> glist;
466 
467     map<string, infoshare> sharelist;
468     creatsharelist(glist, sharelist);
469 
470     stack<Dnode *> arrange;
471     Dnode *ptr = nullptr;
472 
473     map<string, infoshare>::iterator p;
474 
475     for (string::size_type i = 0; i != glist.size(); i++)
476     {
477         if (glist[i] == ()
478         {
479             if (i == 0)
480             {
481                 ptr = new Dnode();
482                 arrange.push(ptr);
483             }
484             else
485             {
486                 Dnode *temp = new Dnode();
487                 if (arrange.top() == ptr)
488                 {
489                     if (arrange.size() != 1)
490                     {
491                         if (p != sharelist.end())
492                         {
493                             p->second.share = temp;
494                         }
495                     }
496                     ptr->p[0] = temp;
497                 }
498                 else
499                 {
500                     ptr->p[1] = temp;
501                 }
502                 ptr = temp;
503 
504                 if (glist[i + 1] != ))
505                 {
506                     p = Searchshare(i + 1, sharelist);
507                     if (p != sharelist.end())
508                     {
509                         if (p->second.share != nullptr)
510                         {
511                             i = p->second.right[p->second.position - 1] - 1;
512                             temp->p[0] = p->second.share;
513                             continue;
514                         }
515                     }
516                 }
517                 arrange.push(ptr);
518             }
519         }
520         else
521         {
522             if (glist[i] == ))
523             {
524                 ptr = arrange.top();
525                 arrange.pop();
526             }
527             else
528             {
529                 if (glist[i] != ,)
530                 {
531                     Dnode *temp = new Dnode(glist[i]);
532                     if (ptr == arrange.top())
533                     {
534                         if (p != sharelist.end())
535                         {
536                             p->second.share = temp;
537                         }
538                         ptr->p[0] = temp;
539                     }
540                     else
541                         ptr->p[1] = temp;
542                     ptr = temp;
543                 }
544             }
545         }
546     }
547     cout << "已将广义表字符串形式转化为子女右兄弟链表示" << endl;
548     return ptr;
549 }
550 
551 int Searchd(Dnode *ptr, int d)
552 {
553     if (d == 2)
554         return 0;
555     else
556     {
557         if (d == 1)
558         {
559             if (ptr->p[1] == nullptr)
560                 return 0;
561             else
562                 return 2;
563         }
564         else
565         {
566             if (ptr->p[0] != nullptr)
567                 return 1;
568             else
569             {
570                 if (ptr->p[1] != nullptr)
571                     return 2;
572                 else
573                     return 0;
574             }
575         }
576     }
577 }
578 
579 int Reversesearchd(Dnode *ptr, int d)
580 {
581     if (d == 1)
582         return 0;
583     else
584     {
585         if (d == 2)
586         {
587             if (ptr->p[0] == nullptr)
588                 return 0;
589             else
590                 return 1;
591         }
592         else
593         {
594             if (ptr->p[1] != nullptr)
595                 return 2;
596             else
597             {
598                 if (ptr->p[0] != nullptr)
599                     return 1;
600                 else
601                     return 0;
602             }
603         }
604     }
605 }
606 
607 int Search(Dnode *ptr, int d)
608 {
609     if (d < ptr->p.size())
610         return d + 1;
611     else
612         return 0;
613 }
614 
615 void suboutput(Dnode *ptr)
616 {
617     stack<Dnode *> arrange;
618     int d = 0;
619     Dnode *const dest = ptr;
620 
621     cout << "转换后的子女右兄弟链表示对应的广义表形式为:";
622     cout << "(";
623     while (true)
624     {
625         if (Searchd(ptr, d) == 0)
626         {
627             if (ptr == dest)
628             {
629                 if (d == 0)
630                     cout << );
631                 break;
632             }
633             else
634             {
635                 if (d == 0)
636                 {
637                     if (ptr->data == \0)
638                         cout << "()";
639                     else
640                         cout << ptr->data;
641                     cout << ")";
642                 }
643                 else
644                 {
645                     cout << ")";
646                 }
647                 ptr = arrange.top();
648                 d = 1;
649                 arrange.pop();
650             }
651         }
652         else
653         {
654             Dnode *interval = nullptr;
655             if (d == 0)
656             {
657                 if (ptr->p[0] != nullptr)
658                 {
659                     if (ptr != dest)
660                         cout << "(";
661                     arrange.push(ptr);
662                     interval = ptr->p[0];
663                 }
664                 else
665                 {
666                     if (ptr->data == \0)
667                     {
668                         cout << "()";
669                     }
670                     else
671                     {
672                         cout << ptr->data;
673                     }
674                     cout << ",";
675                     interval = ptr->p[1];
676                 }
677             }
678             else
679             {
680                 cout << ",";
681                 interval = ptr->p[1];
682             }
683             d = 0;
684             ptr = interval;
685         }
686     }
687     cout << endl;
688 }
689 
690 void creatsharelist(string &glist, map<string, infoshare> &sharelist)
691 {
692     vector<int> stack;
693     int total = 0;
694     for (const auto &s : glist)
695     {
696         if (s == ()
697         {
698             ++total;
699             stack.push_back(total);
700         }
701         else if (s == ))
702         {
703             ++total;
704             string temp = glist.substr(stack[stack.size() - 1] - 1, total - stack[stack.size() - 1] + 1);
705             auto p = sharelist.find(temp);
706             if (p == sharelist.end())
707             {
708                 infoshare m;
709                 auto q = sharelist.insert(make_pair(temp, m));
710                 q.first->second.left.push_back(stack[stack.size() - 1]);
711                 q.first->second.right.push_back(total);
712             }
713             else
714             {
715                 p->second.left.push_back(stack[stack.size() - 1]);
716                 p->second.right.push_back(total);
717             }
718             stack.pop_back();
719         }
720         else
721         {
722             ++total;
723         }
724     }
725 
726     auto m = sharelist.begin();
727     while (m != sharelist.end())
728     {
729         if (m->second.left.size() == 1 || m->first == "()")
730             m = sharelist.erase(m);
731         else
732             ++m;
733     }
734 }
735 
736 map<string, infoshare>::iterator Searchshare(int left, map<string, infoshare> &sharelist)
737 {
738     auto p = sharelist.begin();
739     for (; p != sharelist.end(); ++p)
740     {
741         if (p->second.left.size() != p->second.position && p->second.left[p->second.position] == left)
742         {
743             ++p->second.position;
744             return p;
745         }
746     }
747     return p;
748 }

运行结果:

技术分享图片

子女右兄弟链无共享子树情形(相对简单):

  1 #include "stdafx.h"
  2 #include <iostream>
  3 #include <stack>
  4 #include <vector>
  5 #include <string>
  6 using namespace std;
  7 
  8 class Dnode
  9 {
 10 public:
 11     char data;
 12     vector<Dnode *> p;
 13     Dnode(char d = \0) :data(d) 
 14     {
 15         p.push_back(nullptr);
 16         p.push_back(nullptr);
 17     }
 18 };
 19 
 20 class dtreestacknode
 21 {
 22 public:
 23     Dnode *ptr;
 24     int direction;
 25     dtreestacknode(int d, Dnode *p) :direction(d), ptr(p) {}
 26 };
 27 
 28 Dnode *strtodtree();
 29 int Searchd(Dnode *ptr, int d);
 30 int Search(Dnode *ptr, int d);
 31 void suboutput(Dnode *ptr);
 32 int Reversesearchd(Dnode *ptr, int d);
 33 
 34 int main()
 35 {
 36     Dnode *ptr = strtodtree();
 37     const Dnode *const dest = ptr;
 38     stack<dtreestacknode> dstack;
 39     stack<Dnode *> tstack;
 40     int d = 3;
 41 
 42     while (true)
 43     {
 44         if (Reversesearchd(ptr, d) == 0)
 45         {
 46             if (ptr == dest)
 47             {
 48                 if (d == 3)
 49                     ptr->p.clear();
 50                 else
 51                     dstack.pop();
 52                 break;
 53             }
 54             else
 55             {
 56                 if (d == 3)
 57                 {
 58                     if (!(dstack.top().ptr->p[0] != nullptr && dstack.top().direction == 1))
 59                         tstack.top()->p.push_back(ptr);
 60 
 61                     ptr->p.clear();
 62                 }
 63                 else
 64                 {
 65                     if (d == 1)
 66                         tstack.pop();
 67                     else
 68                         ptr->p.clear();
 69                     dstack.pop();
 70                 }
 71                 ptr = dstack.top().ptr;
 72                 d = dstack.top().direction;
 73             }
 74         }
 75         else
 76         {
 77             Dnode *interval = nullptr;
 78             if (d == 3)
 79             {
 80                 if (ptr != dest)
 81                 {
 82                     if (!(dstack.top().ptr->p[0] != nullptr && dstack.top().direction == 1))
 83                         tstack.top()->p.push_back(ptr);
 84 
 85                     if (ptr->p[1] != nullptr)
 86                     {
 87                         dtreestacknode temp(2, ptr);
 88                         dstack.push(temp);
 89                         interval = ptr->p[1];
 90                         ptr = interval;
 91                         d = 3;
 92                         continue;
 93                     }
 94                 }
 95                 ptr->p.pop_back();
 96                 dtreestacknode temp(1, ptr);
 97                 dstack.push(temp);
 98                 interval = ptr->p[0];
 99                 tstack.push(ptr);
100             }
101             else
102             {
103                 ptr->p.pop_back();
104                 dstack.top().direction = 1;
105                 interval = ptr->p[0];
106                 tstack.push(ptr);
107             }
108             ptr = interval;
109             d = 3;
110         }
111     }
112 
113     cout << "已将子女右兄弟链表示转换为普通树"<<endl;
114     d = 0;
115     while (true)
116     {
117         if (Search(ptr, d) == 0)
118         {
119             if (d == 0)
120             {
121                 ptr->p.push_back(nullptr);
122                 ptr->p.push_back(nullptr);
123                 if (ptr == dest)
124                 {
125                     break;
126                 }
127                 else
128                 {
129                     if (dstack.top().direction != 1)
130                     {
131                         dstack.top().ptr->p[dstack.top().direction - 2]->p[1] = ptr;
132                     }
133                     ptr = dstack.top().ptr;
134                     d = dstack.top().direction;
135                 }
136             }
137             else
138             {
139                 if (ptr->p.size() == 1)
140                 {
141                     ptr->p.push_back(nullptr);
142                 }
143                 else
144                 {
145                     if (ptr->p.size() > 2)
146                     {
147                         while (ptr->p.size() > 2)
148                             ptr->p.pop_back();
149                     }
150                     ptr->p[1] = nullptr;
151                 }
152                 dstack.pop();
153                 if (ptr == dest)
154                     break;
155                 else
156                 {
157                     ptr = dstack.top().ptr;
158                     d = dstack.top().direction;
159                 }
160             }
161         }
162         else
163         {
164             Dnode *interval=nullptr;
165             if (d == 0)
166             {
167                 if (ptr != dest)
168                 {
169                     if (dstack.top().direction != 1)
170                     {
171                         dstack.top().ptr->p[dstack.top().direction - 2]->p[1] = ptr;
172                     }
173                 }
174                 dtreestacknode temp(Search(ptr, d), ptr);
175                 dstack.push(temp);
176                 interval = ptr->p[Search(ptr, d) - 1];
177             }
178             else
179             {
180                 dstack.top().direction = Search(ptr, d);
181                 interval= ptr->p[Search(ptr, d) - 1];
182             }
183             ptr = interval;
184             d = 0;
185         }
186     }
187     cout<<"已将普通树转换为子女右兄弟链表示"<<endl;
188     suboutput(ptr);
189     return 0;
190 }
191 
192 Dnode *strtodtree()
193 {
194     cout << "请输入广义表字符串形式" << endl;
195     string glist;
196     cin >> glist;
197     stack<Dnode *> arrange;
198     Dnode *ptr = nullptr;
199 
200     for (string::size_type i = 0; i != glist.size(); i++)
201     {
202         if (glist[i] == ()
203         {
204             if (i == 0)
205             {
206                 ptr = new Dnode();
207                 arrange.push(ptr);
208             }
209             else
210             {
211                 Dnode *temp = new Dnode();
212                 if (arrange.top() == ptr)
213                 {
214                     ptr->p[0] = temp;
215                 }
216                 else
217                 {
218                     ptr->p[1] = temp;
219                 }
220                 ptr = temp;
221                 arrange.push(ptr);
222             }
223         }
224         else
225         {
226             if (glist[i] == ))
227             {
228                 ptr = arrange.top();
229                 arrange.pop();
230             }
231             else
232             {
233                 if (glist[i] != ,)
234                 {
235                     Dnode *temp = new Dnode(glist[i]);
236                     if (ptr == arrange.top())
237                         ptr->p[0] = temp;
238                     else
239                         ptr->p[1] = temp;
240                     ptr = temp;
241                 }
242             }
243         }
244     }
245     cout << "已将广义表字符串形式转化为子女右兄弟链表示" << endl;
246     return ptr;
247 }
248 
249 int Searchd(Dnode *ptr, int d)
250 {
251     if (d == 2)
252         return 0;
253     else
254     {
255         if (d == 1)
256         {
257             if (ptr->p[1] == nullptr)
258                 return 0;
259             else
260                 return 2;
261         }
262         else
263         {
264             if (ptr->p[0] != nullptr)
265                 return 1;
266             else
267             {
268                 if (ptr->p[1] != nullptr)
269                     return 2;
270                 else
271                     return 0;
272             }
273         }
274     }
275 }
276 
277 int Reversesearchd(Dnode *ptr, int d)
278 {
279     if (d == 1)
280         return 0;
281     else
282     {
283         if (d == 2)
284         {
285             if (ptr->p[0] == nullptr)
286                 return 0;
287             else
288                 return 1;
289         }
290         else
291         {
292             if (ptr->p[1] != nullptr)
293                 return 2;
294             else
295             {
296                 if (ptr->p[0] != nullptr)
297                     return 1;
298                 else
299                     return 0;
300             }
301         }
302     }
303 }
304 
305 int Search(Dnode *ptr, int d)
306 {
307     if (d < ptr->p.size())
308         return d + 1;
309     else
310         return 0;
311 }
312 
313 void suboutput(Dnode *ptr)
314 {
315     stack<Dnode *> arrange;
316     int d = 0;
317     Dnode *const dest = ptr;
318 
319     cout << "转换后的子女右兄弟链表示对应的广义表形式为:";
320     cout << "(";
321     while (true)
322     {
323         if (Searchd(ptr, d) == 0)
324         {
325             if (ptr == dest)
326             {
327                 if (d == 0)
328                     cout << );
329                 break;
330             }
331             else
332             {
333                 if (d == 0)
334                 {
335                     if (ptr->data == \0)
336                         cout << "()";
337                     else
338                         cout << ptr->data;
339                     cout << ")";
340                 }
341                 else
342                 {
343                     cout << ")";
344                 }
345                 ptr = arrange.top();
346                 d = 1;
347                 arrange.pop();
348             }
349         }
350         else
351         {
352             Dnode *interval = nullptr;
353             if (d == 0)
354             {
355                 if (ptr->p[0] != nullptr)
356                 {
357                     if (ptr != dest)
358                         cout << "(";
359                     arrange.push(ptr);
360                     interval = ptr->p[0];
361                 }
362                 else
363                 {
364                     if (ptr->data == \0)
365                     {
366                         cout << "()";
367                     }
368                     else
369                     {
370                         cout << ptr->data;
371                     }
372                     cout << ",";
373                     interval = ptr->p[1];
374                 }
375             }
376             else
377             {
378                 cout << ",";
379                 interval = ptr->p[1];
380             }
381             d = 0;
382             ptr = interval;
383         }
384     }
385     cout << endl;
386 }

运行结果:

技术分享图片

以上是关于编程将二叉树(子女右兄弟链)转换为树然后将树转换为二叉树(直接修改) 非递归实现的主要内容,如果未能解决你的问题,请参考以下文章

如何将二叉树就地转换为二叉搜索树,即我们不能使用任何额外的空间

树森林二叉树的转换原理

树森林二叉树的转换原理

树/森林和二叉树的转换

将二叉树转为有序的双向链表

将二叉查找树转换成有序双向链表