-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataStructures.js
More file actions
373 lines (359 loc) · 9.83 KB
/
dataStructures.js
File metadata and controls
373 lines (359 loc) · 9.83 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Stack data structure
// in stack there are 4 funcs
// 1. push - it puts a item in the stack on the top
// 2. pop - it deletes a item on the top of the stack
// 3. peek - it peeks (sees) the top item
// 4. length - it tells the length of the stack
class Stack
{
constructor()
{
let arr = [];
this.push = (item) => {
arr.push(item);
};
this.pop = () => {
if(arr.length === 0) return;
arr.splice(arr.length - 1, 1);
};
this.peek = () => {
if(arr.length === 0) return;
return arr[arr.length - 1];
};
this.length = () => {
return arr.length;
};
}
}
// sets data structure
// in sets there are 9 funcs
// has - checks wether there is a value in the collection or not
// values - it returns all the values present in the collection
// add - it adds a value to the collection
// remove - it removes a item from the collection
// size - it returns the size of the collection
// union - it returns the union array of two sets
// intersection - it returns the intersection array of two sets
// diferrence - it returns the difference array of two sets
// subset - it checks wether the other set is the subset of the first set
class Sets
{
constructor()
{
let collection = [];
this.has = (item) => {
if(collection.indexOf(item) !== -1) return true;
return false;
}
this.add = (item) => {
collection.push(item);
return true;
}
this.remove = (item) => {
if(collection.indexOf(item) !== -1)
{
collection.splice(collection.indexOf(item), 1);
return true;
}
return false;
}
this.values = () => {
return collection;
}
this.size = () => {
return collection.length;
}
this.union = (otherSet) => {
let arr = []
for(let i = 0; i < this.values().length; i++)
{
arr.push(this.values()[i]);
}
for(let i = 0; i < otherSet.values().length; i++)
{
if(arr.indexOf(otherSet.values()[i]) === -1) arr.push(otherSet.values()[i]);
else continue;
}
return arr;
}
this.intersection = (otherSet) => {
let arr = [];
for(let i = 0; i < otherSet.values().length; i++)
{
if(this.values().indexOf(otherSet.values()[i]) !== -1) arr.push(otherSet.values()[i]);
else continue;
}
return arr;
}
this.difference = (otherSet) => {
let arr = [];
for(let i = 0; i < this.values().length; i++)
{
arr.push(this.values()[i]);
}
for(let i = 0; i < otherSet.values().length; i++)
{
if(arr.indexOf(otherSet.values()[i]) !== -1) arr.splice(arr.indexOf(otherSet.values()[i]), 1);
else continue;
}
return arr;
}
this.subset = (otherSet) => {
for(let i = 0; i < otherSet.values().length; i++)
{
if(this.values().indexOf(otherSet.values()[i]) !== -1) continue;
return false;
}
return true;
}
}
}
// Queue data structure
// in Queue data structure there are 5 funcs
// 1. enqueue - it enters a value in the back of the queue
// 2. dequeue - it removes a value in front of the queue
// 3. print - it prints the whole queue
// 4. front - it shows the front value of the queue
// 5. size - it tells the size of the queue
// 6. isEmpty - it checks wether the queue is empty or not
class Queue
{
constructor()
{
let collection = [];
this.enQueue = (value) => {
collection.push(value);
}
this.deQueue = () => {
collection.shift();
}
this.print = () => {
return collection;
}
this.front = () => {
if(collection.length > 0) return collection[0];
return null;
}
this.size = () => {
return collection.length;
}
this.isEmpty = () => {
if(collection.length === 0) return true;
return false;
}
}
}
// priority Queues data structure
// same as normal queue just the element with the higher priority will go ahead of the lower priority elements
class priorityQueue
{
constructor()
{
let collection = [];
this.enQueue = (value) => {
if(collection.length === 0) collection.push(value);
else
{
for(let i = 0; i < collection.length; i++)
{
if(collection[i][1] < value[1])
{
collection.splice(i, 0, value);
return;
}
}
collection.push(value);
}
}
this.deQueue = () => {
collection.shift();
}
this.print = () => {
return collection;
}
this.front = () => {
if(collection.length > 0) return collection[0];
return null;
}
this.size = () => {
return collection.length;
}
this.isEmpty = () => {
if(collection.length === 0) return true;
return false;
}
}
}
// Binary Search Tree
class Node
{
constructor(data, left = null, right = null)
{
this.data = data;
this.left = left;
this.right = right;
}
}
class BST
{
constructor()
{
this.root = null;
}
add(data)
{
const node = this.root;
if(node === null)
{
this.root = new Node(data);
return;
}
else
{
const searchTree = (node) => {
if(data < node.data)
{
if(node.left === null)
{
node.left = new Node(data);
return;
}
else if(node.left !== null)
{
return searchTree(node.left);
}
}
else if(data > node.data)
{
if(node.right === null)
{
node.right = new Node(data);
return;
}
else if(node.right !== null)
{
return searchTree(node.right);
}
}
else
{
return null;
}
}
return searchTree(node);
}
}
findMin = () => {
let current = this.root;
while(current.left !== null)
{
current = current.left;
}
return current.data;
}
findMax = () => {
let current = this.root;
while(current.right !== null)
{
current = current.right;
}
return current.data;
}
find = (data) => {
if(data > this.root.data)
{
let current = this.root;
while(current)
{
if(current.data === data) return current;
else if(current.data > data) current = current.left
else current = current.right;
}
return false;
}
else if(data < this.root.data)
{
let current = this.root;
while(current)
{
if(current.data === data) return current;
else if(current.data > data) current = current.left
else current = current.right;
}
return false;
}
else return this.root;
}
isPresent = (data) => {
if(this.find(data)) return true;
else return false;
}
remove = (data) => {
const removeNode = (node, data) => {
if(node === null)
{
return null;
}
if(data === node.data)
{
// node has no children
if(node.left === null && node.right === null)
{
return null;
}
// node has no left child
if(node.left === null)
{
return node.right;
}
// node has no right child
if(node.right === null)
{
return node.left;
}
// node has two children
var tempNode = node.right;
while(tempNode.left !== null)
{
tempNode = tempNode.left;
}
node.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
}
}
this.root = removeNode(this.root, data);
}
}
// linked lists data structure
class LNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class LinkedList
{
constructor()
{
this.header = null;
this.add = (data) =>
{
if(this.header === null)
{
this.header = new LNode(data);
}
else
{
let currentNode = this.header;
while(currentNode.next !== null)
{
currentNode = currentNode.next;
}
currentNode.next = new LNode(data);
}
}
}
}