Convert a binary tree to double list

Using traversal sequence, connect the left subtree root predecessor node and the right subtree of the root node and the subsequent root node.

  . 1 #include <the iostream>
   2  
  . 3  the using  namespace STD;
   . 4  
  . 5 Template < class T>
   . 6  struct tree_node
   . 7  {
   . 8      T Data;
   . 9      tree_node * left_child;
 10      tree_node * right_child;
 . 11  };
 12 is  
13 is  / * ***** *******************************************
 14  Function: // double_stack
 15  Description: // construct binary function using the recursive method
 16                     implemented dual binary conversion list. By
 17                    In order tree traversal method recursive traversal throughout
 18 is  the Input: // the root: root node
 . 19  the Return: None //
 20  ************************* ************************************************************ * / 
21 is Template < class T>
 22 is  void recursion (tree_node <T> * the root)
 23 is  {
 24      tree_node <T> * TEMP;
 25      IF (! root = NULL)
 26 is      {
 27          // traverse the left subtree, find the list of lists have been converted into a left subtree of a rightmost node
 28          // connected to the root node becomes a node on its left 
29          recursion ( directory root-> left_child);
 30         directory root- = TEMP> left_child;        
 31 is          IF (TEMP =! NULL)
 32          {
 33 is              the while (temp-> right_child =! NULL)
 34 is              {
 35                  TEMP = temp-> right_child;
 36              }
 37 [              temp-> = right_child the root;
 38 is              the root -> left_child = TEMP;
 39          }
 40  
41 is          // traverse right subtree, find the list of lists have been converted into a left subtree of a leftmost node
 42 is          // connected to the root node as its left node 
43 is          recursion (directory root->right_child);
 44         temp = root->right_child;
 45         if(temp != NULL)
 46         {
 47             while (temp->left_child != NULL)
 48             {
 49                 temp = temp->left_child;
 50             }
 51             temp->left_child = root;
 52             root->right_child = temp;
 53         }
 54     }
 55 }
 56 
 57 /*************************************************
58  Function: // trans_algorithm
 59  the Description: // recursive call to the function package
 60  the Input: // root node of the binary tree the root
 61 is  the Return: None //
 62  ******************* **************************************************************** * / 
63 is Template < class T>
 64  void trans_algorithm (tree_node <T> * & the root)
 65  {
 66      recursion (the root);
 67      IF (! the root = NULL)
 68      {
 69          the while (! directory root-> left_child = NULL)
 70          {
 71 is             = directory root- the root> left_child;
 72          }
 73 is      }
 74  }
 75  
76  / * ********************************* ***************
 77  Function: // insert_node
 78  the Description: // insert a node into a tree in the tree
 79  the Input: // the root: root of the tree
 80                  // Data : value tree node
 81  the Return: None //
 82  *************************************** *************************************** * / 
83 Template < class T>
 84  static  void insert_node (tree_node <T> * the root, T Data)
 85 {
 86     tree_node<int> *new_node = new tree_node<int>;
 87     new_node->left_child = NULL;
 88     new_node->right_child = NULL;
 89     new_node->data = data;
 90     
 91     tree_node<int> *temp, *temp1;
 92     temp = root;
 93     while(temp != NULL)
 94     {
 95         temp1 = temp;
 96         if(temp->data > data)
 97         {                
 98             temp = temp->left_child;
 99         }
100         else
101         {
102             temp = temp->right_child;
103         }
104     }
105     if(temp1->data > data)
106     {
107         temp1->left_child = new_node;
108     }
109     else
110     {
111         temp1->right_child = new_node;
112     }
113 }
114 
115 void main()
116 {    
117     //构建一个排序二叉树
118     tree_node<int> *root = new tree_node<int>;
119     tree_node<int> *temp, *temp1;
120     root->left_child = NULL;
121     root->right_child = NULL;
122     root->data = 10;    
123     insert_node(root, 6);
124     insert_node(root, 4);
125     insert_node(root, 8);
126     insert_node(root, 3);
127     insert_node(root, 5);
128     insert_node(root, 7);
129     insert_node(root, 9);
130     insert_node(root, 2);
131     insert_node(root, 13);
132     insert_node(root, 12);
133     insert_node(root, 17);
134     insert_node(root, 14);
135     insert_node(root, 16 );
 136      insert_node (the root, 15 );
 137      insert_node (the root, . 1 );
 138      insert_node (the root, 20 is );
 139      
140      // invoking a transform function 
141 is      trans_algorithm (the root);
 142      // delete nodes and the nodes allocated memory space 
143      the while (= the root! NULL)
 144      {
 145          COUT << directory root-> Data << "  " ;
 146          TEMP = the root;
 147          the root directory root- => right_child;
 148         delete temp;
149     }
150 }

 

Reproduced in: https: //www.cnblogs.com/liuxg/archive/2013/04/08/3007584.html

Guess you like

Origin blog.csdn.net/weixin_34326558/article/details/93433629