@gabrielrufino/cube 1.0.27 → 1.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,1672 +24,18 @@ const cube = require('@gabrielrufino/cube')
24
24
 
25
25
  ## Data Structures
26
26
 
27
- #### Array/List
28
-
29
- The **List** data structure is just an alias to the **Array** data structure.
30
-
31
- ```js
32
- import cube from '@gabrielrufino/cube'
33
-
34
- console.log(cube.Array === cube.List) // true
35
- ```
36
-
37
- You can initialize an empty array or an array with some elements:
38
-
39
- ```js
40
- import cube from '@gabrielrufino/cube'
41
-
42
- const array1 = new cube.Array()
43
- const array2 = new cube.Array(1, 2, 3, 4)
44
- ```
45
-
46
- Use the property `.data` to access the elements in the array:
47
-
48
- ```js
49
- import cube from '@gabrielrufino/cube'
50
-
51
- const array = new cube.Array(1, 2, 3, 4)
52
- console.log(array.data) // [ 1, 2, 3, 4 ]
53
- ```
54
-
55
- Use the property `.size` to get how many elements the array has:
56
-
57
- ```js
58
- import cube from '@gabrielrufino/cube'
59
-
60
- const array = new cube.Array(1, 2, 3, 4)
61
- console.log(array.size) // 4
62
- ```
63
-
64
- Use the method `.insertInLastPosition()` to put a new element in the last position of the array:
65
-
66
- ```js
67
- import cube from '@gabrielrufino/cube'
68
-
69
- const array = new cube.Array(1, 2, 3)
70
- array.insertInLastPosition(4)
71
- console.log(array.data) // [ 1, 2, 3, 4 ]
72
- ```
73
-
74
- Use the method `.insertInFirstPosition()` to put a new element ein the first position of the array:
75
-
76
- ```js
77
- import cube from '@gabrielrufino/cube'
78
-
79
- const array = new cube.Array(2, 3, 4)
80
- array.insertInFirstPosition(1)
81
- console.log(array.data) // [ 1, 2, 3, 4 ]
82
- ```
83
-
84
- Use the method `.removeFromLastPosition()` to remove the last element from the array:
85
-
86
- ```js
87
- import cube from '@gabrielrufino/cube'
88
-
89
- const array = new cube.Array(1, 2, 3, 4)
90
- array.removeFromLastPosition()
91
- console.log(array.data) // [ 1, 2, 3 ]
92
- ```
93
-
94
- Use the method `.removeFromFirstPosition()` to remove the first element from the array:
95
-
96
- ```js
97
- import cube from '@gabrielrufino/cube'
98
-
99
- const array = new cube.Array(1, 2, 3, 4)
100
- array.removeFromFirstPosition()
101
- console.log(array.data) // [ 2, 3, 4 ]
102
- ```
103
-
104
- Use the method `.removeFromPosition()` to remove an element from some arbitrary position:
105
-
106
- ```js
107
- import cube from '@gabrielrufino/cube'
108
-
109
- const array = new cube.Array(1, 2, 3, 4)
110
- array.removeFromPosition(2)
111
- console.log(array.data) // [ 1, 2, 4 ]
112
- ```
113
-
114
- #### Stack
115
-
116
- You can initialize an empty stack or a stack with some elements:
117
-
118
- ```js
119
- import cube from '@gabrielrufino/cube'
120
-
121
- const stack1 = new cube.Stack()
122
- const stack2 = new cube.Stack(1, 2, 3, 4)
123
- ```
124
-
125
- Use the property `.data` to access the elements in the stack:
126
-
127
- ```js
128
- import cube from '@gabrielrufino/cube'
129
-
130
- const stack = new cube.Stack(1, 2, 3, 4)
131
- console.log(stack.data) // [ 1, 2, 3, 4 ]
132
- ```
133
-
134
- Use the property `.size` to get how many elements the stack has:
135
-
136
- ```js
137
- import cube from '@gabrielrufino/cube'
138
-
139
- const stack = new cube.Stack(1, 2, 3, 4)
140
- console.log(stack.size) // 4
141
- ```
142
-
143
- Use the method `.push()` to insert a new element in the top of the stack:
144
-
145
- ```js
146
- import cube from '@gabrielrufino/cube'
147
-
148
- const stack = new cube.Stack(1, 2, 3)
149
- stack.push(4)
150
- console.log(stack.data) // [ 1, 2, 3, 4 ]
151
- ```
152
-
153
- Use the method `.pop()` to get and remove the element in the top of the stack:
154
-
155
- ```js
156
- import cube from '@gabrielrufino/cube'
157
-
158
- const stack = new cube.Stack(1, 2, 3, 4)
159
- const element = stack.pop()
160
- console.log(stack.data) // [ 1, 2, 3 ]
161
- console.log(element) // 4
162
- ```
163
-
164
- Use the method `.peek()` to get the element in the top of the stack without remove:
165
-
166
- ```js
167
- import cube from '@gabrielrufino/cube'
168
-
169
- const stack = new cube.Stack(1, 2, 3, 4)
170
- const element = stack.peek()
171
- console.log(stack.data) // [ 1, 2, 3, 4 ]
172
- console.log(element) // 4
173
- ```
174
-
175
- Use the method `.clear()` to remove all elements from stack:
176
-
177
- ```js
178
- import cube from '@gabrielrufino/cube'
179
-
180
- const stack = new cube.Stack(1, 2, 3, 4)
181
- stack.clear()
182
- console.log(stack.data) // []
183
- ```
184
-
185
- Use the property `.isEmpty` to find out if the stack is empty:
186
-
187
- ```js
188
- import cube from '@gabrielrufino/cube'
189
-
190
- const stack1 = new cube.Stack()
191
- const stack2 = new cube.Stack(1, 2, 3, 4)
192
- console.log(stack1.isEmpty) // true
193
- console.log(stack2.isEmpty) // false
194
- ```
195
-
196
- #### Queue
197
-
198
- You can initialize an empty queue or a queue with some elements:
199
-
200
- ```js
201
- import cube from '@gabrielrufino/cube'
202
-
203
- const queue1 = new cube.Queue()
204
- const queue2 = new cube.Queue(1, 2, 3, 4)
205
- ```
206
-
207
- Use the property `.data` to access the elements in the queue:
208
-
209
- ```js
210
- import cube from '@gabrielrufino/cube'
211
-
212
- const queue = new cube.Queue(1, 2, 3, 4)
213
- console.log(queue.data) // [ 1, 2, 3, 4 ]
214
- ```
215
-
216
- Use the property `.size` to get how many elements the queue has:
217
-
218
- ```js
219
- import cube from '@gabrielrufino/cube'
220
-
221
- const queue = new cube.Queue(1, 2, 3, 4)
222
- console.log(queue.size) // 4
223
- ```
224
-
225
- Use the method `.enqueue()` to put a new element at the end of the queue:
226
-
227
- ```js
228
- import cube from '@gabrielrufino/cube'
229
-
230
- const queue = new cube.Queue(1, 2, 3)
231
- queue.enqueue(4);
232
- console.log(queue.data) // [ 1, 2, 3, 4 ]
233
- ```
234
-
235
- Use the method `.dequeue()` to get and remove the first element of the queue:
236
-
237
- ```js
238
- import cube from '@gabrielrufino/cube'
239
-
240
- const queue = new cube.Queue(1, 2, 3, 4)
241
- const element = queue.dequeue();
242
- console.log(queue.data) // [ 2, 3, 4 ]
243
- console.log(element) // 1
244
- ```
245
-
246
- Use the method `.peek()` to get the first element of the queue without removing:
247
-
248
- ```js
249
- import cube from '@gabrielrufino/cube'
250
-
251
- const queue = new cube.Queue(1, 2, 3, 4)
252
- const element = queue.peek()
253
- console.log(queue.data) // [ 1, 2, 3, 4 ]
254
- console.log(element) // 1
255
- ```
256
-
257
- Use the method `.clear()` to remove all the elements in the queue:
258
-
259
- ```js
260
- import cube from '@gabrielrufino/cube'
261
-
262
- const queue = new cube.Queue(1, 2, 3, 4)
263
- queue.clear()
264
- console.log(queue.data) // []
265
- ```
266
-
267
- #### Deck
268
-
269
- You can initialize an empty deck or a deck with some elements:
270
-
271
- ```js
272
- import cube from '@gabrielrufino/cube'
273
-
274
- const deck1 = new cube.Deck()
275
- const deck2 = new cube.Deck(1, 2, 3, 4)
276
- ```
277
-
278
- Use the property `.data` to access the elements in the deck:
279
-
280
- ```js
281
- import cube from '@gabrielrufino/cube'
282
-
283
- const deck = new cube.Deck(1, 2, 3, 4)
284
- console.log(deck.data) // [ 1, 2, 3, 4 ]
285
- ```
286
-
287
- Use the property `.size` to get how many elements the deck has:
288
-
289
- ```js
290
- import cube from '@gabrielrufino/cube'
291
-
292
- const deck = new cube.Deck(1, 2, 3, 4)
293
- console.log(deck.size) // 4
294
- ```
295
-
296
- Use the method `.addFront()` to insert a new element in the front of the deck:
297
-
298
- ```js
299
- import cube from '@gabrielrufino/cube'
300
-
301
- const deck = new cube.Deck(2, 3, 4)
302
- deck.addFront(1)
303
- console.log(deck.data) // [ 1, 2, 3, 4 ]
304
- ```
305
-
306
- Use the method `.addBack()` to insert a new element in the back of the deck:
307
-
308
- ```js
309
- import cube from '@gabrielrufino/cube'
310
-
311
- const deck = new cube.Deck(1, 2, 3)
312
- deck.addBack(4)
313
- console.log(deck.data) // [ 1, 2, 3, 4 ]
314
- ```
315
-
316
- Use the method `.removeFront()` to get and remove an element from the front of the deck:
317
-
318
- ```js
319
- import cube from '@gabrielrufino/cube'
320
-
321
- const deck = new cube.Deck(1, 2, 3, 4)
322
- const element = deck.removeFront()
323
- console.log(deck.data) // [ 2, 3, 4 ]
324
- console.log(element) // 1
325
- ```
326
-
327
- Use the method `.removeBack()` to get and remove an element from the back of the deck:
328
-
329
- ```js
330
- import cube from '@gabrielrufino/cube'
331
-
332
- const deck = new cube.Deck(1, 2, 3, 4)
333
- const element = deck.removeBack()
334
- console.log(deck.data) // [ 1, 2, 3 ]
335
- console.log(element) // 4
336
- ```
337
-
338
- Use the method `.peekFront()` to get an element from the front of the deck without removing:
339
-
340
- ```js
341
- import cube from '@gabrielrufino/cube'
342
-
343
- const deck = new cube.Deck(1, 2, 3, 4)
344
- const element = deck.peekFront()
345
- console.log(deck.data) // [ 1, 2, 3, 4 ]
346
- console.log(element) // 1
347
- ```
348
-
349
- Use the method `.peekBack()` to get an element from the back of the deck without removing:
350
-
351
- ```js
352
- import cube from '@gabrielrufino/cube'
353
-
354
- const deck = new cube.Deck(1, 2, 3, 4)
355
- const element = deck.peekBack()
356
- console.log(deck.data) // [ 1, 2, 3, 4 ]
357
- console.log(element) // 4
358
- ```
359
-
360
- #### Linked List
361
-
362
- You can initialize an empty linked list or a linked list with some elements:
363
-
364
- ```js
365
- import cube from '@gabrielrufino/cube'
366
-
367
- const linkedList1 = new cube.LinkedList()
368
- const linkedList2 = new cube.LinkedList(1, 2, 3, 4)
369
- ```
370
-
371
- Use the property `.data` to access the elements in the linked list:
372
-
373
- ```js
374
- import cube from '@gabrielrufino/cube'
375
-
376
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
377
- console.log(linkedList.data)
378
- /*
379
- [
380
- { value: 1, next: 2 },
381
- { value: 2, next: 3 },
382
- { value: 3, next: 4 },
383
- { value: 4, next: null }
384
- ]
385
- */
386
- ```
387
-
388
- Use the property `.size` to get how many elements the linked list has:
389
-
390
- ```js
391
- import cube from '@gabrielrufino/cube'
392
-
393
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
394
- console.log(linkedList.size) // 4
395
- ```
396
-
397
- Use the property `.isEmpty` to find out if the linked list is empty:
398
-
399
- ```js
400
- import cube from '@gabrielrufino/cube'
401
-
402
- const linkedList1 = new cube.LinkedList()
403
- const linkedList2 = new cube.LinkedList(1, 2, 3, 4)
404
- console.log(linkedList1.isEmpty) // true
405
- console.log(linkedList2.isEmpty) // false
406
- ```
407
-
408
- Use the method `.push()` to insert a new element in the linked list:
409
-
410
- ```js
411
- import cube from '@gabrielrufino/cube'
412
-
413
- const linkedList = new cube.LinkedList(1, 2, 3)
414
- linkedList.push(4)
415
- console.log(linkedList.data)
416
- /*
417
- [
418
- { value: 1, next: 2 },
419
- { value: 2, next: 3 },
420
- { value: 3, next: 4 },
421
- { value: 4, next: null }
422
- ]
423
- */
424
- ```
425
-
426
- Use the method `.remove()` to remove an element from the linked lisr:
427
-
428
- ```js
429
- import cube from '@gabrielrufino/cube'
430
-
431
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
432
- linkedList.remove(3)
433
- console.log(linkedList.data)
434
- /*
435
- [
436
- { value: 1, next: 2 },
437
- { value: 2, next: 4 },
438
- { value: 4, next: null }
439
- ]
440
- */
441
- ```
442
-
443
- Use the method `.removeFromPosition()` to remove an element from the received position in the linked list:
444
-
445
- ```js
446
- import cube from '@gabrielrufino/cube'
447
-
448
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
449
- linkedList.removeFromPosition(2)
450
- console.log(linkedList.data)
451
- /*
452
- [
453
- { value: 1, next: 2 },
454
- { value: 2, next: 4 },
455
- { value: 4, next: null }
456
- ]
457
- */
458
- ```
459
-
460
- Use the method `.getFromPosition()` to find some element at the received position in the linked list:
461
-
462
- ```js
463
- import cube from '@gabrielrufino/cube'
464
-
465
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
466
- const element = linkedList.getFromPosition(1)
467
- console.log(element) // { value: 2, next: 3 }
468
- ```
469
-
470
- Use the method `.insertInPosition()` to insert a new element in the specified position:
471
-
472
- ```js
473
- import cube from '@gabrielrufino/cube'
474
-
475
- const linkedList = new cube.LinkedList(1, 2, 4)
476
- linkedList.insertInPosition(3, 2)
477
- console.log(linkedList.data)
478
- /*
479
- [
480
- { value: 1, next: 2 },
481
- { value: 2, next: 3 },
482
- { value: 3, next: 4 },
483
- { value: 4, next: null }
484
- ]
485
- */
486
- ```
487
-
488
- Use the method `.positionOf()` to find out the position of the specified element:
489
-
490
- ```js
491
- import cube from '@gabrielrufino/cube'
492
-
493
- const linkedList = new cube.LinkedList(1, 2, 3, 4)
494
- const position = linkedList.positionOf(2)
495
- console.log(position) // 1
496
- ```
497
-
498
- #### Doubly Linked List
499
-
500
- You can initialize an empty doubly linked list or a doubly linked list with some elements:
501
-
502
- ```js
503
- import cube from '@gabrielrufino/cube'
504
-
505
- const doublyLinkedList1 = new cube.DoublyLinkedList()
506
- const doublyLinkedList2 = new cube.DoublyLinkedList(1, 2, 3, 4)
507
- ```
508
-
509
- Use the property `.data` to access the elements in the doubly linked list:
510
-
511
- ```js
512
- import cube from '@gabrielrufino/cube'
513
-
514
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
515
- console.log(doublyLinkedList.data)
516
- /*
517
- [
518
- { previous: null, value: 1, next: 2 },
519
- { previous: 1, value: 2, next: 3 },
520
- { previous: 2, value: 3, next: 4 },
521
- { previous: 3, value: 4, next: null }
522
- ]
523
- */
524
- ```
525
-
526
- Use the property `.size` to get how many elements the doubly linked list has:
527
-
528
- ```js
529
- import cube from '@gabrielrufino/cube'
530
-
531
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
532
- console.log(doublyLinkedList.size) // 4
533
- ```
534
-
535
- Use the method `.push()` to insert a new element in the doubly linked list:
536
-
537
- ```js
538
- import cube from '@gabrielrufino/cube'
539
-
540
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3)
541
- doublyLinkedList.push(4)
542
- console.log(doublyLinkedList.data)
543
- /*
544
- [
545
- { previous: null, value: 1, next: 2 },
546
- { previous: 1, value: 2, next: 3 },
547
- { previous: 2, value: 3, next: 4 },
548
- { previous: 3, value: 4, next: null }
549
- ]
550
- */
551
- ```
552
-
553
- Use the method `.getFromPosition()` to get some element from specified position:
554
-
555
- ```js
556
- import cube from '@gabrielrufino/cube'
557
-
558
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
559
- const element = doublyLinkedList.getFromPosition(2)
560
- console.log(element) // { previous: 2, value: 3, next: 4 }
561
- ```
562
-
563
- Use the method `.positionOf()` to find out the position of some element:
564
-
565
- ```js
566
- import cube from '@gabrielrufino/cube'
567
-
568
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
569
- const position = doublyLinkedList.positionOf(3)
570
- console.log(position) // 2
571
- ```
572
-
573
- Use the method `.insertInPosition()` to insert a new element in the specified position:
574
-
575
- ```js
576
- import cube from '@gabrielrufino/cube'
577
-
578
- const doublyLinkedList = new cube.DoublyLinkedList(1, 3, 4)
579
- doublyLinkedList.insertInPosition(2, 1)
580
- console.log(doublyLinkedList.data)
581
- /*
582
- [
583
- { previous: null, value: 1, next: 2 },
584
- { previous: 1, value: 2, next: 3 },
585
- { previous: 2, value: 3, next: 4 },
586
- { previous: 3, value: 4, next: null }
587
- ]
588
- */
589
- ```
590
-
591
- Use the method `.remove()` to remove the specified element:
592
-
593
- ```js
594
- import cube from '@gabrielrufino/cube'
595
-
596
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
597
- doublyLinkedList.remove(3)
598
- console.log(doublyLinkedList.data)
599
- /*
600
- [
601
- { previous: null, value: 1, next: 2 },
602
- { previous: 1, value: 2, next: 4 },
603
- { previous: 2, value: 4, next: null }
604
- ]
605
- */
606
- ```
607
-
608
- Use the method `.removeFromPosition()` to remove the element from the specified element:
609
-
610
- ```js
611
- import cube from '@gabrielrufino/cube'
612
-
613
- const doublyLinkedList = new cube.DoublyLinkedList(1, 2, 3, 4)
614
- doublyLinkedList.removeFromPosition(2)
615
- console.log(doublyLinkedList.data)
616
- /*
617
- [
618
- { previous: null, value: 1, next: 2 },
619
- { previous: 1, value: 2, next: 4 },
620
- { previous: 2, value: 4, next: null }
621
- ]
622
- */
623
- ```
624
-
625
- #### Set
626
-
627
- You can initialize an empty set or a set with some elements:
628
-
629
- ```js
630
- import cube from '@gabrielrufino/cube'
631
-
632
- const set1 = new cube.Set()
633
- const set2 = new cube.Set(1, 2, 3, 4)
634
- ```
635
-
636
- Use the property `.data` to access the elements in the set:
637
-
638
- ```js
639
- import cube from '@gabrielrufino/cube'
640
-
641
- const set = new cube.Set(1, 2, 3, 4)
642
- console.log(set.data) // [ 1, 2, 3, 4 ]
643
- ```
644
-
645
- Use the property `.size` to get how many elements the set has:
646
-
647
- ```js
648
- import cube from '@gabrielrufino/cube'
649
-
650
- const set = new cube.Set(1, 2, 3, 4)
651
- console.log(set.size) // 4
652
- ```
653
-
654
- Use the method `.has()` to find out if the set has the specified element:
655
-
656
- ```js
657
- import cube from '@gabrielrufino/cube'
658
-
659
- const set = new cube.Set(1, 2, 3, 4)
660
- console.log(set.has(2)) // true
661
- console.log(set.has(5)) // false
662
- ```
663
-
664
- Use the method `.add()` to insert a new element in the set. If the element is already in the set, nothing changes:
665
-
666
- ```js
667
- import cube from '@gabrielrufino/cube'
668
-
669
- const set = new cube.Set(1, 2, 3)
670
- set.add(4)
671
- console.log(set.data) // [ 1, 2, 3, 4 ]
672
-
673
- set.add(4)
674
- console.log(set.data) // [ 1, 2, 3, 4 ]
675
- ```
676
-
677
- Use the method `.delete()` to remove the specified element from the set:
678
-
679
- ```js
680
- import cube from '@gabrielrufino/cube'
681
-
682
- const set = new cube.Set(1, 2, 3, 4)
683
- set.delete(2)
684
- console.log(set.data) // [ 1, 3, 4 ]
685
- ```
686
-
687
- Use the method `.clear()` to remove all the elements in the set:
688
-
689
- ```js
690
- import cube from '@gabrielrufino/cube'
691
-
692
- const set = new cube.Set(1, 2, 3, 4)
693
- set.clear()
694
- console.log(set.data) // []
695
- ```
696
-
697
- Use the method `.union()` to unite two different sets:
698
-
699
- ```js
700
- import cube from '@gabrielrufino/cube'
701
-
702
- const set1 = new cube.Set(1, 2, 3)
703
- const set2 = new cube.Set(2, 3, 4)
704
- const union = set1.union(set2)
705
-
706
- console.log(union.data) // [ 1, 2, 3, 4 ]
707
-
708
- // You can also use the static method for this operation:
709
- console.log(cube.Set.union(set1, set2).data) // [ 1, 2, 3, 4 ]
710
- ```
711
-
712
- Use the method `.intersection()` to find out the intersection set between two different sets:
713
-
714
- ```js
715
- import cube from '@gabrielrufino/cube'
716
-
717
- const set1 = new cube.Set(1, 2, 3, 4)
718
- const set2 = new cube.Set(3, 4, 5, 6)
719
- const intersection = set1.intersection(set2)
720
-
721
- console.log(intersection.data) // [ 3, 4 ]
722
-
723
- // You can also use the static method for this operation:
724
- console.log(cube.Set.intersection(set1, set2).data) // [ 3, 4 ]
725
- ```
726
-
727
- Use the method `.difference()` to find out the difference set between two different sets:
728
-
729
- ```js
730
- import cube from '@gabrielrufino/cube'
731
-
732
- const set1 = new cube.Set(1, 2, 3, 4)
733
- const set2 = new cube.Set(3, 4, 5, 6)
734
- const difference = set1.difference(set2)
735
-
736
- console.log(difference.data) // [ 1, 2 ]
737
-
738
- // You can also use the static method for this operation:
739
- console.log(cube.Set.difference(set1, set2).data) // [ 1, 2 ]
740
- ```
741
-
742
- Use the method `.isSubsetOf()` to find out if the set is subset of the specified set:
743
-
744
- ```js
745
- import cube from '@gabrielrufino/cube'
746
-
747
- const set1 = new cube.Set(1, 2)
748
- const set2 = new cube.Set(1, 2, 3, 4)
749
- const set3 = new cube.Set(3, 4, 5, 6)
750
-
751
- console.log(set1.isSubsetOf(set2)) // true
752
- console.log(set1.isSubsetOf(set3)) // false
753
-
754
- // You can also use the static method for this operation:
755
- console.log(cube.Set.isSubset(set1, set2)) // true
756
- ```
757
-
758
- Use the method `.contains()` to find out if the set contains the specified set:
759
-
760
- ```js
761
- import cube from '@gabrielrufino/cube'
762
-
763
- const set1 = new cube.Set(1, 2, 3, 4)
764
- const set2 = new cube.Set(2, 4)
765
- const set3 = new cube.Set(3, 5)
766
-
767
- console.log(set1.contains(set2)) // true
768
- console.log(set1.contains(set3)) // false
769
- ```
770
-
771
- #### Dictionary
772
-
773
- You can initialize an empty dictionary or a dictionary with some elements:
774
-
775
- ```js
776
- import cube from '@gabrielrufino/cube'
777
-
778
- const dictionary1 = new cube.Dictionary()
779
- const dictionary2 = new cube.Dictionary({
780
- first: 1,
781
- second: 2,
782
- third: 3,
783
- fourth: 4
784
- })
785
- ```
786
-
787
- Use the property `.data` to access the elements in the dictionary:
788
-
789
- ```js
790
- import cube from '@gabrielrufino/cube'
791
-
792
- const dictionary = new cube.Dictionary({
793
- first: 1,
794
- second: 2,
795
- third: 3,
796
- fourth: 4
797
- })
798
-
799
- console.log(dictionary.data) // { first: 1, second: 2, third: 3, fourth: 4 }
800
- ```
801
-
802
- Use the property `.size` to get how many elements the dictionary has:
803
-
804
- ```js
805
- import cube from '@gabrielrufino/cube'
806
-
807
- const dictionary = new cube.Dictionary({
808
- first: 1,
809
- second: 2,
810
- third: 3,
811
- fourth: 4
812
- })
813
- console.log(dictionary.size) // 4
814
- ```
815
-
816
- Use the property `.isEmpty` to find out is the dictionary is empty:
817
-
818
- ```js
819
- import cube from '@gabrielrufino/cube'
820
-
821
- const dictionary1 = new cube.Dictionary()
822
- const dictionary2 = new cube.Dictionary({ first: 1 })
823
-
824
- console.log(dictionary1.isEmpty) // true
825
- console.log(dictionary2.isEmpty) // false
826
- ```
827
-
828
- Use the property `.keys` to get the list of all dictionary keys:
829
-
830
- ```js
831
- import cube from '@gabrielrufino/cube'
832
-
833
- const dictionary = new cube.Dictionary({
834
- first: 1,
835
- second: 2,
836
- third: 3,
837
- fourth: 4
838
- })
839
- console.log(dictionary.keys) // [ 'first', 'second', 'third', 'fourth' ]
840
- ```
841
-
842
- Use the property `.values` to get the list of all dictionary values:
843
-
844
- ```js
845
- import cube from '@gabrielrufino/cube'
846
-
847
- const dictionary = new cube.Dictionary({
848
- first: 1,
849
- second: 2,
850
- third: 3,
851
- fourth: 4
852
- })
853
- console.log(dictionary.values) // [ 1, 2, 3, 4 ]
854
- ```
855
-
856
- Use the property `.pairs` to get the list of all key value pairs:
857
-
858
- ```js
859
- import cube from '@gabrielrufino/cube'
860
-
861
- const dictionary = new cube.Dictionary({
862
- first: 1,
863
- second: 2,
864
- third: 3,
865
- fourth: 4
866
- })
867
- console.log(dictionary.pairs) // [ [ 'first', 1 ], [ 'second', 2 ], [ 'third', 3 ], [ 'fourth', 4 ] ]
868
- ```
869
-
870
- Use the method `.set()` to set a new key value pair in the dictionary:
871
-
872
- ```js
873
- import cube from '@gabrielrufino/cube'
874
-
875
- const dictionary = new cube.Dictionary({
876
- first: 1
877
- })
878
- dictionary.set('second', 2)
879
- console.log(dictionary.data) // { first: 1, second: 2 }
880
- ```
881
-
882
- Use the method `.remove()` to remove the key value pair of the specified key:
883
-
884
- ```js
885
- import cube from '@gabrielrufino/cube'
886
-
887
- const dictionary = new cube.Dictionary({
888
- first: 1,
889
- second: 2
890
- })
891
- dictionary.remove('first')
892
- console.log(dictionary.data) // { second: 2 }
893
- ```
894
-
895
- Use the method `.hasKey()` to find out if the dictionary has or not a key value pair with the specified key:
896
-
897
- ```js
898
- import cube from '@gabrielrufino/cube'
899
-
900
- const dictionary = new cube.Dictionary({
901
- first: 1
902
- })
903
- console.log(dictionary.hasKey('first')) // true
904
- console.log(dictionary.hasKey('second')) // false
905
- ```
906
-
907
- Use the method `.get()` to get the value of the specified key:
908
-
909
- ```js
910
- import cube from '@gabrielrufino/cube'
911
-
912
- const dictionary = new cube.Dictionary({
913
- first: 1
914
- })
915
- const value = dictionary.get('first')
916
- console.log(value) // 1
917
- ```
918
-
919
- Use the method `.clear()` to remove all the key value pairs from the dictionary:
920
-
921
- ```js
922
- import cube from '@gabrielrufino/cube'
923
-
924
- const dictionary = new cube.Dictionary({
925
- first: 1,
926
- second: 2,
927
- third: 3,
928
- fourth: 4
929
- })
930
- dictionary.clear()
931
- console.log(dictionary.data) // {}
932
- ```
933
-
934
- Use the method `.forEach()` to go through the dictionary with the specified callback:
935
-
936
- ```js
937
- import cube from '@gabrielrufino/cube'
938
-
939
- const dictionary = new cube.Dictionary({
940
- first: 1,
941
- second: 2,
942
- third: 3,
943
- fourth: 4,
944
- })
945
- dictionary.forEach((key, value) => {
946
- console.log(`Key: ${key}, Value: ${value}`)
947
- })
948
- /*
949
- Key: first, Value: 1
950
- Key: second, Value: 2
951
- Key: third, Value: 3
952
- Key: fourth, Value: 4
953
- */
954
- ```
955
-
956
- #### Hash Table
957
-
958
- You can initialize an empty hash table or a hash table with some elements:
959
-
960
- ```js
961
- import cube from '@gabrielrufino/cube'
962
-
963
- const hashTable1 = new cube.HashTable()
964
- const hashTable2 = new cube.HashTable({
965
- first: 1,
966
- second: 2,
967
- third: 3,
968
- fourth: 4
969
- })
970
- ```
971
-
972
- Use the property `.data` to access the elements in the hash table:
973
-
974
- ```js
975
- import cube from '@gabrielrufino/cube'
976
-
977
- const hashTable = new cube.HashTable({
978
- first: 1,
979
- second: 2,
980
- third: 3,
981
- fourth: 4
982
- })
983
-
984
- console.log(hashTable.data) // { '36': 2, '39': 3, '52': 1, '64': 4 }
985
- ```
986
-
987
- Use the property `.size` to get how many elements the hash table has:
988
-
989
- ```js
990
- import cube from '@gabrielrufino/cube'
991
-
992
- const hashTable = new cube.HashTable({
993
- first: 1,
994
- second: 2,
995
- third: 3,
996
- fourth: 4
997
- })
998
- console.log(hashTable.size) // 4
999
- ```
1000
-
1001
- Use the method `.put()` to insert a new element in the hash table:
1002
-
1003
- ```js
1004
- import cube from '@gabrielrufino/cube'
1005
-
1006
- const hashTable = new cube.HashTable({
1007
- first: 1
1008
- })
1009
- hashTable.put('second', 2)
1010
- console.log(hashTable.data) // { '36': 2, '52': 1 }
1011
- ```
1012
-
1013
- Use the method `.get()` to get the value of the specified key:
1014
-
1015
- ```js
1016
- import cube from '@gabrielrufino/cube'
1017
-
1018
- const hashTable = new cube.HashTable({
1019
- first: 1,
1020
- second: 2
1021
- })
1022
- const value = hashTable.get('first')
1023
-
1024
- console.log(value) // 1
1025
- ```
1026
-
1027
- Use the method `.remove()` to remove the key value pair of the specified key:
1028
-
1029
- ```js
1030
- import cube from '@gabrielrufino/cube'
1031
-
1032
- const hashTable = new cube.HashTable({
1033
- first: 1,
1034
- second: 2
1035
- })
1036
- hashTable.remove('second')
1037
-
1038
- console.log(hashTable.data) // { '52': 1 }
1039
- ```
1040
-
1041
- ##### Hash Table Separate Chaining
1042
-
1043
- The `HashTableSeparateChaining` class has the same method of the `HashTable` class. The difference is on the collision strategy.
1044
-
1045
- All the values are actually linked lists. This means that in case of conflict, the new element is pushed to the linked list.
1046
-
1047
- ```js
1048
- import cube from '@gabrielrufino/cube'
1049
-
1050
- const hashTableSeparateChaining = new cube.HashTableSeparateChaining({
1051
- first: 1
1052
- })
1053
-
1054
- console.log(hashTableSeparateChaining.data)
1055
- /*
1056
- {
1057
- '52': LinkedList {}
1058
- }
1059
- */
1060
-
1061
- console.log(hashTableSeparateChaining.data['52'].data)
1062
- /*
1063
- [
1064
- {
1065
- value: HashTableSeparateChainingElement { key: 'first', value: 1 },
1066
- next: null
1067
- }
1068
- ]
1069
- */
1070
-
1071
- hashTableSeparateChaining.put('tsrif', -1)
1072
- console.log(hashTableSeparateChaining.data['52'].data)
1073
- /*
1074
- [
1075
- {
1076
- value: HashTableSeparateChainingElement { key: 'first', value: 1 },
1077
- next: HashTableSeparateChainingElement { key: 'tsrif', value: -1 }
1078
- },
1079
- {
1080
- value: HashTableSeparateChainingElement { key: 'tsrif', value: -1 },
1081
- next: null
1082
- }
1083
- ]
1084
- */
1085
- ```
1086
-
1087
- ##### Hash Table Linear Probing
1088
-
1089
- The `HashTableLinearProbing` class has the same method of the `HashTable` class. The difference is on the collision strategy.
1090
-
1091
- In case of conflict, the position of the new element will be the next available position.
1092
-
1093
- ```js
1094
- import cube from '@gabrielrufino/cube'
1095
-
1096
- const hashTableLinearProbing = new cube.HashTableLinearProbing({
1097
- first: 1
1098
- }, {
1099
- maxSize: 2
1100
- })
1101
-
1102
- console.log(hashTableLinearProbing.data)
1103
- /*
1104
- { '0': HashTableLinearProbingElement { key: 'first', value: 1 } }
1105
- */
1106
-
1107
- hashTableLinearProbing.put('tsrif', -1)
1108
- console.log(hashTableLinearProbing.data)
1109
- /*
1110
- {
1111
- '0': HashTableLinearProbingElement { key: 'first', value: 1 },
1112
- '1': HashTableLinearProbingElement { key: 'tsrif', value: -1 }
1113
- }
1114
- */
1115
- ```
1116
-
1117
- #### Binary Search Tree
1118
-
1119
- You can initialize an empty binary search tree or a binary search tree with some elements:
1120
-
1121
- ```js
1122
- import cube from '@gabrielrufino/cube'
1123
-
1124
- const bst1 = new cube.BinarySearchTree()
1125
- const bst2 = new cube.BinarySearchTree({
1126
- inputs: [4, 2, 1, 3, 5],
1127
- })
1128
- ```
1129
-
1130
- Use the property `.data` to access the elements in the binary search tree:
1131
-
1132
- ```js
1133
- import cube from '@gabrielrufino/cube'
1134
-
1135
- const bst = new cube.BinarySearchTree({
1136
- inputs: [4, 2, 1, 3, 5],
1137
- })
1138
-
1139
- console.log(bst.data)
1140
- /*
1141
- {
1142
- left: {
1143
- left: { left: null, right: null, value: 1 },
1144
- right: { left: null, right: null, value: 3 },
1145
- value: 2
1146
- },
1147
- right: { left: null, right: null, value: 5 },
1148
- value: 4
1149
- }
1150
- */
1151
- ```
1152
-
1153
- Use the property `.size` to get how many elements the binary search tree has:
1154
-
1155
- ```js
1156
- import cube from '@gabrielrufino/cube'
1157
-
1158
- const bst = new cube.BinarySearchTree({
1159
- inputs: [4, 2, 1, 3, 5],
1160
- })
1161
- console.log(bst.size) // 5
1162
- ```
1163
-
1164
- Use the method `.insert()` to insert a new element in the binary search tree:
1165
-
1166
- ```js
1167
- import cube from '@gabrielrufino/cube'
1168
-
1169
- const bst = new cube.BinarySearchTree({
1170
- inputs: [2, 1, 4]
1171
- })
1172
- bst.insert(3)
1173
-
1174
- console.log(bst.data)
1175
- /*
1176
- {
1177
- left: { left: null, right: null, value: 1 },
1178
- right: {
1179
- left: { left: null, right: null, value: 3 },
1180
- right: null,
1181
- value: 4
1182
- },
1183
- value: 2
1184
- }
1185
- */
1186
- ```
1187
-
1188
- Use the method `.walkInOrder()` to walk through the tree using the algorithm **in order** with the specified callback:
1189
-
1190
- ```js
1191
- import cube from '@gabrielrufino/cube'
1192
-
1193
- const bst = new cube.BinarySearchTree({
1194
- inputs: [2, 1, 4, 3]
1195
- })
1196
-
1197
- bst.walkInOrder(value => {
1198
- console.log(value)
1199
- })
1200
- /*
1201
- 1
1202
- 2
1203
- 3
1204
- 4
1205
- */
1206
- ```
1207
-
1208
- Use the method `.walkPreOrder()` to walk through the tree using the algorithm **pre order** with the specified callback:
1209
-
1210
- ```js
1211
- import cube from '@gabrielrufino/cube'
1212
-
1213
- const bst = new cube.BinarySearchTree({
1214
- inputs: [2, 1, 4, 3]
1215
- })
1216
-
1217
- bst.walkPreOrder(value => {
1218
- console.log(value)
1219
- })
1220
- /*
1221
- 2
1222
- 1
1223
- 4
1224
- 3
1225
- */
1226
- ```
1227
-
1228
- Use the method `.walkPostOrder()` to walk through the tree using the algorithm **post order** with the specified callback:
1229
-
1230
- ```js
1231
- import cube from '@gabrielrufino/cube'
1232
-
1233
- const bst = new cube.BinarySearchTree({
1234
- inputs: [2, 1, 4, 3]
1235
- })
1236
-
1237
- bst.walkPostOrder(value => {
1238
- console.log(value)
1239
- })
1240
-
1241
- /*
1242
- 1
1243
- 3
1244
- 4
1245
- 2
1246
- */
1247
- ```
1248
-
1249
- Use the property `.min` to get the minimum element in the binary search tree:
1250
-
1251
- ```js
1252
- import cube from '@gabrielrufino/cube'
1253
-
1254
- const bst = new cube.BinarySearchTree({
1255
- inputs: [2, 1, 4, 3]
1256
- })
1257
- console.log(bst.min) // 1
1258
- ```
1259
-
1260
- Use the property `.max` to get the maximum element in the binary search tree:
1261
-
1262
- ```js
1263
- import cube from '@gabrielrufino/cube'
1264
-
1265
- const bst = new cube.BinarySearchTree({
1266
- inputs: [2, 1, 4, 3]
1267
- })
1268
- console.log(bst.max) // 4
1269
- ```
1270
-
1271
- Use the method `.search()` to find out if the specified element in the binary search tree:
1272
-
1273
- ```js
1274
- import cube from '@gabrielrufino/cube'
1275
-
1276
- const bst = new cube.BinarySearchTree({
1277
- inputs: [2, 1, 4, 3]
1278
- })
1279
- console.log(bst.search(4)) // true
1280
- console.log(bst.search(5)) // false
1281
- ```
1282
-
1283
- Use the method `.remove()` to remove the specified element from the binary search tree:
1284
-
1285
- ```js
1286
- import cube from '@gabrielrufino/cube'
1287
-
1288
- const bst = new cube.BinarySearchTree({
1289
- inputs: [2, 1, 4, 3]
1290
- })
1291
- bst.remove(4)
1292
-
1293
- console.log(bst.search(4)) // false
1294
- console.log(bst.data)
1295
- /*
1296
- {
1297
- left: { left: null, right: null, value: 1 },
1298
- right: { left: null, right: null, value: 3 },
1299
- value: 2
1300
- }
1301
- */
1302
- ```
1303
-
1304
- #### Min Heap
1305
-
1306
- You can initialize an empty min heap or a min heap with some elements:
1307
-
1308
- ```js
1309
- import cube from '@gabrielrufino/cube'
1310
-
1311
- const minHeap1 = new cube.MinHeap()
1312
- const minHeap2 = new cube.MinHeap({
1313
- inputs: [4, 2, 1, 3, 5],
1314
- })
1315
- ```
1316
-
1317
- Use the property `.data` to access the elements in the min heap:
1318
-
1319
- ```js
1320
- import cube from '@gabrielrufino/cube'
1321
-
1322
- const minHeap = new cube.MinHeap({
1323
- inputs: [4, 2, 1, 3, 5],
1324
- })
1325
-
1326
- console.log(minHeap.data) // [ 1, 3, 2, 4, 5 ]
1327
- ```
1328
-
1329
- Use the property `.size` to get how many elements the min heap has:
1330
-
1331
- ```js
1332
- import cube from '@gabrielrufino/cube'
1333
-
1334
- const minHeap = new cube.MinHeap({
1335
- inputs: [4, 2, 1, 3, 5],
1336
- })
1337
- console.log(minHeap.size) // 5
1338
- ```
1339
-
1340
- Use the property `.isEmpty` to find out if the min heap is empty:
1341
-
1342
- ```js
1343
- import cube from '@gabrielrufino/cube'
1344
-
1345
- const minHeap1 = new cube.MinHeap()
1346
- const minHeap2 = new cube.MinHeap({
1347
- inputs: [4, 2, 1, 3, 5],
1348
- })
1349
- console.log(minHeap1.isEmpty) // true
1350
- console.log(minHeap2.isEmpty) // false
1351
- ```
1352
-
1353
- Use the property `.min` to get the minimum element in the min heap:
1354
-
1355
- ```js
1356
- import cube from '@gabrielrufino/cube'
1357
-
1358
- const minHeap = new cube.MinHeap({
1359
- inputs: [4, 2, 1, 3, 5],
1360
- })
1361
- console.log(minHeap.min) // 1
1362
- ```
1363
-
1364
- Use the method `.insert()` to insert a new element in the min heap:
1365
-
1366
- ```js
1367
- import cube from '@gabrielrufino/cube'
1368
-
1369
- const minHeap = new cube.MinHeap({
1370
- inputs: [4, 2, 1, 3, 5],
1371
- })
1372
- minHeap.insert(6)
1373
-
1374
- console.log(minHeap.data) // [ 1, 3, 2, 4, 5, 6 ]
1375
- ```
1376
-
1377
- Use the method `.extract()` to extract the min value of the min heap:
1378
-
1379
- ```js
1380
- import cube from '@gabrielrufino/cube'
1381
-
1382
- const minHeap = new cube.MinHeap({
1383
- inputs: [4, 2, 1, 3, 5]
1384
- })
1385
- const min = minHeap.extract()
1386
-
1387
- console.log(min) // 1
1388
- console.log(minHeap.data) // [ 2, 4, 3, 5 ]
1389
- ```
1390
-
1391
- #### Max Heap
1392
-
1393
- You can initialize an empty max heap or a max heap with some elements:
1394
-
1395
- ```js
1396
- import cube from '@gabrielrufino/cube'
1397
-
1398
- const maxHeap1 = new cube.MaxHeap()
1399
- const maxHeap2 = new cube.MaxHeap({
1400
- inputs: [4, 2, 1, 3, 5],
1401
- })
1402
- ```
1403
-
1404
- Use the property `.data` to access the elements in the max heap:
1405
-
1406
- ```js
1407
- import cube from '@gabrielrufino/cube'
1408
-
1409
- const maxHeap = new cube.MaxHeap({
1410
- inputs: [4, 2, 1, 3, 5],
1411
- })
1412
-
1413
- console.log(maxHeap.data) // [ 5, 4, 1, 2, 3 ]
1414
- ```
1415
-
1416
- Use the property `.size` to get how many elements the max heap has:
1417
-
1418
- ```js
1419
- import cube from '@gabrielrufino/cube'
1420
-
1421
- const maxHeap = new cube.MaxHeap({
1422
- inputs: [4, 2, 1, 3, 5],
1423
- })
1424
- console.log(maxHeap.size) // 5
1425
- ```
1426
-
1427
- Use the property `.isEmpty` to find out if the max heap is empty:
1428
-
1429
- ```js
1430
- import cube from '@gabrielrufino/cube'
1431
-
1432
- const maxHeap1 = new cube.MaxHeap()
1433
- const maxHeap2 = new cube.MaxHeap({
1434
- inputs: [4, 2, 1, 3, 5],
1435
- })
1436
- console.log(maxHeap1.isEmpty) // true
1437
- console.log(maxHeap2.isEmpty) // false
1438
- ```
1439
-
1440
- Use the property `.max` to get the maximum element in the max heap:
1441
-
1442
- ```js
1443
- import cube from '@gabrielrufino/cube'
1444
-
1445
- const maxHeap = new cube.MaxHeap({
1446
- inputs: [4, 2, 1, 3, 5],
1447
- })
1448
- console.log(maxHeap.max) // 5
1449
- ```
1450
-
1451
- Use the method `.insert()` to insert a new element in the max heap:
1452
-
1453
- ```js
1454
- import cube from '@gabrielrufino/cube'
1455
-
1456
- const maxHeap = new cube.MaxHeap({
1457
- inputs: [4, 2, 1, 3, 5],
1458
- })
1459
- maxHeap.insert(6)
1460
-
1461
- console.log(maxHeap.data) // [ 6, 4, 5, 2, 3, 1 ]
1462
- ```
1463
-
1464
- Use the method `.extract()` to extract the max value of the max heap:
1465
-
1466
- ```js
1467
- import cube from '@gabrielrufino/cube'
1468
-
1469
- const maxHeap = new cube.MaxHeap({
1470
- inputs: [4, 2, 1, 3, 5]
1471
- })
1472
- const max = maxHeap.extract()
1473
-
1474
- console.log(max) // 5
1475
- console.log(maxHeap.data) // [ 4, 3, 1, 2 ]
1476
- ```
1477
-
1478
- #### Graph
1479
-
1480
- You can initialize an empty graph or a graph with some elements:
1481
-
1482
- ```js
1483
- import cube from '@gabrielrufino/cube'
1484
-
1485
- const graph1 = new cube.Graph()
1486
- const graph2 = new cube.Graph({
1487
- inputs: {
1488
- A: ['B', 'C', 'D'],
1489
- B: ['A', 'C'],
1490
- C: ['A', 'B'],
1491
- D: ['A'],
1492
- }
1493
- })
1494
- ```
1495
-
1496
- Use the property `.data` to access the elements in the graph:
1497
-
1498
- ```js
1499
- import cube from '@gabrielrufino/cube'
1500
-
1501
- const graph = new cube.Graph({
1502
- inputs: {
1503
- A: ['B', 'C', 'D'],
1504
- B: ['A', 'C'],
1505
- C: ['A', 'B'],
1506
- D: ['A'],
1507
- }
1508
- })
1509
-
1510
- console.log(graph.data)
1511
- /*
1512
- {
1513
- A: [ 'B', 'C', 'D' ],
1514
- B: [ 'A', 'C' ],
1515
- C: [ 'A', 'B' ],
1516
- D: [ 'A' ]
1517
- }
1518
- */
1519
- ```
1520
-
1521
- Use the property `.size` to get how many elements the graph has:
1522
-
1523
- ```js
1524
- import cube from '@gabrielrufino/cube'
1525
-
1526
- const graph = new cube.Graph({
1527
- inputs: {
1528
- A: ['B', 'C', 'D'],
1529
- B: ['A', 'C'],
1530
- C: ['A', 'B'],
1531
- D: ['A'],
1532
- }
1533
- })
1534
- console.log(graph.size) // 4
1535
- ```
1536
-
1537
- Use the property `.nodes` to get a list of all the nodes in the graph:
1538
-
1539
- ```js
1540
- import cube from '@gabrielrufino/cube'
1541
-
1542
- const graph = new cube.Graph({
1543
- inputs: {
1544
- A: ['B', 'C', 'D'],
1545
- B: ['A', 'C'],
1546
- C: ['A', 'B'],
1547
- D: ['A'],
1548
- }
1549
- })
1550
- console.log(graph.nodes) // [ 'A', 'B', 'C', 'D' ]
1551
- ```
1552
-
1553
- Use the property `.edges` to get a list of all the links between nodes in the graph:
1554
-
1555
- ```js
1556
- import cube from '@gabrielrufino/cube'
1557
-
1558
- const graph = new cube.Graph({
1559
- inputs: {
1560
- A: ['B', 'C', 'D'],
1561
- B: ['A', 'C'],
1562
- C: ['A', 'B'],
1563
- D: ['A'],
1564
- }
1565
- })
1566
- console.log(graph.edges)
1567
- /*
1568
- [
1569
- [ 'A', 'B' ],
1570
- [ 'A', 'C' ],
1571
- [ 'A', 'D' ],
1572
- [ 'B', 'C' ]
1573
- ]
1574
- */
1575
- ```
1576
-
1577
- Use the method `.insert()` to insert a new node in the graph:
1578
-
1579
- ```js
1580
- import cube from '@gabrielrufino/cube'
1581
-
1582
- const graph = new cube.Graph({
1583
- inputs: {
1584
- A: ['B', 'C', 'D'],
1585
- B: ['A', 'C'],
1586
- C: ['A', 'B'],
1587
- D: ['A'],
1588
- }
1589
- })
1590
- graph.insert('E')
1591
-
1592
- console.log(graph.data)
1593
- /*
1594
- {
1595
- A: [ 'B', 'C', 'D' ],
1596
- B: [ 'A', 'C' ],
1597
- C: [ 'A', 'B' ],
1598
- D: [ 'A' ],
1599
- E: []
1600
- }
1601
- */
1602
-
1603
- console.log(graph.nodes) // [ 'A', 'B', 'C', 'D', 'E' ]
1604
- ```
1605
-
1606
- Use the method `.connect()` to connect two disconnected nodes:
1607
-
1608
- ```js
1609
- import cube from '@gabrielrufino/cube'
1610
-
1611
- const graph = new cube.Graph({
1612
- inputs: {
1613
- A: ['B', 'C', 'D'],
1614
- B: ['A', 'C'],
1615
- C: ['A', 'B'],
1616
- D: ['A'],
1617
- E: []
1618
- }
1619
- })
1620
-
1621
- console.log(graph.data)
1622
- /*
1623
- {
1624
- A: [ 'B', 'C', 'D' ],
1625
- B: [ 'A', 'C' ],
1626
- C: [ 'A', 'B' ],
1627
- D: [ 'A' ],
1628
- E: []
1629
- }
1630
- */
1631
-
1632
- graph.connect('A', 'E')
1633
-
1634
- console.log(graph.data)
1635
- /*
1636
- {
1637
- A: [ 'B', 'C', 'D', 'E' ],
1638
- B: [ 'A', 'C' ],
1639
- C: [ 'A', 'B' ],
1640
- D: [ 'A' ],
1641
- E: [ 'A' ]
1642
- }
1643
- */
1644
- ```
1645
-
1646
- Use the method `.breadthFirstSearch()` to walk through all the graph:
1647
-
1648
- ```js
1649
- import cube from '@gabrielrufino/cube'
1650
-
1651
- const graph = new cube.Graph({
1652
- inputs: {
1653
- A: ['B', 'C', 'D'],
1654
- B: ['A', 'C'],
1655
- C: ['A', 'B'],
1656
- D: ['A'],
1657
- }
1658
- })
1659
-
1660
- graph.breadthFirstSearch('A', value => {
1661
- console.log(value)
1662
- })
1663
- /*
1664
- A
1665
- B
1666
- C
1667
- D
1668
- */
1669
- ```
1670
-
1671
- Use the method `.getDistancesFrom()` to get the distance between the specified node and the rest of nodes:
1672
-
1673
- ```js
1674
- import cube from '@gabrielrufino/cube'
1675
-
1676
- const graph = new cube.Graph({
1677
- inputs: {
1678
- A: ['B', 'C', 'D'],
1679
- B: ['A', 'C'],
1680
- C: ['A', 'B'],
1681
- D: ['A'],
1682
- }
1683
- })
1684
-
1685
- const distances = graph.getDistancesFrom('B')
1686
- console.log(distances)
1687
- /*
1688
- {
1689
- A: 1,
1690
- B: 0,
1691
- C: 1,
1692
- D: 2
1693
- }
1694
- */
1695
- ```
27
+ * [Array/List](./docs/Array.md)
28
+ * [Stack](./docs/Stack.md)
29
+ * [Queue](./docs/Queue.md)
30
+ * [Deck](./docs/Deck.md)
31
+ * [Linked List](./docs/LinkedList.md)
32
+ * [Doubly Linked List](./docs/DoublyLinkedList.md)
33
+ * [Set](./docs/Set.md)
34
+ * [Dictionary](./docs/Dictionary.md)
35
+ * [Hash Table](./docs/HashTable.md)
36
+ * [Hash Table Separate Chaining](./docs/HashTableSeparateChaining.md)
37
+ * [Hash Table Linear Probing](./docs/HashTableLinearProbing.md)
38
+ * [Binary Search Tree](./docs/BinarySearchTree.md)
39
+ * [Min Heap](./docs/MinHeap.md)
40
+ * [Max Heap](./docs/MaxHeap.md)
41
+ * [Graph](./docs/Graph.md)