data-structure-typed 1.39.1 → 1.39.3
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +58 -107
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +65 -27
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/bst.js +12 -12
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +14 -14
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +7 -7
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/helpers.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +58 -107
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +65 -27
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/bst.js +12 -12
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +14 -14
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +7 -7
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/helpers.d.ts +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +10 -10
- package/src/data-structures/binary-tree/binary-tree.ts +165 -53
- package/src/data-structures/binary-tree/bst.ts +29 -31
- package/src/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/data-structures/binary-tree/tree-multiset.ts +14 -14
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/helpers.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +33 -7
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +65 -6
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +57 -13
- package/test/unit/data-structures/queue/deque.test.ts +43 -43
- package/test/unit/data-structures/queue/queue.test.ts +7 -7
|
@@ -87,17 +87,13 @@ export class SinglyLinkedList<E = any> {
|
|
|
87
87
|
return singlyLinkedList;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
getLength(): number {
|
|
91
|
-
return this._length;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
90
|
/**
|
|
95
|
-
* The `push` function adds a new node with the given
|
|
96
|
-
* @param {E}
|
|
91
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
92
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
97
93
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
98
94
|
*/
|
|
99
|
-
push(
|
|
100
|
-
const newNode = new SinglyLinkedListNode(
|
|
95
|
+
push(val: E): void {
|
|
96
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
101
97
|
if (!this.head) {
|
|
102
98
|
this.head = newNode;
|
|
103
99
|
this.tail = newNode;
|
|
@@ -108,6 +104,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
108
104
|
this._length++;
|
|
109
105
|
}
|
|
110
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
109
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
110
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
111
|
+
*/
|
|
112
|
+
addLast(val: E): void {
|
|
113
|
+
this.push(val);
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
/**
|
|
112
117
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
113
118
|
* pointers accordingly.
|
|
@@ -135,6 +140,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
135
140
|
return val;
|
|
136
141
|
}
|
|
137
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
145
|
+
* pointers accordingly.
|
|
146
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
147
|
+
* the linked list is empty, it returns `null`.
|
|
148
|
+
*/
|
|
149
|
+
popLast(): E | undefined {
|
|
150
|
+
return this.pop();
|
|
151
|
+
}
|
|
152
|
+
|
|
138
153
|
/**
|
|
139
154
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
140
155
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -147,6 +162,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
147
162
|
return removedNode.val;
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
/**
|
|
166
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
167
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
168
|
+
*/
|
|
169
|
+
popFirst(): E | undefined {
|
|
170
|
+
return this.shift();
|
|
171
|
+
}
|
|
172
|
+
|
|
150
173
|
/**
|
|
151
174
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
152
175
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -164,6 +187,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
164
187
|
this._length++;
|
|
165
188
|
}
|
|
166
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
192
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
193
|
+
* linked list.
|
|
194
|
+
*/
|
|
195
|
+
addFirst(val: E): void {
|
|
196
|
+
this.unshift(val);
|
|
197
|
+
}
|
|
198
|
+
|
|
167
199
|
/**
|
|
168
200
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
169
201
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -382,7 +414,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
382
414
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
383
415
|
* the specified value is found, the function returns `null`.
|
|
384
416
|
*/
|
|
385
|
-
|
|
417
|
+
getNode(value: E): SinglyLinkedListNode<E> | null {
|
|
386
418
|
let current = this.head;
|
|
387
419
|
|
|
388
420
|
while (current) {
|
|
@@ -432,9 +464,6 @@ export class SinglyLinkedList<E = any> {
|
|
|
432
464
|
return false;
|
|
433
465
|
}
|
|
434
466
|
|
|
435
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
436
|
-
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
437
|
-
|
|
438
467
|
/**
|
|
439
468
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
440
469
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -449,7 +478,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
449
478
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
450
479
|
existingNode = existingValueOrNode;
|
|
451
480
|
} else {
|
|
452
|
-
existingNode = this.
|
|
481
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
453
482
|
}
|
|
454
483
|
|
|
455
484
|
if (existingNode) {
|
|
@@ -485,6 +514,82 @@ export class SinglyLinkedList<E = any> {
|
|
|
485
514
|
return count;
|
|
486
515
|
}
|
|
487
516
|
|
|
517
|
+
/**
|
|
518
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
519
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
520
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
521
|
+
* current node in the linked list.
|
|
522
|
+
*/
|
|
523
|
+
forEach(callback: (val: E, index: number) => void): void {
|
|
524
|
+
let current = this.head;
|
|
525
|
+
let index = 0;
|
|
526
|
+
while (current) {
|
|
527
|
+
callback(current.val, index);
|
|
528
|
+
current = current.next;
|
|
529
|
+
index++;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
535
|
+
* SinglyLinkedList with the transformed values.
|
|
536
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
537
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
538
|
+
* SinglyLinkedList).
|
|
539
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
540
|
+
*/
|
|
541
|
+
map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
|
|
542
|
+
const mappedList = new SinglyLinkedList<U>();
|
|
543
|
+
let current = this.head;
|
|
544
|
+
while (current) {
|
|
545
|
+
mappedList.push(callback(current.val));
|
|
546
|
+
current = current.next;
|
|
547
|
+
}
|
|
548
|
+
return mappedList;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
553
|
+
* elements that satisfy the given callback function.
|
|
554
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
555
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
556
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
557
|
+
*/
|
|
558
|
+
filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
|
|
559
|
+
const filteredList = new SinglyLinkedList<E>();
|
|
560
|
+
let current = this.head;
|
|
561
|
+
while (current) {
|
|
562
|
+
if (callback(current.val)) {
|
|
563
|
+
filteredList.push(current.val);
|
|
564
|
+
}
|
|
565
|
+
current = current.next;
|
|
566
|
+
}
|
|
567
|
+
return filteredList;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
572
|
+
* single value.
|
|
573
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
574
|
+
* used to perform a specific operation on each element of the linked list.
|
|
575
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
576
|
+
* point for the reduction operation.
|
|
577
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
578
|
+
* elements in the linked list.
|
|
579
|
+
*/
|
|
580
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
|
|
581
|
+
let accumulator = initialValue;
|
|
582
|
+
let current = this.head;
|
|
583
|
+
while (current) {
|
|
584
|
+
accumulator = callback(accumulator, current.val);
|
|
585
|
+
current = current.next;
|
|
586
|
+
}
|
|
587
|
+
return accumulator;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
|
+
*/
|
|
488
593
|
*[Symbol.iterator]() {
|
|
489
594
|
let current = this.head;
|
|
490
595
|
|
|
@@ -95,12 +95,12 @@ export class ObjectDeque<E = number> {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* The function `
|
|
98
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
99
99
|
* @returns The value of the first element in the data structure.
|
|
100
100
|
*/
|
|
101
|
-
|
|
101
|
+
popFirst() {
|
|
102
102
|
if (!this._size) return;
|
|
103
|
-
const value = this.
|
|
103
|
+
const value = this.getFirst();
|
|
104
104
|
delete this._nodes[this._first];
|
|
105
105
|
this._first++;
|
|
106
106
|
this._size--;
|
|
@@ -108,20 +108,20 @@ export class ObjectDeque<E = number> {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
|
-
* The `
|
|
111
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
112
112
|
* @returns The element at the first position of the `_nodes` array.
|
|
113
113
|
*/
|
|
114
|
-
|
|
114
|
+
getFirst() {
|
|
115
115
|
if (this._size) return this._nodes[this._first];
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
* The `
|
|
119
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
120
120
|
* @returns The value that was removed from the data structure.
|
|
121
121
|
*/
|
|
122
|
-
|
|
122
|
+
popLast() {
|
|
123
123
|
if (!this._size) return;
|
|
124
|
-
const value = this.
|
|
124
|
+
const value = this.getLast();
|
|
125
125
|
delete this._nodes[this._last];
|
|
126
126
|
this._last--;
|
|
127
127
|
this._size--;
|
|
@@ -130,10 +130,10 @@ export class ObjectDeque<E = number> {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
|
-
* The `
|
|
133
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
134
134
|
* @returns The last element in the array "_nodes" is being returned.
|
|
135
135
|
*/
|
|
136
|
-
|
|
136
|
+
getLast() {
|
|
137
137
|
if (this._size) return this._nodes[this._last];
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -188,19 +188,19 @@ export class ArrayDeque<E> {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
/**
|
|
191
|
-
* The function "
|
|
192
|
-
* @returns The method `
|
|
191
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
192
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
193
193
|
*/
|
|
194
|
-
|
|
194
|
+
popLast(): E | null {
|
|
195
195
|
return this._nodes.pop() ?? null;
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
/**
|
|
199
|
-
* The `
|
|
200
|
-
* @returns The `
|
|
199
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
200
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
201
201
|
* empty.
|
|
202
202
|
*/
|
|
203
|
-
|
|
203
|
+
popFirst(): E | null {
|
|
204
204
|
return this._nodes.shift() ?? null;
|
|
205
205
|
}
|
|
206
206
|
|
|
@@ -219,19 +219,19 @@ export class ArrayDeque<E> {
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
|
-
* The `
|
|
223
|
-
* @returns The function `
|
|
222
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
223
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
224
224
|
* empty, it will return `null`.
|
|
225
225
|
*/
|
|
226
|
-
|
|
226
|
+
getFirst(): E | null {
|
|
227
227
|
return this._nodes[0] ?? null;
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
/**
|
|
231
|
-
* The `
|
|
232
|
-
* @returns The method `
|
|
231
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
232
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
233
233
|
*/
|
|
234
|
-
|
|
234
|
+
getLast(): E | null {
|
|
235
235
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
236
236
|
}
|
|
237
237
|
|
|
@@ -123,11 +123,11 @@ export class Queue<E = any> {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
|
-
* The `
|
|
127
|
-
* @returns The method `
|
|
126
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
127
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
128
128
|
* array is empty, it returns `null`.
|
|
129
129
|
*/
|
|
130
|
-
|
|
130
|
+
getLast(): E | undefined {
|
|
131
131
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult,
|
|
2
|
+
import {BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
|
-
createNode(key:
|
|
5
|
+
createNode(key: BTNKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
|
-
add(keyOrNode:
|
|
7
|
+
add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
delete<C extends
|
|
9
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {BSTNode} from '../../../data-structures';
|
|
2
|
-
import type {
|
|
2
|
+
import type {BTNKey, BinaryTreeOptions} from './binary-tree';
|
|
3
3
|
|
|
4
|
-
export type BSTComparator = (a:
|
|
4
|
+
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
5
5
|
|
|
6
6
|
// prettier-ignore
|
|
7
7
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
package/src/types/helpers.ts
CHANGED
|
@@ -71,10 +71,10 @@ describe('BinaryTreeNode', () => {
|
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
describe('BinaryTree', () => {
|
|
74
|
-
let tree: BinaryTree
|
|
74
|
+
let tree: BinaryTree<number>;
|
|
75
75
|
|
|
76
76
|
beforeEach(() => {
|
|
77
|
-
tree = new BinaryTree();
|
|
77
|
+
tree = new BinaryTree<number>();
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
afterEach(() => {
|
|
@@ -99,16 +99,18 @@ describe('BinaryTree', () => {
|
|
|
99
99
|
});
|
|
100
100
|
|
|
101
101
|
it('should add and find nodes', () => {
|
|
102
|
-
tree.add(1);
|
|
103
|
-
tree.add(2);
|
|
104
|
-
tree.add(3);
|
|
102
|
+
tree.add(1, 1);
|
|
103
|
+
tree.add(2, 2);
|
|
104
|
+
tree.add(3, 3);
|
|
105
105
|
|
|
106
106
|
expect(tree.has(1)).toBe(true);
|
|
107
107
|
expect(tree.has(2)).toBe(true);
|
|
108
108
|
expect(tree.has(3)).toBe(true);
|
|
109
109
|
expect(tree.has(4)).toBe(false);
|
|
110
110
|
const node4 = tree.get(4);
|
|
111
|
+
expect(tree.has(node4)).toBe(false);
|
|
111
112
|
expect(tree.has(node4, node => node)).toBe(false);
|
|
113
|
+
expect(tree.has('3', node => node.val?.toString())).toBe(true);
|
|
112
114
|
});
|
|
113
115
|
|
|
114
116
|
it('should getDepth return correct depth', () => {
|
|
@@ -125,11 +127,19 @@ describe('BinaryTree', () => {
|
|
|
125
127
|
});
|
|
126
128
|
|
|
127
129
|
it('should traverse in-order', () => {
|
|
130
|
+
tree.add(null);
|
|
131
|
+
tree.delete(1);
|
|
132
|
+
expect(tree.getHeight()).toBe(-1);
|
|
128
133
|
tree.add(4);
|
|
129
134
|
tree.add(2);
|
|
135
|
+
expect(tree.getHeight()).toBe(1);
|
|
136
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
137
|
+
expect(tree.getHeight()).toBe(1);
|
|
138
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
139
|
+
|
|
130
140
|
tree.add(6);
|
|
131
141
|
tree.add(1);
|
|
132
|
-
tree.add(3);
|
|
142
|
+
tree.add(new BinaryTreeNode(3));
|
|
133
143
|
tree.add(5);
|
|
134
144
|
tree.add(7);
|
|
135
145
|
|
|
@@ -160,6 +170,7 @@ describe('BinaryTree', () => {
|
|
|
160
170
|
]);
|
|
161
171
|
|
|
162
172
|
expect(tree.isSubtreeBST(tree.get(4), IterationType.RECURSIVE)).toBe(true);
|
|
173
|
+
expect(tree.isSubtreeBST(tree.get(4), IterationType.ITERATIVE)).toBe(true);
|
|
163
174
|
});
|
|
164
175
|
|
|
165
176
|
it('should subTreeTraverse', () => {
|
|
@@ -197,6 +208,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
197
208
|
|
|
198
209
|
expect(result).toEqual(expected);
|
|
199
210
|
expect(tree.dfs(node => node.key, 'in')).toEqual(expected);
|
|
211
|
+
expect(tree.dfs(node => node.key, 'in', tree.root, IterationType.RECURSIVE)).toEqual(expected);
|
|
200
212
|
});
|
|
201
213
|
|
|
202
214
|
it('should perform pre-order Morris traversal correctly as dfs traversal', () => {
|
|
@@ -255,8 +267,14 @@ describe('BinaryTree traversals', () => {
|
|
|
255
267
|
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
|
|
256
268
|
tree.refill(arr);
|
|
257
269
|
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
|
270
|
+
expect(tree.dfs(node => node.key, 'pre', tree.root, IterationType.RECURSIVE)).toEqual([
|
|
271
|
+
35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55
|
|
272
|
+
]);
|
|
258
273
|
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
|
259
274
|
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
|
|
275
|
+
expect(tree.dfs(node => node.key, 'post', tree.root, IterationType.RECURSIVE)).toEqual([
|
|
276
|
+
16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35
|
|
277
|
+
]);
|
|
260
278
|
expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([
|
|
261
279
|
35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55
|
|
262
280
|
]);
|
|
@@ -281,7 +299,7 @@ describe('BinaryTree', () => {
|
|
|
281
299
|
let tree: BinaryTree<string>;
|
|
282
300
|
|
|
283
301
|
beforeEach(() => {
|
|
284
|
-
tree = new BinaryTree<string>();
|
|
302
|
+
tree = new BinaryTree<string>({iterationType: IterationType.RECURSIVE});
|
|
285
303
|
});
|
|
286
304
|
|
|
287
305
|
afterEach(() => {
|
|
@@ -441,6 +459,14 @@ describe('BinaryTree', () => {
|
|
|
441
459
|
tree.add(3, 'B');
|
|
442
460
|
tree.add(7, 'C');
|
|
443
461
|
|
|
462
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
463
|
+
// @ts-ignore
|
|
464
|
+
expect([...tree]).toEqual([3, 5, 7]);
|
|
465
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
466
|
+
// @ts-ignore
|
|
467
|
+
expect([...tree]).toEqual([3, 5, 7]);
|
|
468
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
469
|
+
|
|
444
470
|
const result = tree.morris();
|
|
445
471
|
expect(result).toEqual([3, 5, 7]);
|
|
446
472
|
// Add assertions for the result of Morris traversal
|
|
@@ -52,3 +52,23 @@ describe('CoordinateMap', () => {
|
|
|
52
52
|
expect(retrievedValue).toBe(value);
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe('CoordinateMap', () => {
|
|
57
|
+
class MyCoordinateMap<V = any> extends CoordinateMap<V> {
|
|
58
|
+
constructor(joint?: string) {
|
|
59
|
+
super(joint);
|
|
60
|
+
this._setJoint((joint += '-'));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const cMap = new MyCoordinateMap<number>('*');
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
cMap.set([0, 0], 0);
|
|
68
|
+
cMap.set([0, 1], 1);
|
|
69
|
+
cMap.set([1, 1], 11);
|
|
70
|
+
});
|
|
71
|
+
it('should joint to be *-', () => {
|
|
72
|
+
expect(cMap.joint).toBe('*-');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -39,3 +39,28 @@ describe('CoordinateSet', () => {
|
|
|
39
39
|
expect(hasValue).toBe(true);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
+
|
|
43
|
+
describe('MyCoordinateSet', () => {
|
|
44
|
+
class MyCoordinateSet extends CoordinateSet {
|
|
45
|
+
constructor(joint?: string) {
|
|
46
|
+
super(joint);
|
|
47
|
+
this._setJoint((joint += '-'));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const mySet = new MyCoordinateSet('*');
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
mySet.add([0, 0]);
|
|
55
|
+
mySet.add([0, 1]);
|
|
56
|
+
mySet.add([1, 1]);
|
|
57
|
+
});
|
|
58
|
+
it('should joint to be *-', () => {
|
|
59
|
+
expect(mySet.joint).toBe('*-');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should has, delete', () => {
|
|
63
|
+
mySet.delete([0, 1]);
|
|
64
|
+
expect(mySet.has([0, 1])).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -15,7 +15,9 @@ describe('HashNode', () => {
|
|
|
15
15
|
describe('HashTable', () => {
|
|
16
16
|
it('should initialize with default capacity', () => {
|
|
17
17
|
const hashTable = new HashTable<string, string>();
|
|
18
|
-
|
|
18
|
+
hashTable.capacity = hashTable.capacity;
|
|
19
|
+
hashTable.buckets = hashTable.buckets;
|
|
20
|
+
hashTable.hashFn = hashTable.hashFn;
|
|
19
21
|
expect(hashTable.capacity).toBe(16);
|
|
20
22
|
expect(hashTable.size).toBe(0);
|
|
21
23
|
expect(hashTable.buckets.length).toBe(16);
|
|
@@ -1,6 +1,65 @@
|
|
|
1
|
-
import {DoublyLinkedList} from '../../../../src';
|
|
1
|
+
import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
|
+
describe('DoublyLinkedListNode', () => {
|
|
5
|
+
it('should DoublyLinkedListNode', () => {
|
|
6
|
+
const node1 = new DoublyLinkedListNode<number>(2);
|
|
7
|
+
expect(node1.val).toBe(2);
|
|
8
|
+
node1.val = 1;
|
|
9
|
+
expect(node1.val).toBe(1);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('DoublyLinkedList Operation Test', () => {
|
|
14
|
+
let list: DoublyLinkedList<number>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
list = DoublyLinkedList.fromArray([1, 2, 3, 4, 5]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should out of bound index', () => {
|
|
21
|
+
expect(list.getNodeAt(-1)).toBe(null);
|
|
22
|
+
expect(list.getNodeAt(5)).toBe(null);
|
|
23
|
+
expect(list.insertAt(5, 6)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should insertBefore', () => {
|
|
27
|
+
expect(list.insertBefore(1, 0)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should deleteAt', () => {
|
|
31
|
+
expect(list.deleteAt(1)).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should delete tail', () => {
|
|
35
|
+
expect(list.delete(list.tail)).toBe(true);
|
|
36
|
+
expect(list.tail?.val).toBe(4);
|
|
37
|
+
expect(list.delete(6)).toBe(false);
|
|
38
|
+
expect(list.tail?.val).toBe(4);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should find null', () => {
|
|
42
|
+
expect(list.find(val => val === 6)).toBe(null);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should indexOf -1', () => {
|
|
46
|
+
expect(list.indexOf(6)).toBe(-1);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should findBackward null', () => {
|
|
50
|
+
expect(list.findBackward(val => val === 0)).toBe(null);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should insertAfter tail', () => {
|
|
54
|
+
expect(list.insertAfter(list.tail!, 6)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should insertAfter tail', () => {
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
expect([...list]).toEqual([1, 2, 3, 4, 5]);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
4
63
|
describe('DoublyLinkedList Operation Test', () => {
|
|
5
64
|
let list: DoublyLinkedList<number>;
|
|
6
65
|
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
@@ -109,7 +168,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
109
168
|
list.reverse();
|
|
110
169
|
|
|
111
170
|
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
112
|
-
expect(list.
|
|
171
|
+
expect(list.toArrayBackward()).toEqual([1, 2, 3]);
|
|
113
172
|
});
|
|
114
173
|
|
|
115
174
|
it('should map elements using a callback function', () => {
|
|
@@ -189,7 +248,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
189
248
|
list.push(3);
|
|
190
249
|
list.push(4);
|
|
191
250
|
|
|
192
|
-
const lastEven = list.
|
|
251
|
+
const lastEven = list.findBackward(val => val % 2 === 0);
|
|
193
252
|
|
|
194
253
|
expect(lastEven).toBe(4);
|
|
195
254
|
});
|
|
@@ -211,7 +270,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
211
270
|
list.push(2);
|
|
212
271
|
list.push(3);
|
|
213
272
|
|
|
214
|
-
const reversedArray = list.
|
|
273
|
+
const reversedArray = list.toArrayBackward();
|
|
215
274
|
|
|
216
275
|
expect(reversedArray).toEqual([3, 2, 1]);
|
|
217
276
|
});
|
|
@@ -327,8 +386,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
327
386
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
328
387
|
expect(insertSuccess).toBe(true);
|
|
329
388
|
|
|
330
|
-
const
|
|
331
|
-
expect(
|
|
389
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
390
|
+
expect(getNode?.val).toEqual(newObj);
|
|
332
391
|
|
|
333
392
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
334
393
|
expect(deleted).toBe(true);
|
|
@@ -11,7 +11,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
11
11
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
12
12
|
doublyList.push(i);
|
|
13
13
|
if (i === midIndex) {
|
|
14
|
-
midNode = doublyList.
|
|
14
|
+
midNode = doublyList.getNode(i);
|
|
15
15
|
} else if (i > midIndex && midNode) {
|
|
16
16
|
doublyList.insertBefore(midNode, i);
|
|
17
17
|
}
|
|
@@ -24,7 +24,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
24
24
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
25
25
|
singlyList.push(i);
|
|
26
26
|
if (i === midIndex) {
|
|
27
|
-
midSinglyNode = singlyList.
|
|
27
|
+
midSinglyNode = singlyList.getNode(i);
|
|
28
28
|
} else if (i > midIndex && midSinglyNode) {
|
|
29
29
|
singlyList.insertBefore(midSinglyNode.val, i);
|
|
30
30
|
}
|