-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer04.c
266 lines (223 loc) · 6.51 KB
/
answer04.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "pa04.h"
#include <stdio.h>
#include <stdlib.h>
/* Create a single instance of a sparse array node with a specific
* index and value. This is a constructor function that allocates
* memory, copies the integer values, and sets the subtree pointers to
* NULL.
*/
SparseNode *SparseNode_create(int index, int value)
{
SparseNode * one_node = NULL;
one_node = malloc(sizeof(SparseNode));
one_node->index = index;
one_node->value = value;
one_node->left = NULL;
one_node->right = NULL;
return one_node;
}
/* Add a particular value into a sparse array on a particular index.
* The sparse array uses the index as a key in a binary search tree.
* It returns the new sparse array root
* as its return value. If the index does not exist, create it.
* If the index exists, REPLACE the value to the new one. Use the index to
* determine whether to go left or right in the tree (smaller index
* values than the current one go left, larger ones go right).
*/
SparseNode * SparseArray_add ( SparseNode * array, int index, int value )
{
if (array == NULL)
{
array = SparseNode_create(index, value);
return array;
}
if (array->index > index)
{
SparseArray_add (array->left, index, value);
}
else if (array->index < index)
{
SparseArray_add (array->right, index, value);
}
else if (array->index == index)
{
array->value = value;
}
return array;
}
/* Build a sparse array from given indices and values with specific length.
* This function takes an array of indices, an array of values, and
* the length as inputs.
* It returns a sparse array.
* You need to insert tree nodes in order:
* the first sparse array node contains indices[0] and values[0])
*/
SparseNode *SparseArray_build(int * indicies, int * values, int length)
{
SparseNode * array = NULL;
int i = 0;
for (i = 0; i < length; i++)
{
array = SparseArray_add(array, indicies[i], values[i]);
}
return array;
}
/* Destroy an entire sparse array.
* traversing the binary tree in postorder. Use the
* SparseNode_destroy () function to destroy each node by itself.
*/
void SparseArray_destroy ( SparseNode * array )
{
if (array == NULL)
{
return;
}
SparseArray_destroy(array->left);
SparseArray_destroy(array->right);
free (array);
return;
}
/* Retrieve the smallest index in the sparse array.
*/
int SparseArray_getMin ( SparseNode * array )
{
if (array->left == NULL)
{
return array->index;
}
return SparseArray_getMin(array->left);
}
/* Retrieve the largest index in the sparse array.
*/
int SparseArray_getMax ( SparseNode * array )
{
if (array->right == NULL)
{
return array->index;
}
return SparseArray_getMax(array->right);
}
/* Retrieve the node associated with a specific index in a sparse
* array. It returns the value
* associated with the index. If the index does not exist in the
* array, it returns NULL. If the given index is smaller than the current
* node, search left ; if it is larger, search right.
*/
SparseNode * SparseArray_getNode(SparseNode * array, int index )
{
if (array->index == index)
{
return array;
}
if (array->index > index && array->left != NULL)
{
return SparseArray_getNode(array->left, index);
}
if (array->index > index && array->right != NULL)
{
return SparseArray_getNode(array->right, index);
}
return NULL;
}
/* Remove a value associated with a particular index from the sparse
* array. It returns the new
* sparse array ( binary tree root ). HINT : You will need to isolate
* several different cases here :
* - If the array is empty ( NULL ), return NULL
* - Go left or right if the current node index is different.
* - If both subtrees are empty, you can just remove the node.
* - If one subtree is empty, you can just remove the current and
* replace it with the non - empty child.
* - If both children exist, you must find the immediate successor of
* the current node ( leftmost of right branch ), swap its values with
* the current node ( BOTH index and value ), and then delete the
* index in the right subtree.
*/
SparseNode * SparseArray_remove ( SparseNode * array, int index )
{
SparseNode * temp = NULL;
int temp2 = 0;
if (array == NULL)
{
return NULL;
}
if (array->index > index)
{
array->left = SparseArray_remove(array->left, index);
}
if (array->index < index)
{
array->right = SparseArray_remove(array->right, index);
}
if (array->left == NULL && array->right == NULL)
{
free(array);
return NULL;
}
if (array->left != NULL && array->right == NULL)
{
temp = array->left;
free(array);
return temp;
}
if (array->left == NULL && array->right != NULL)
{
temp = array->right;
free(array);
return temp;
}
//this part may be a problem
if (array->left != NULL && array->right != NULL)
{
temp2 = SparseArray_getMin(array->right);
temp = SparseArray_getNode(array->right, temp2);
array->index = temp->index;
array->value = temp->value;
free(temp);
return array;
}
return array;
}
/* The function makes a copy of the input sparse array
* and it returns a new copy.
*/
void copy_helper (SparseNode * array, int * indicies, int * values, int * sum)
{
if (array == NULL)
{
return;
}
indicies[*sum] = array->index;
values[*sum] = array->value;
(*sum)++;
copy_helper(array->left, indicies, values, sum);
copy_helper(array->right, indicies, values, sum);
}
SparseNode * SparseArray_copy(SparseNode * array)
{
SparseNode * copy = NULL;
int * indicies = NULL;
int * values = NULL;
int sum = 0;
copy_helper(array, indicies, values, &sum);
copy = SparseArray_build(indicies, values, sum);
return copy;
}
/* Merge array_1 and array_2, and return the result array.
* This function WILL NOT CHANGE the contents in array_1 and array_2.
* When merging two sparse array:
* 1. The contents in array_1 and array_2 should not be changed. You should make
* a copy of array_1, and do merging in this copy.
* 2. array_2 will be merged to array_1. This means you need to read nodes in
* array_2 and insert them into array_1.
* 3. You need to use POST-ORDER to traverse the array_2 tree.
* 4. Values of two nodes need to be added only when the indices are the same.
* 5. A node with value of 0 should be removed.
* 6. if array_2 has nodes with index different than any nodes in array_1, you
* should insert those nodes into array_1.
*
*/
SparseNode * SparseArray_merge(SparseNode * array_1, SparseNode * array_2)
{
return NULL;
}