-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseTable.cpp
More file actions
265 lines (257 loc) · 9.31 KB
/
Copy pathSparseTable.cpp
File metadata and controls
265 lines (257 loc) · 9.31 KB
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 <iostream>
using namespace std;
struct node { //node of a trie
int key;
node** child = nullptr; //child pointer is allocated to nullptr by default to save more memory
};
void childAlloc(node& nod, int k) //allocates child pointers
{
if (nod.child == nullptr) //but only if the child pointer is not allocated yet
{
nod.child = new node * [k];
for (int i = 0; i < k; i++)
nod.child[i] = nullptr;
}
}
void addnode(node& nod, int x) //adds a value to the node
{
nod.key = x;
nod.child = nullptr;
}
void insert2(node& nod, int x, int xdv, int k) // inserts a value into the trie, it is another function to avoid sending so many parameters by recursion
{
int pos = xdv % k; //position to allocate
if (x == nod.key)
{
printf("%d exist\n", x);
return; //if the value already exists
}
childAlloc(nod, k); //checks if child pointer allocation is needed
if (nod.child[pos] != nullptr)
insert2(*nod.child[pos], x, xdv / k, k); //going deeper if needed
else
{
nod.child[pos] = new node; //here we are in a good position to allocate a new node
addnode(*nod.child[pos], x);
}
}
void insert(node& nod, int x, int n, int k) // inserts a value into the trie
{
int pos = x % n;
if (x == nod.key)
{
printf("%d exist\n", x);
return;
}
childAlloc(nod, k);
if (nod.child[pos] != nullptr)
insert2(*nod.child[pos], x, x/n, k);
else
{
nod.child[pos] = new node;
addnode(*nod.child[pos], x);
}
}
void show2(node& nod, int k)
{
printf("%d ", nod.key); //prints the key of the node
if (nod.child == nullptr) //if there are no children, we stop
return;
for (int i = 0; i < k; i++)
{
if (nod.child[i] != nullptr) //if it has children in that position, we go deeper
{
show2(*nod.child[i], k);
}
}
}
void show(node& nod, int n, int k)
{
printf("%d ", nod.key);
if (nod.child == nullptr) //if there are no children, we stop
return;
for (int i = 0; i < n; i++)
{
if (nod.child[i] != nullptr)
{
show2(*nod.child[i],k);
}
}
}
bool look2(node& nod, int x, int xdv, int k)
{
if (nod.key == x) //this is the key we are looking for
{
return true;
}
else
{
int pos = xdv % k;
if (nod.child != nullptr && nod.child[pos] != nullptr) //checks whether it has children and if there is a child in the right place
{
return look2(*nod.child[pos], x, xdv / k, k); //and goes deeper if it is the case
}
else
return false; //and stops when it is not the case
}
}
bool look(node& nod, int x, int n, int k)
{
if (nod.key == x)
{
return true;
}
else
{
int pos = x % n;
if (nod.child != nullptr && nod.child[pos] != nullptr)
{
return look2(*nod.child[pos], x, x / n, k);
}
else
return false;
}
}
int leftLeaf(node*& nod, int k) // finds the leftmost leaf in the trie and deletes it
{
for (int i = 0; i < k; i++)
{
if (nod->child != nullptr && nod->child[i] != nullptr)
{
return leftLeaf(nod->child[i], k); //goes deeper if there is the first (leftmost) child
}
}
int rep = nod->key; //deletes the node with the leftmost leaf and returns its key
delete nod;
nod = nullptr;
return rep;
}
void dell2(node*& nod, int x, int xdv, int k)
{
if (nod->key == x) //here we found the node we are looking for
{
bool b = true; //it is a flag to check if the node is a leaf or not
if (nod->child != nullptr)
{
for (int i = 0; i < k; i++)
{
if (nod->child[i] != nullptr) //here we found that the node has children, we are going to set the current's nodde value for leftmost child
{
int ll = leftLeaf(nod->child[i], k);
nod->key = ll;
b = false;
break;
}
}
}
if(b)
{
delete nod; // here it indicates that deleted value is a leaf, we just delete the node
nod = nullptr;
}
}
else
{
int pos = xdv % k;
if (nod->child != nullptr && nod->child[pos] != nullptr)//checks if it there is any child and than if there is a child in the right place
{
dell2(nod->child[pos], x, xdv / k, k); //here it goes deeper because the node has a child in the right place
}
else
{
printf("%d not exist\n", x); // here we know the node we want to delete does not exist
}
}
}
void dell(node& nod, int x, int n, int k)
{
if (nod.key == x)
{
if (nod.child != nullptr)
{
for (int i = 0; i < n; i++)
{
if (nod.child[i] != nullptr)
{
int ll = leftLeaf(nod.child[i], k);
nod.key = ll;
break;
}
}
}
}
else
{
int pos = x % n;
if (nod.child != nullptr && nod.child[pos] != nullptr)
{
dell2(nod.child[pos], x, x / n, k);
}
else
{
printf("%d not exist\n", x);
}
}
}
int main()
{
int N; //number of inputs
int mx, mi; //maximum and minimum values (not used for the code)
int n, k; //n - size of the child array in root, k - size of the child array in other nodes
scanf("%d %d %d %d %d", &N, &mx, &mi, &n, &k);
char q;
int x;
node root; //data structure variable for this task, it is not a reference so I cannot delete the root, I can only delete its children
int siz = 0; //so I implemented a variable for counting the number of elements
for(int i=0;i<N;i++)
{
scanf(" %c", &q);
if (q == 'I')
{
scanf("%d", &x);
if (siz == 0) //size is 0, we add the first element and allocate child nodes pointers, siz will be equal to 0 only once
{
addnode(root, x);
childAlloc(root, n);
}
else if (siz == -1) // siz is -1, when all elements were previously deleted
{
root.key = x; //we set the root key fot element from input, but now we can print it, siz will be equal to 1 after that
siz++;
}
else
{
insert(root, x, n, k); //inserting the element
}
siz++;
}
else if (q == 'P')//showing all elements in inorder
{
if(siz > 0)
show(root,n,k);
}
else if (q == 'L')//looks if there is an inputed number
{
scanf("%d", &x);
if (look(root, x, n, k) && siz > 0) //and checks additionally if size of the trie is greater than 0
printf("%d exist\n", x);
else
printf("%d not exist\n", x);
}
else if (q == 'D')
{
scanf("%d", &x);
if (siz == 1)
{//it still stores the int value of the the deleted root element, but it can never be shown but can be changed more quickly in the next step
siz = -1;
if (root.key != x)
printf("%d not exist\n", x);
}
else
{
dell(root, x, n, k);
siz--;
}
}
}
}