数据结构——二叉树的构建与遍历

二叉树的设计实现与遍历

#include<iostream>
  2 using namespace std;
  3 
  4 typedef char DataType;//定义DataType为char类型
  5 
  6 //定义节点结构体
  7 typedef struct Node
  8 {
  9     DataType data;
 10     struct Node *leftChild;
 11     struct Node *rightChild;
 12 
 13     Node(int x): data(x),leftChild(NULL),rightChild(NULL){}//dump()
 14 
 15 }BiTreeNode;
 16 
 17 
 18 void Destroy(BiTreeNode **root);
 19 
 20 
 21 //初始化
 22 void Initiate(BiTreeNode **root)
 23 {
 24     *root = (BiTreeNode *)malloc(sizeof(BiTreeNode));
 25     (*root)->leftChild = NULL;
 26     (*root)->rightChild = NULL;
 27 }
28 //左插入节点
 29 BiTreeNode *InsertLeftNode(BiTreeNode *curr, DataType x)
 30 {
 31     BiTreeNode *s, *t;
 32     if(curr == NULL)
 33         return NULL;
 34     t = curr->leftChild;
 35     s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
 36     s->data = x;
 37     s->leftChild = t;
 38     s->rightChild = NULL;
 39     curr->leftChild = s;
 40     return curr->leftChild;
 41 }
 42 //右插入节点
 43 BiTreeNode *InsertRightNode(BiTreeNode *curr, DataType x)
 44 {
 45     BiTreeNode *s, *t;
 46     if(curr == NULL)
 47         return NULL;
 48     t = curr->rightChild;
 49     s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
 50     s->data = x;
 51     s->rightChild = t;
 52     s->leftChild = NULL;
 53     curr->rightChild = s;
 54     return curr->rightChild;
 55 }

57 //左删除子树

 58 BiTreeNode *DeleteLeftTree(BiTreeNode *curr)
 59 {
 60     if(curr == NULL||curr->leftChild == NULL)
 61         return NULL;
 62     Destroy(&curr->leftChild);
 63     curr->leftChild = NULL;
 64     return curr;
 65 }
 66 //右删除子树
 67 BiTreeNode *DeleteRightTree(BiTreeNode *curr)
 68 {
 69     if(curr == NULL || curr->rightChild == NULL)
 70         return NULL;
 71     Destroy(&curr->rightChild);
 72     curr->rightChild = NULL;
 73     return curr;
 74 }
 75 //打印二叉树
 76 void PrintBiTree(BiTreeNode *root, int n)
 77 {
 78     int i;
 79     if(root == NULL)
 80         return ;
 81     PrintBiTree(root->rightChild, n+1);
 82     for(i = 0; i< n-1; i++)
 83         cout<<"   ";
 84     if(n>0)
 85     {
 86         cout<<"--";
 87         cout<<root->data<<endl;
 88     }
 89     PrintBiTree(root->leftChild, n+1);
 90 }
//查找数据元素
 93 BiTreeNode *Search(BiTreeNode *root, DataType x)
 94 {
 95     BiTreeNode *find = NULL;
 96     if(root != NULL)
 97     {
 98         if(root->data == x)
 99             find =root;
100         else
101         {
102             find = Search(root->leftChild, x);
103             if(find == NULL)
104                 find = Search(root->rightChild, x);
105         }
106     }
107     return find;
108 }
 //前序遍历非递归
112 void preorder(BiTreeNode *root, void visit(DataType item))
113 {
114     BiTreeNode *cur = root,*prev = NULL;
115     while(cur != NULL)
116     {
117         if(cur->leftChild == NULL)
118         {
119             cout<<cur->data;
120             cur = cur->rightChild;
121         }
122         else
123         {
124             prev = cur->leftChild;
125             while(prev->rightChild != NULL && prev->rightChild != cur)
126                 prev = prev->rightChild;
127             if(prev->rightChild == NULL)
128             {
129                 cout<<cur->data;
130                 prev->rightChild = cur;
131                 cur = cur->leftChild;
132             }
133             else
134             {
135                 prev->rightChild = NULL;
136                 cur = cur->rightChild;
137             }
138         }
139     }
140 }
142 /*//前序遍历递归
143   void preorder(BiTreeNode *root, void visit(DataType item))
144   {
145   if(root != NULL)
146   {
147   visit(root->data);
148   preorder(root->leftChild, visit);
149   preorder(root->rightChild, visit);
150   }
151   }
152   */

