码迷,mamicode.com
首页 > 其他好文 > 详细

层序遍历二叉树

时间:2018-08-20 13:17:08      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:遍历二叉树   print   style   front   been   free   bin   roo   sem   

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 //Real capacity is CircularQueueMaxSize -1
  6 #define CircularQueueMaxSize 1000
  7 #define MaxSize 1000
  8 
  9 typedef int TreeEleType;
 10 typedef struct BinarySearchTreeNode* QueueEleType;
 11 
 12 struct BinarySearchTreeNode
 13 {
 14     TreeEleType Element;
 15     struct BinarySearchTreeNode *Left;
 16     struct BinarySearchTreeNode *Right;
 17 };
 18 
 19 struct CircularQueue
 20 {
 21     QueueEleType QueueData[CircularQueueMaxSize];
 22     int Front;
 23     int Rear;
 24 };
 25 
 26 struct BinarySearchTreeNode *BinarySearchTreeInit()
 27 {
 28     struct BinarySearchTreeNode *TreeRoot = NULL;
 29 
 30     return TreeRoot;
 31 }
 32 
 33 int CircularQueueIsEmpty(struct CircularQueue *Queue)
 34 {
 35     return (Queue -> Front == Queue -> Rear);
 36 }
 37 
 38 int CircularQueueIsFull(struct CircularQueue *Queue)
 39 {
 40     return ((Queue -> Rear + 1) % CircularQueueMaxSize == Queue -> Front);
 41 }
 42 
 43 struct CircularQueue *CircularQueueInit()
 44 {
 45     struct CircularQueue *Queue;
 46     Queue = malloc(sizeof(struct CircularQueue));
 47 
 48     Queue -> Front = Queue -> Rear = 0;
 49 
 50     return Queue;
 51 }
 52 
 53 //if Queue is full,return 1
 54 int CircularQueueEnqueue(struct CircularQueue *Queue,QueueEleType ToBeEnqueue)
 55 {
 56     if(CircularQueueIsFull(Queue))
 57     {
 58         return 1;
 59     }
 60     else
 61     {
 62         Queue -> Rear = (Queue -> Rear + 1) % CircularQueueMaxSize;
 63         Queue -> QueueData[Queue -> Rear] = ToBeEnqueue;
 64     }
 65     return 0;
 66 }
 67 
 68 //if Queue is empty,return NULL
 69 QueueEleType CircularQueueTop(struct CircularQueue *Queue)
 70 {
 71     if(CircularQueueIsEmpty(Queue))
 72     {
 73         return NULL;
 74     }
 75     else
 76     {
 77         return Queue -> QueueData[(Queue -> Front + 1) % CircularQueueMaxSize];
 78     }
 79     return 0;
 80 }
 81 
 82 //if Queue is empty,return 1
 83 int CircularQueueDequeue(struct CircularQueue *Queue)
 84 {
 85     if(CircularQueueIsEmpty(Queue))
 86     {
 87         return 1;
 88     }
 89     else
 90     {
 91         Queue -> Front = (Queue -> Front + 1) % CircularQueueMaxSize;
 92         return 0;
 93     }
 94 }
 95 
 96 int MakeCircularQueueEmpty(struct CircularQueue *Queue)
 97 {
 98     Queue -> Front = Queue -> Rear = 0;
 99 
100     return 0;
101 }
102 
103 int CircularQueueDelete(struct CircularQueue *Queue)
104 {
105     free(Queue);
106     Queue = NULL;
107     return 0;
108 }
109 int BinarySearchTreeDestroy(struct BinarySearchTreeNode *TreeRoot)
110 {
111     if(TreeRoot != NULL)
112     {
113         BinarySearchTreeDestroy(TreeRoot -> Left);
114         BinarySearchTreeDestroy(TreeRoot -> Right);
115         free(TreeRoot);
116     }
117     return 0;
118 }
119 
120 struct BinarySearchTreeNode *BinarySearchTreeNodeFind(TreeEleType ToBeFind,struct BinarySearchTreeNode *TreeRoot)
121 {
122     if(TreeRoot==NULL)
123     {
124         return NULL;
125     }
126 
127     if(ToBeFind < TreeRoot -> Element)
128     {
129         return BinarySearchTreeNodeFind(ToBeFind,TreeRoot->Left);
130     }
131     else if(ToBeFind > TreeRoot -> Element)
132     {
133         return BinarySearchTreeNodeFind(ToBeFind,TreeRoot->Right);
134     }
135     else
136     {
137         return TreeRoot;
138     }
139 }
140 
141 struct BinarySearchTreeNode *BinarySearchTreeNodeFindMin(struct BinarySearchTreeNode *TreeRoot)
142 {
143     if(TreeRoot==NULL)
144     {
145         return NULL;
146     }
147     else if(TreeRoot->Left==NULL)
148     {
149         return TreeRoot;
150     }
151     else
152     {
153         return BinarySearchTreeNodeFindMin(TreeRoot->Left);
154     }
155 }
156 
157 struct BinarySearchTreeNode *BinarySearchTreeNodeFindMax(struct BinarySearchTreeNode *TreeRoot)
158 {
159     if(TreeRoot==NULL)
160     {
161         return NULL;
162     }
163     else if(TreeRoot->Right==NULL)
164     {
165         return TreeRoot;
166     }
167     else
168     {
169         return BinarySearchTreeNodeFindMax(TreeRoot->Right);
170     }
171 }
172 
173 //be careful of return NULL
174 struct BinarySearchTreeNode *BinarySearchTreeNodeInsert(TreeEleType ToBeInsert,struct BinarySearchTreeNode *TreeRoot)
175 {
176     if(TreeRoot==NULL)
177     {
178         TreeRoot = malloc(sizeof(struct BinarySearchTreeNode));
179         TreeRoot -> Element = ToBeInsert;
180         TreeRoot -> Left = TreeRoot -> Right = NULL;
181     }
182     else if(ToBeInsert < TreeRoot->Element)
183     {
184         TreeRoot -> Left = BinarySearchTreeNodeInsert(ToBeInsert,TreeRoot->Left);
185     }
186     else if(ToBeInsert > TreeRoot->Element)
187     {
188         TreeRoot -> Right = BinarySearchTreeNodeInsert(ToBeInsert,TreeRoot->Right);
189     }
190     return TreeRoot;
191 }
192 
193 //if doesn‘t find,return NULL
194 struct BinarySearchTreeNode *BinarySearchTreeNodeDelete(TreeEleType ToBeDelete,struct BinarySearchTreeNode *TreeRoot)
195 {
196     if(TreeRoot == NULL)
197         return NULL;
198 
199     struct BinarySearchTreeNode *TmpCell;
200 
201     if(ToBeDelete < TreeRoot -> Element)
202     {
203         TreeRoot -> Left = BinarySearchTreeNodeDelete(ToBeDelete,TreeRoot->Left);
204     }
205     else if(ToBeDelete > TreeRoot -> Element)
206     {
207         TreeRoot -> Right = BinarySearchTreeNodeDelete(ToBeDelete,TreeRoot->Right);
208     }
209     else //if(ToBeDelete == TreeRoot -> Element)
210     {
211         if(TreeRoot->Left && TreeRoot->Right)
212         {
213             TmpCell = BinarySearchTreeNodeFindMin(TreeRoot -> Right);
214             TreeRoot -> Element = TmpCell -> Element;
215             TreeRoot -> Right = BinarySearchTreeNodeDelete(TreeRoot->Element,TreeRoot->Right);
216         }
217         else
218         {
219             TmpCell = TreeRoot;
220             if(TreeRoot->Left==NULL)
221             {
222                 TreeRoot = TreeRoot -> Right;
223             }
224             else if(TreeRoot->Right==NULL)
225             {
226                 TreeRoot = TreeRoot -> Left;
227             }
228             free(TmpCell);
229         }
230     }
231     return TreeRoot;
232 }
233 
234 int BinarySearchTreeLevelOrder(struct BinarySearchTreeNode *TreeRoot)
235 {
236     struct CircularQueue *Queue;
237     Queue = CircularQueueInit();
238     CircularQueueEnqueue(Queue,TreeRoot);
239     while(!CircularQueueIsEmpty(Queue))
240     {
241         struct BinarySearchTreeNode *NodePtr = CircularQueueTop(Queue);
242         printf("%d ",NodePtr->Element);
243         CircularQueueDequeue(Queue);
244         if(NodePtr->Left != NULL)
245         {
246             CircularQueueEnqueue(Queue,NodePtr->Left);
247         }
248         if(NodePtr->Right != NULL)
249         {
250             CircularQueueEnqueue(Queue,NodePtr->Right);
251         }
252     }
253     return 0;
254 }
255 
256 int main()
257 {
258     struct BinarySearchTreeNode *TreeRoot = BinarySearchTreeInit();
259     TreeRoot = BinarySearchTreeNodeInsert(5,TreeRoot);
260     TreeRoot = BinarySearchTreeNodeInsert(6,TreeRoot);
261     TreeRoot = BinarySearchTreeNodeInsert(3,TreeRoot);
262     TreeRoot = BinarySearchTreeNodeInsert(7,TreeRoot);
263     TreeRoot = BinarySearchTreeNodeInsert(2,TreeRoot);
264     TreeRoot = BinarySearchTreeNodeInsert(4,TreeRoot);
265     TreeRoot = BinarySearchTreeNodeInsert(1,TreeRoot);
266     BinarySearchTreeLevelOrder(TreeRoot);
267     return 0;
268 }

 

层序遍历二叉树

标签:遍历二叉树   print   style   front   been   free   bin   roo   sem   

原文地址:https://www.cnblogs.com/Asurudo/p/9504844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!