min-heap-typed 1.39.1 → 1.39.2
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/dist/data-structures/binary-tree/binary-tree.d.ts +11 -1
- package/dist/data-structures/binary-tree/binary-tree.js +38 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +47 -5
- package/src/data-structures/binary-tree/bst.ts +1 -2
- package/src/data-structures/binary-tree/rb-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -2
- package/src/data-structures/graph/abstract-graph.ts +10 -11
- package/src/data-structures/graph/directed-graph.ts +1 -2
- package/src/data-structures/graph/undirected-graph.ts +4 -5
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +119 -14
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +27 -26
- package/src/data-structures/queue/queue.ts +4 -4
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -69,16 +69,13 @@ class SinglyLinkedList {
|
|
|
69
69
|
}
|
|
70
70
|
return singlyLinkedList;
|
|
71
71
|
}
|
|
72
|
-
getLength() {
|
|
73
|
-
return this._length;
|
|
74
|
-
}
|
|
75
72
|
/**
|
|
76
|
-
* The `push` function adds a new node with the given
|
|
77
|
-
* @param {E}
|
|
73
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
74
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
78
75
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
79
76
|
*/
|
|
80
|
-
push(
|
|
81
|
-
const newNode = new SinglyLinkedListNode(
|
|
77
|
+
push(val) {
|
|
78
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
82
79
|
if (!this.head) {
|
|
83
80
|
this.head = newNode;
|
|
84
81
|
this.tail = newNode;
|
|
@@ -89,6 +86,14 @@ class SinglyLinkedList {
|
|
|
89
86
|
}
|
|
90
87
|
this._length++;
|
|
91
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
91
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
92
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
93
|
+
*/
|
|
94
|
+
addLast(val) {
|
|
95
|
+
this.push(val);
|
|
96
|
+
}
|
|
92
97
|
/**
|
|
93
98
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
94
99
|
* pointers accordingly.
|
|
@@ -115,6 +120,15 @@ class SinglyLinkedList {
|
|
|
115
120
|
this._length--;
|
|
116
121
|
return val;
|
|
117
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
125
|
+
* pointers accordingly.
|
|
126
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
127
|
+
* the linked list is empty, it returns `null`.
|
|
128
|
+
*/
|
|
129
|
+
popLast() {
|
|
130
|
+
return this.pop();
|
|
131
|
+
}
|
|
118
132
|
/**
|
|
119
133
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
120
134
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -127,6 +141,13 @@ class SinglyLinkedList {
|
|
|
127
141
|
this._length--;
|
|
128
142
|
return removedNode.val;
|
|
129
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
146
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
147
|
+
*/
|
|
148
|
+
popFirst() {
|
|
149
|
+
return this.shift();
|
|
150
|
+
}
|
|
130
151
|
/**
|
|
131
152
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
132
153
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -144,6 +165,14 @@ class SinglyLinkedList {
|
|
|
144
165
|
}
|
|
145
166
|
this._length++;
|
|
146
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
170
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
171
|
+
* linked list.
|
|
172
|
+
*/
|
|
173
|
+
addFirst(val) {
|
|
174
|
+
this.unshift(val);
|
|
175
|
+
}
|
|
147
176
|
/**
|
|
148
177
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
149
178
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -350,7 +379,7 @@ class SinglyLinkedList {
|
|
|
350
379
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
351
380
|
* the specified value is found, the function returns `null`.
|
|
352
381
|
*/
|
|
353
|
-
|
|
382
|
+
getNode(value) {
|
|
354
383
|
let current = this.head;
|
|
355
384
|
while (current) {
|
|
356
385
|
if (current.val === value) {
|
|
@@ -409,7 +438,7 @@ class SinglyLinkedList {
|
|
|
409
438
|
existingNode = existingValueOrNode;
|
|
410
439
|
}
|
|
411
440
|
else {
|
|
412
|
-
existingNode = this.
|
|
441
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
413
442
|
}
|
|
414
443
|
if (existingNode) {
|
|
415
444
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
@@ -439,6 +468,78 @@ class SinglyLinkedList {
|
|
|
439
468
|
}
|
|
440
469
|
return count;
|
|
441
470
|
}
|
|
471
|
+
/**
|
|
472
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
473
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
474
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
475
|
+
* current node in the linked list.
|
|
476
|
+
*/
|
|
477
|
+
forEach(callback) {
|
|
478
|
+
let current = this.head;
|
|
479
|
+
let index = 0;
|
|
480
|
+
while (current) {
|
|
481
|
+
callback(current.val, index);
|
|
482
|
+
current = current.next;
|
|
483
|
+
index++;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
488
|
+
* SinglyLinkedList with the transformed values.
|
|
489
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
490
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
491
|
+
* SinglyLinkedList).
|
|
492
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
493
|
+
*/
|
|
494
|
+
map(callback) {
|
|
495
|
+
const mappedList = new SinglyLinkedList();
|
|
496
|
+
let current = this.head;
|
|
497
|
+
while (current) {
|
|
498
|
+
mappedList.push(callback(current.val));
|
|
499
|
+
current = current.next;
|
|
500
|
+
}
|
|
501
|
+
return mappedList;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
505
|
+
* elements that satisfy the given callback function.
|
|
506
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
507
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
508
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
509
|
+
*/
|
|
510
|
+
filter(callback) {
|
|
511
|
+
const filteredList = new SinglyLinkedList();
|
|
512
|
+
let current = this.head;
|
|
513
|
+
while (current) {
|
|
514
|
+
if (callback(current.val)) {
|
|
515
|
+
filteredList.push(current.val);
|
|
516
|
+
}
|
|
517
|
+
current = current.next;
|
|
518
|
+
}
|
|
519
|
+
return filteredList;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
523
|
+
* single value.
|
|
524
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
525
|
+
* used to perform a specific operation on each element of the linked list.
|
|
526
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
527
|
+
* point for the reduction operation.
|
|
528
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
529
|
+
* elements in the linked list.
|
|
530
|
+
*/
|
|
531
|
+
reduce(callback, initialValue) {
|
|
532
|
+
let accumulator = initialValue;
|
|
533
|
+
let current = this.head;
|
|
534
|
+
while (current) {
|
|
535
|
+
accumulator = callback(accumulator, current.val);
|
|
536
|
+
current = current.next;
|
|
537
|
+
}
|
|
538
|
+
return accumulator;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
542
|
+
*/
|
|
442
543
|
*[Symbol.iterator]() {
|
|
443
544
|
let current = this.head;
|
|
444
545
|
while (current) {
|
|
@@ -37,25 +37,25 @@ export declare class ObjectDeque<E = number> {
|
|
|
37
37
|
*/
|
|
38
38
|
addLast(value: E): void;
|
|
39
39
|
/**
|
|
40
|
-
* The function `
|
|
40
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
41
41
|
* @returns The value of the first element in the data structure.
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
popFirst(): E | undefined;
|
|
44
44
|
/**
|
|
45
|
-
* The `
|
|
45
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
46
46
|
* @returns The element at the first position of the `_nodes` array.
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
getFirst(): E | undefined;
|
|
49
49
|
/**
|
|
50
|
-
* The `
|
|
50
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
51
51
|
* @returns The value that was removed from the data structure.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
popLast(): E | undefined;
|
|
54
54
|
/**
|
|
55
|
-
* The `
|
|
55
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
56
56
|
* @returns The last element in the array "_nodes" is being returned.
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
getLast(): E | undefined;
|
|
59
59
|
/**
|
|
60
60
|
* The get function returns the element at the specified index in an array-like data structure.
|
|
61
61
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
@@ -87,16 +87,16 @@ export declare class ArrayDeque<E> {
|
|
|
87
87
|
*/
|
|
88
88
|
addLast(value: E): number;
|
|
89
89
|
/**
|
|
90
|
-
* The function "
|
|
91
|
-
* @returns The method `
|
|
90
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
91
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
popLast(): E | null;
|
|
94
94
|
/**
|
|
95
|
-
* The `
|
|
96
|
-
* @returns The `
|
|
95
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
96
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
97
97
|
* empty.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
popFirst(): E | null;
|
|
100
100
|
/**
|
|
101
101
|
* O(n) time complexity of adding at the beginning and the end
|
|
102
102
|
*/
|
|
@@ -108,16 +108,16 @@ export declare class ArrayDeque<E> {
|
|
|
108
108
|
*/
|
|
109
109
|
addFirst(value: E): number;
|
|
110
110
|
/**
|
|
111
|
-
* The `
|
|
112
|
-
* @returns The function `
|
|
111
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
112
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
113
113
|
* empty, it will return `null`.
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
getFirst(): E | null;
|
|
116
116
|
/**
|
|
117
|
-
* The `
|
|
118
|
-
* @returns The method `
|
|
117
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
118
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
119
119
|
*/
|
|
120
|
-
|
|
120
|
+
getLast(): E | null;
|
|
121
121
|
/**
|
|
122
122
|
* O(1) time complexity of obtaining the value
|
|
123
123
|
*/
|
|
@@ -85,44 +85,44 @@ class ObjectDeque {
|
|
|
85
85
|
this._size++;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
* The function `
|
|
88
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
89
89
|
* @returns The value of the first element in the data structure.
|
|
90
90
|
*/
|
|
91
|
-
|
|
91
|
+
popFirst() {
|
|
92
92
|
if (!this._size)
|
|
93
93
|
return;
|
|
94
|
-
const value = this.
|
|
94
|
+
const value = this.getFirst();
|
|
95
95
|
delete this._nodes[this._first];
|
|
96
96
|
this._first++;
|
|
97
97
|
this._size--;
|
|
98
98
|
return value;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
* The `
|
|
101
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
102
102
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
getFirst() {
|
|
105
105
|
if (this._size)
|
|
106
106
|
return this._nodes[this._first];
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* The `
|
|
109
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
110
|
* @returns The value that was removed from the data structure.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
popLast() {
|
|
113
113
|
if (!this._size)
|
|
114
114
|
return;
|
|
115
|
-
const value = this.
|
|
115
|
+
const value = this.getLast();
|
|
116
116
|
delete this._nodes[this._last];
|
|
117
117
|
this._last--;
|
|
118
118
|
this._size--;
|
|
119
119
|
return value;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
|
-
* The `
|
|
122
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
123
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
getLast() {
|
|
126
126
|
if (this._size)
|
|
127
127
|
return this._nodes[this._last];
|
|
128
128
|
}
|
|
@@ -172,19 +172,19 @@ class ArrayDeque {
|
|
|
172
172
|
return this._nodes.push(value);
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
|
-
* The function "
|
|
176
|
-
* @returns The method `
|
|
175
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
176
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
177
177
|
*/
|
|
178
|
-
|
|
178
|
+
popLast() {
|
|
179
179
|
var _a;
|
|
180
180
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
|
-
* The `
|
|
184
|
-
* @returns The `
|
|
183
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
184
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
185
185
|
* empty.
|
|
186
186
|
*/
|
|
187
|
-
|
|
187
|
+
popFirst() {
|
|
188
188
|
var _a;
|
|
189
189
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
190
190
|
}
|
|
@@ -201,19 +201,19 @@ class ArrayDeque {
|
|
|
201
201
|
return this._nodes.unshift(value);
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
|
-
* The `
|
|
205
|
-
* @returns The function `
|
|
204
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
205
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
206
206
|
* empty, it will return `null`.
|
|
207
207
|
*/
|
|
208
|
-
|
|
208
|
+
getFirst() {
|
|
209
209
|
var _a;
|
|
210
210
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
|
-
* The `
|
|
214
|
-
* @returns The method `
|
|
213
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
214
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
215
215
|
*/
|
|
216
|
-
|
|
216
|
+
getLast() {
|
|
217
217
|
var _a;
|
|
218
218
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
219
219
|
}
|
|
@@ -68,11 +68,11 @@ export declare class Queue<E = any> {
|
|
|
68
68
|
*/
|
|
69
69
|
peek(): E | undefined;
|
|
70
70
|
/**
|
|
71
|
-
* The `
|
|
72
|
-
* @returns The method `
|
|
71
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
72
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
73
73
|
* array is empty, it returns `null`.
|
|
74
74
|
*/
|
|
75
|
-
|
|
75
|
+
getLast(): E | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* The enqueue function adds a value to the end of a queue.
|
|
78
78
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -109,11 +109,11 @@ class Queue {
|
|
|
109
109
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* The `
|
|
113
|
-
* @returns The method `
|
|
112
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
113
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
114
114
|
* array is empty, it returns `null`.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
getLast() {
|
|
117
117
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
118
118
|
}
|
|
119
119
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.2",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.39.
|
|
134
|
+
"data-structure-typed": "^1.39.2"
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -21,8 +21,7 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
26
25
|
/**
|
|
27
26
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
28
27
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -161,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
161
160
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
162
161
|
switch (
|
|
163
162
|
this._balanceFactor(A) // second O(1)
|
|
164
|
-
|
|
163
|
+
) {
|
|
165
164
|
case -2:
|
|
166
165
|
if (A && A.left) {
|
|
167
166
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
20
|
+
constructor({frequency = 0, max}: { frequency?: number; max: number }) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback} from '../../types';
|
|
10
10
|
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
import {trampoline} from '../../utils';
|
|
@@ -108,8 +108,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
110
|
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
-
implements IBinaryTree<V, N>
|
|
112
|
-
{
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
113
112
|
/**
|
|
114
113
|
* Creates a new instance of BinaryTree.
|
|
115
114
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -395,7 +394,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
395
394
|
return -1;
|
|
396
395
|
}
|
|
397
396
|
|
|
398
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
397
|
+
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
|
|
399
398
|
let maxHeight = 0;
|
|
400
399
|
|
|
401
400
|
while (stack.length > 0) {
|
|
@@ -832,7 +831,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
832
831
|
_traverse(beginRoot);
|
|
833
832
|
} else {
|
|
834
833
|
// 0: visit, 1: print
|
|
835
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
834
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
|
|
836
835
|
|
|
837
836
|
while (stack.length > 0) {
|
|
838
837
|
const cur = stack.pop();
|
|
@@ -1093,6 +1092,49 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1093
1092
|
return ans;
|
|
1094
1093
|
}
|
|
1095
1094
|
|
|
1095
|
+
/**
|
|
1096
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1097
|
+
* either an iterative or recursive manner.
|
|
1098
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1099
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1100
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1101
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1102
|
+
* binary tree nodes in a specific order.
|
|
1103
|
+
*/
|
|
1104
|
+
* [Symbol.iterator](node = this.root): Generator<BinaryTreeNodeKey, void, undefined> {
|
|
1105
|
+
if (!node) {
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1110
|
+
const stack: (N | null | undefined)[] = [];
|
|
1111
|
+
let current: N | null | undefined = node;
|
|
1112
|
+
|
|
1113
|
+
while (current || stack.length > 0) {
|
|
1114
|
+
while (current) {
|
|
1115
|
+
stack.push(current);
|
|
1116
|
+
current = current.left;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
current = stack.pop();
|
|
1120
|
+
|
|
1121
|
+
if (current) yield current.key;
|
|
1122
|
+
if (current) current = current.right;
|
|
1123
|
+
}
|
|
1124
|
+
} else {
|
|
1125
|
+
|
|
1126
|
+
if (node.left) {
|
|
1127
|
+
yield* this[Symbol.iterator](node.left);
|
|
1128
|
+
}
|
|
1129
|
+
yield node.key;
|
|
1130
|
+
if (node.right) {
|
|
1131
|
+
yield* this[Symbol.iterator](node.right);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
|
|
1096
1138
|
/**
|
|
1097
1139
|
* Swap the data of two nodes in the binary tree.
|
|
1098
1140
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -19,8 +19,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
19
19
|
|
|
20
20
|
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
21
21
|
extends BinaryTree<V, N>
|
|
22
|
-
implements IBinaryTree<V, N>
|
|
23
|
-
{
|
|
22
|
+
implements IBinaryTree<V, N> {
|
|
24
23
|
/**
|
|
25
24
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
26
25
|
* function.
|
|
@@ -21,8 +21,7 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
|
|
|
21
21
|
|
|
22
22
|
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
25
|
-
{
|
|
24
|
+
implements IBinaryTree<V, N> {
|
|
26
25
|
constructor(options?: RBTreeOptions) {
|
|
27
26
|
super(options);
|
|
28
27
|
}
|
|
@@ -37,8 +37,7 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
41
|
-
{
|
|
40
|
+
implements IBinaryTree<V, N> {
|
|
42
41
|
/**
|
|
43
42
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
44
43
|
* merge duplicated values.
|
|
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
|
|
|
105
105
|
export abstract class AbstractGraph<
|
|
106
106
|
V extends AbstractVertex<any> = AbstractVertex<any>,
|
|
107
107
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
108
|
-
> implements IGraph<V, E>
|
|
109
|
-
{
|
|
108
|
+
> implements IGraph<V, E> {
|
|
110
109
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
111
110
|
|
|
112
111
|
get vertices(): Map<VertexKey, V> {
|
|
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
|
|
|
554
553
|
}
|
|
555
554
|
|
|
556
555
|
getMinDist &&
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
}
|
|
556
|
+
distMap.forEach((d, v) => {
|
|
557
|
+
if (v !== srcVertex) {
|
|
558
|
+
if (d < minDist) {
|
|
559
|
+
minDist = d;
|
|
560
|
+
if (genPaths) minDest = v;
|
|
563
561
|
}
|
|
564
|
-
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
565
564
|
|
|
566
565
|
genPaths && getPaths(minDest);
|
|
567
566
|
|
|
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
|
|
|
623
622
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
624
623
|
}
|
|
625
624
|
|
|
626
|
-
const heap = new PriorityQueue<{key: number; val: V}>({comparator: (a, b) => a.key - b.key});
|
|
625
|
+
const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key});
|
|
627
626
|
heap.add({key: 0, val: srcVertex});
|
|
628
627
|
|
|
629
628
|
distMap.set(srcVertex, 0);
|
|
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
|
|
|
852
851
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
853
852
|
* path between vertices in the
|
|
854
853
|
*/
|
|
855
|
-
floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
|
|
854
|
+
floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
|
|
856
855
|
const idAndVertices = [...this._vertices];
|
|
857
856
|
const n = idAndVertices.length;
|
|
858
857
|
|
|
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
64
64
|
|
|
65
65
|
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
|
|
66
66
|
extends AbstractGraph<V, E>
|
|
67
|
-
implements IGraph<V, E>
|
|
68
|
-
{
|
|
67
|
+
implements IGraph<V, E> {
|
|
69
68
|
/**
|
|
70
69
|
* The constructor function initializes an instance of a class.
|
|
71
70
|
*/
|
|
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class UndirectedGraph<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
V extends UndirectedVertex<any> = UndirectedVertex,
|
|
55
|
+
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
|
+
>
|
|
57
57
|
extends AbstractGraph<V, E>
|
|
58
|
-
implements IGraph<V, E>
|
|
59
|
-
{
|
|
58
|
+
implements IGraph<V, E> {
|
|
60
59
|
/**
|
|
61
60
|
* The constructor initializes a new Map object to store edges.
|
|
62
61
|
*/
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeMap {
|
|
1
|
+
export class TreeMap {
|
|
2
|
+
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export class TreeSet {
|
|
1
|
+
export class TreeSet {
|
|
2
|
+
}
|