//中序遍历非递归
154 void inorder(BiTreeNode *root,  void visit(DataType item))
155 {
156     BiTreeNode *cur = root,*prev = NULL;
157     while(cur != NULL)
158     {
159         if(cur->leftChild == NULL)
160         {
161             cout<<cur->data;
162             cur = cur->rightChild;
163         }
164         else
165         {
166             prev = cur->leftChild;
167             while(prev->rightChild != NULL && prev->rightChild != cur)
168                 prev = prev->rightChild;
169             if(prev->rightChild == NULL)
170             {
171                 prev->rightChild = cur;
172                 cur = cur->leftChild;
173             }
174             else
175             {
176                 prev->rightChild = NULL;
177                 cout<<cur->data;
178                 cur = cur->rightChild;
179             }
180         }
181     }
182 }


/*
184 //中序遍历递归
185 void inorder(BiTreeNode *root, void visit(DataType item))
186 {
187 if(root !=NULL)
188 {
189 inorder(root->leftChild, viist);
190 visit(root->data);
191 inorder(root->rightChild, visit);
192 }
193 }
194  * */


 // 比前两个稍复杂,需要建立临时节点,令其左孩子是root,并且还需要一个子过程,就是倒序输出某两个节点之间路径上的各节点
196 void reverse(BiTreeNode *from, BiTreeNode *to)
197 {
198     if(from == to)
199         return ;
200     BiTreeNode *x =from, *y = from->rightChild, *z;
201     while(true)
202     {
203         z = y->rightChild;
204         y->rightChild = x;
205         x = y;
206         y = z;
207         if(x == to)
208             break;
209     }
210 }
211 void printreverse(BiTreeNode *from, BiTreeNode *to)
212 {
213     reverse(from, to);
214     BiTreeNode *p = to;
215     while(true)
216     {
217         cout<<p->data;
218         if(p == from)
219             break;
220         p = p->rightChild;
221     }
222     reverse(to, from);
223 }
224 void postorder(BiTreeNode *root,  void visit(DataType item))
225 {
226     BiTreeNode dump(0);
227     dump.leftChild = root;
228     BiTreeNode *cur = &dump,*prev = NULL;
229     while(cur)
230     {
231         if(cur->leftChild == NULL)
232         {
233             cur = cur->rightChild;
234         }
235         else
236         {
237             prev = cur->leftChild;
238             while(prev->rightChild != NULL && prev->rightChild != cur)
239                 prev = prev->rightChild;
240             if(prev->rightChild == NULL)
241             {
242                 prev->rightChild = cur;
243                 cur = cur->leftChild;
244             }
245             else
246             {
247                 printreverse(cur->leftChild, prev);
248                 prev->rightChild = NULL;
249                 cur = cur->rightChild;
250             }
251         }
252     }
253 }
256 /*
257 //后序遍历递归
258 void inorder(BiTreeNode *root, void visit(DataType item))
259 {
260     if(root != NULL)
261     {
262         postorder(root->leftChild, visit);
263         postorder(root->rightChild, visit);
264         visit(root->data);
265     }
266 }
267 */
//撤销二叉树操作设计
268 void Destroy(BiTreeNode **root)
269 {
270     if((*root) != NULL && (*root)->leftChild != NULL)
271         Destroy(&(*root)->leftChild);
272     if((*root) != NULL && (*root)->rightChild != NULL)
273         Destroy(&(*root)->rightChild);
274     free(*root);
275 }

主函数检测void Visit(DataType item)
278 {
279     cout<<item<<"     ";
280 }
281 int main()
282 {
283     BiTreeNode *root, *p, *find;
284     char x = 'E';
285     Initiate(&root);
286     p = InsertLeftNode(root, 'A');
287     p = InsertLeftNode(p, 'B');
288     p = InsertLeftNode(p, 'D');
289     p = InsertRightNode(p, 'G');
290     p = InsertRightNode(root->leftChild, 'C');
291     InsertLeftNode(p, 'E');
292     InsertRightNode(p, 'F');
293     PrintBiTree(root, 0);
294     cout<<"前序遍历:"<<endl;
295     preorder(root->leftChild, Visit);
296     cout<<endl;
297     cout<<"中序遍历:"<<endl;
298     inorder(root->leftChild, Visit);
299     cout<<endl;
300     cout<<"后续遍历:"<<endl;
301     postorder(root->leftChild, Visit);
302     cout<<endl;
303     find = Search(root, x);
304     if(find != NULL)
305         cout<<"数据元素"<<x<<"在二叉树中"<<endl;
306     cout<<" not find x";
307     return 0;
308     Destroy(&root);
309 }

 
 
 

猜你喜欢

转载自blog.csdn.net/csdn_wanziooo/article/details/77102939