This repository was archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
230 lines (180 loc) · 4.47 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *insert(struct node *root, int data) {
if (root == NULL) {
struct node *node = (struct node *) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
} else {
struct node *cur;
if (data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
/**
* A 'linked list' style QueueNode, which points to the next QueueNode
*/
struct QueueNode {
struct node *node;
struct QueueNode *next;
};
/**
* A classic Queue, that keeps track of the front and the rear nodes
*/
struct Queue {
struct QueueNode *front, *rear;
};
/**
* Creates a new QueueNode
*
* @param key
* @return
*/
struct QueueNode *newNode(struct node *node) {
struct QueueNode *tmpNode = (struct QueueNode *) malloc(sizeof(struct QueueNode));
tmpNode->node = node;
tmpNode->next = NULL;
return tmpNode;
}
/**
* Creates a new Queue
*
* @return
*/
struct Queue *newQueue() {
struct Queue *q = (struct Queue *) malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
/**
* Adds a new QueueNode to the end of the Queue
*
* @param q
* @param key
*/
void enQueue(struct Queue *q, struct node *node) {
struct QueueNode *tmpNode = newNode(node);
// if queue is empty, then the new node is both front and rear
if (q->rear == NULL) {
q->front = q->rear = tmpNode;
return;
}
// add the new node at the end of queue and change rear
q->rear->next = tmpNode;
q->rear = tmpNode;
}
/**
* Removes a QueueNode of the front of the Queue, moves front->next to actual "front"
*
* @param q
* @return
*/
struct QueueNode *deQueue(struct Queue *q) {
// if queue is empty, return NULL.
if (q->front == NULL) {
return NULL;
}
// store previous front and move front one node ahead
struct QueueNode *tmpNode = q->front;
q->front = q->front->next;
// if front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
return tmpNode;
}
/**
* Checks whether the Queue is empty
*
* @param q
* @return
*/
int isEmpty(struct Queue *q) {
// queue is empty if there's no front or rear nodes
if (q->front == NULL && q->rear == NULL) {
return 1;
}
return 0;
}
void levelOrder(struct node *root) {
if (root == NULL) {
return;
}
/*
* Solution:
*
* First node is visited, than removed, child nodes are put in a FIFO queue,
* The loop continues till the queue is empty
*
*
* Using Queue Linked List algorithm from
* https://github.com/gritt/queue-linked-list
*/
struct Queue *treeQueue = newQueue();
/*
* root node is added to the queue (1)
*
* while the queue is not consumed {
*
* we visit the first node of the queue (1)
*
* we then remove the first node of the queue (empty)
*
* and we add it's child nodes to the queue, left(2) -> right(3), as is expected to show in level order traversal)
*
* following the loop..
*
* we visit the first node of the queue - left(2)
*
* we then remove the first node of the queue (left the right node at front (3))
*
* add left and right child..
*
* visit the first node - right(3)
*
* remove it..
*
* add left and right
* }
*/
enQueue(treeQueue, root);
while (isEmpty(treeQueue) == 0) {
struct QueueNode *queueNode = treeQueue->front;
printf("%d ", queueNode->node->data);
deQueue(treeQueue);
// enqueue left child
if (queueNode->node->left != NULL) {
enQueue(treeQueue, queueNode->node->left);
}
// enqueue right child
if (queueNode->node->right != NULL) {
enQueue(treeQueue, queueNode->node->right);
}
}
}
int main() {
struct node *root = NULL;
int treeSize;
int data;
scanf("%d", &treeSize);
while (treeSize-- > 0) {
scanf("%d", &data);
root = insert(root, data);
}
levelOrder(root);
return 0;
}