-
Notifications
You must be signed in to change notification settings - Fork 0
/
btreebltraverse.c
211 lines (167 loc) · 5.45 KB
/
btreebltraverse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* Breadth first search, both recursive and iterative way.
*/
#include "btree.h"
#include <stdio.h>
#include <stdlib.h>
/* This is recursive method to do breadth level traversal, the logic is simple:
* we maintain two lists:
* - first level is the Nth level nodes of the tree that we are visiting now.
* - the second level is the N+1th level nodes of the tree.
*
* we will print first level node values.
* we will then copy nodes of 2nd level into 1st level.
* we will then gather child nodes of refreshed 1st level nodes into 2nd level.
* then we call the same method recursively. :)
*/
void breadthLevelTraversal_Internal(struct TreeNode **currentLevelList,
int currentLevelCnt,
struct TreeNode **nextLevelList,
int nextLevelCnt) {
int i;
if (currentLevelCnt <= 0)
return;
for (i = 0; i < currentLevelCnt; i++) {
printf("%d", currentLevelList[i]->val);
if (i + 1 < currentLevelCnt)
printf(", ");
}
for (i = 0; i < nextLevelCnt; i++)
currentLevelList[i] = nextLevelList[i];
currentLevelCnt = i;
nextLevelCnt = 0;
for (i = 0; i < currentLevelCnt; i++) {
struct TreeNode *node = currentLevelList[i];
if (NULL != node->left)
nextLevelList[nextLevelCnt++] = node->left;
if (NULL != node->right)
nextLevelList[nextLevelCnt++] = node->right;
}
if (currentLevelCnt > 0)
printf(", ");
breadthLevelTraversal_Internal(currentLevelList, currentLevelCnt,
nextLevelList, nextLevelCnt);
}
/* The breadth level traversal, the iterative version, the logic is simple as
* none-iterative version:
* - we maintain two list levels, current and next level.
* - the loop is going on if current level is NOT exhausted.
* - the first iterative of the current level, we clear the next level list
* (empty it).
* - as we process each node of current level, we gather the next level node
* into the next level list (appending to what is built up).
* - if we exhaust the current level list, and there is next level list, we
* refresh the current level list with next level nodes.
*/
void breadthLevelTraversalIter(struct TreeNode *root) {
struct TreeNode *currentLevelList[100];
struct TreeNode *nextLevelList[100];
struct TreeNode *node;
int currentLevelCnt = 0;
int nextLevelCnt = 0;
int index = 0;
if (NULL == root)
return;
currentLevelList[currentLevelCnt++] = root;
while (index < currentLevelCnt) {
if (0 == index)
nextLevelCnt = 0;
node = currentLevelList[index];
if (NULL != node->left)
nextLevelList[nextLevelCnt++] = node->left;
if (NULL != node->right)
nextLevelList[nextLevelCnt++] = node->right;
printf("%d", node->val);
index++;
if (index >= currentLevelCnt) {
if (nextLevelCnt > 0) {
for (currentLevelCnt = 0; currentLevelCnt < nextLevelCnt;
currentLevelCnt++) {
currentLevelList[currentLevelCnt] = nextLevelList[currentLevelCnt];
}
index = 0;
printf(", ");
}
} else {
printf(", ");
}
}
printf("\n");
}
/* This is API entry to do breadth level traversal, kicker of
* recursive method.
*/
void breadthLevelTraversal(struct TreeNode *root) {
struct TreeNode *nextLevelList[100];
struct TreeNode *currentLevelList[100];
int nextLevelCnt = 0;
int currentLevelCnt = 0;
if (NULL == root)
return;
currentLevelList[currentLevelCnt++] = root;
if (NULL != root->left)
nextLevelList[nextLevelCnt++] = root->left;
if (NULL != root->right)
nextLevelList[nextLevelCnt++] = root->right;
breadthLevelTraversal_Internal(currentLevelList, currentLevelCnt,
nextLevelList, nextLevelCnt);
printf("\n");
}
int main(int argc, char *argv[]) {
struct TreeNode *root = NULL;
struct TreeNode *other = NULL;
root = malloc(sizeof(struct TreeNode));
root->val = 0;
root->left = NULL;
root->right = NULL;
other = malloc(sizeof(struct TreeNode));
other->val = 1;
other->left = NULL;
other->right = NULL;
root->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 3;
other->left = NULL;
other->right = NULL;
root->left->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 4;
other->left = NULL;
other->right = NULL;
root->left->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 2;
other->left = NULL;
other->right = NULL;
root->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 5;
other->left = NULL;
other->right = NULL;
root->right->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 6;
other->left = NULL;
other->right = NULL;
root->right->right->left = other;
other = malloc(sizeof(struct TreeNode));
other->val = 7;
other->left = NULL;
other->right = NULL;
root->right->right->left->right = other;
other = malloc(sizeof(struct TreeNode));
other->val = 8;
other->left = NULL;
other->right = NULL;
root->right->right->right = other;
printf("breadth level traverse (recursive) = ");
breadthLevelTraversal(root);
printf("breadth level traverse (iterative) = ");
breadthLevelTraversalIter(root);
printf("\n");
printf("\n");
// I do not care about freeing malloced memory, OS will take care of freeing
// heap that is part of process for this one off program.
return 0;
}