heap-typed 1.53.5 → 1.53.7
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 +136 -171
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
package/README.md
CHANGED
|
@@ -30,225 +30,190 @@ npm i heap-typed --save
|
|
|
30
30
|
yarn add heap-typed
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
### methods
|
|
34
|
-
|
|
35
|
-
Min Heap
|
|
36
|
-

|
|
37
|
-
Max Heap
|
|
38
|
-

|
|
39
|
-
|
|
40
33
|
### snippet
|
|
41
34
|
|
|
42
|
-
|
|
35
|
+
[//]: # (No deletion!!! Start of Example Replace Section)
|
|
43
36
|
|
|
37
|
+
### Use Heap to sort an array
|
|
44
38
|
```typescript
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// Use Heap to sort an array
|
|
50
|
-
function heapSort(arr: number[]): number[] {
|
|
51
|
-
const heap = new Heap<number>(arr, {comparator: (a, b) => a - b});
|
|
52
|
-
const sorted: number[] = [];
|
|
53
|
-
while (!heap.isEmpty()) {
|
|
39
|
+
function heapSort(arr: number[]): number[] {
|
|
40
|
+
const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
41
|
+
const sorted: number[] = [];
|
|
42
|
+
while (!heap.isEmpty()) {
|
|
54
43
|
sorted.push(heap.poll()!); // Poll minimum element
|
|
44
|
+
}
|
|
45
|
+
return sorted;
|
|
55
46
|
}
|
|
56
|
-
return sorted;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
console.log('Heap sorted:', heapSort([5, 3, 8, 4, 1, 2])); // [1, 2, 3, 4, 5, 8];
|
|
60
47
|
|
|
48
|
+
const array = [5, 3, 8, 4, 1, 2];
|
|
49
|
+
console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
61
50
|
```
|
|
62
51
|
|
|
63
|
-
|
|
64
|
-
#### top K problem TS
|
|
65
|
-
|
|
52
|
+
### Use Heap to solve top k problems
|
|
66
53
|
```typescript
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
arr.forEach((num) => {
|
|
54
|
+
function topKElements(arr: number[], k: number): number[] {
|
|
55
|
+
const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
56
|
+
arr.forEach(num => {
|
|
71
57
|
heap.add(num);
|
|
72
58
|
if (heap.size > k) heap.poll(); // Keep the heap size at K
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
59
|
+
});
|
|
60
|
+
return heap.toArray();
|
|
61
|
+
}
|
|
76
62
|
|
|
77
|
-
const numbers = [10, 30, 20, 5, 15, 25];
|
|
78
|
-
console.log(
|
|
63
|
+
const numbers = [10, 30, 20, 5, 15, 25];
|
|
64
|
+
console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
79
65
|
```
|
|
80
66
|
|
|
81
|
-
|
|
67
|
+
### Use Heap to merge sorted sequences
|
|
68
|
+
```typescript
|
|
69
|
+
function mergeSortedSequences(sequences: number[][]): number[] {
|
|
70
|
+
const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
71
|
+
comparator: (a, b) => a.value - b.value // Min heap
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Initialize heap
|
|
75
|
+
sequences.forEach((seq, seqIndex) => {
|
|
76
|
+
if (seq.length) {
|
|
77
|
+
heap.add({ value: seq[0], seqIndex, itemIndex: 0 });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const merged: number[] = [];
|
|
82
|
+
while (!heap.isEmpty()) {
|
|
83
|
+
const { value, seqIndex, itemIndex } = heap.poll()!;
|
|
84
|
+
merged.push(value);
|
|
85
|
+
|
|
86
|
+
if (itemIndex + 1 < sequences[seqIndex].length) {
|
|
87
|
+
heap.add({
|
|
88
|
+
value: sequences[seqIndex][itemIndex + 1],
|
|
89
|
+
seqIndex,
|
|
90
|
+
itemIndex: itemIndex + 1
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return merged;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const sequences = [
|
|
99
|
+
[1, 4, 7],
|
|
100
|
+
[2, 5, 8],
|
|
101
|
+
[3, 6, 9]
|
|
102
|
+
];
|
|
103
|
+
console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
104
|
+
```
|
|
82
105
|
|
|
106
|
+
### Use Heap to dynamically maintain the median
|
|
83
107
|
```typescript
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
private high: MinHeap<number>; // Min heap, stores the larger half
|
|
108
|
+
class MedianFinder {
|
|
109
|
+
private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
110
|
+
private high: MinHeap<number>; // Min heap, stores the larger half
|
|
88
111
|
|
|
89
|
-
|
|
112
|
+
constructor() {
|
|
90
113
|
this.low = new MaxHeap<number>([]);
|
|
91
114
|
this.high = new MinHeap<number>([]);
|
|
92
|
-
|
|
115
|
+
}
|
|
93
116
|
|
|
94
|
-
|
|
117
|
+
addNum(num: number): void {
|
|
95
118
|
if (this.low.isEmpty() || num <= this.low.peek()!) this.low.add(num);
|
|
96
119
|
else this.high.add(num);
|
|
97
120
|
|
|
98
121
|
// Balance heaps
|
|
99
122
|
if (this.low.size > this.high.size + 1) this.high.add(this.low.poll()!);
|
|
100
123
|
else if (this.high.size > this.low.size) this.low.add(this.high.poll()!);
|
|
101
|
-
|
|
124
|
+
}
|
|
102
125
|
|
|
103
|
-
|
|
126
|
+
findMedian(): number {
|
|
127
|
+
if (this.low.size === this.high.size) return (this.low.peek()! + this.high.peek()!) / 2;
|
|
104
128
|
return this.low.peek()!;
|
|
129
|
+
}
|
|
105
130
|
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const medianFinder = new MedianFinder();
|
|
109
|
-
medianFinder.addNum(10);
|
|
110
|
-
console.log('realtime median: ', medianFinder.findMedian()) // 10
|
|
111
|
-
medianFinder.addNum(20);
|
|
112
|
-
console.log('realtime median: ', medianFinder.findMedian()) // 10
|
|
113
|
-
medianFinder.addNum(30);
|
|
114
|
-
console.log('realtime median: ', medianFinder.findMedian()) // 20
|
|
115
|
-
medianFinder.addNum(40);
|
|
116
|
-
console.log('realtime median: ', medianFinder.findMedian()) // 20
|
|
117
|
-
medianFinder.addNum(50);
|
|
118
|
-
console.log('realtime median: ', medianFinder.findMedian()) // 30
|
|
119
|
-
```
|
|
120
131
|
|
|
121
|
-
|
|
132
|
+
const medianFinder = new MedianFinder();
|
|
133
|
+
medianFinder.addNum(10);
|
|
134
|
+
console.log(medianFinder.findMedian()); // 10
|
|
135
|
+
medianFinder.addNum(20);
|
|
136
|
+
console.log(medianFinder.findMedian()); // 15
|
|
137
|
+
medianFinder.addNum(30);
|
|
138
|
+
console.log(medianFinder.findMedian()); // 20
|
|
139
|
+
medianFinder.addNum(40);
|
|
140
|
+
console.log(medianFinder.findMedian()); // 25
|
|
141
|
+
medianFinder.addNum(50);
|
|
142
|
+
console.log(medianFinder.findMedian()); // 30
|
|
143
|
+
```
|
|
122
144
|
|
|
145
|
+
### Use Heap for load balancing
|
|
123
146
|
```typescript
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const serverLoads = new Array(servers).fill(0);
|
|
147
|
+
function loadBalance(requests: number[], servers: number): number[] {
|
|
148
|
+
const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
149
|
+
const serverLoads = new Array(servers).fill(0);
|
|
128
150
|
|
|
129
|
-
|
|
151
|
+
for (let i = 0; i < servers; i++) {
|
|
130
152
|
serverHeap.add({ id: i, load: 0 });
|
|
131
|
-
|
|
153
|
+
}
|
|
132
154
|
|
|
133
|
-
|
|
155
|
+
requests.forEach(req => {
|
|
134
156
|
const server = serverHeap.poll()!;
|
|
135
157
|
serverLoads[server.id] += req;
|
|
136
158
|
server.load += req;
|
|
137
159
|
serverHeap.add(server); // The server after updating the load is re-entered into the heap
|
|
138
|
-
|
|
160
|
+
});
|
|
139
161
|
|
|
140
|
-
|
|
141
|
-
}
|
|
162
|
+
return serverLoads;
|
|
163
|
+
}
|
|
142
164
|
|
|
143
|
-
const requests = [5, 2, 8, 3, 7];
|
|
144
|
-
|
|
145
|
-
console.log('server loads: ', serversLoads); // [12, 8, 5]
|
|
165
|
+
const requests = [5, 2, 8, 3, 7];
|
|
166
|
+
console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
146
167
|
```
|
|
147
|
-
#### conventional operation TS
|
|
148
168
|
|
|
169
|
+
### Use Heap to schedule tasks
|
|
149
170
|
```typescript
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
maxHeap.add(obj1);
|
|
173
|
-
maxHeap.has(obj1) // true
|
|
174
|
-
maxHeap.has(obj9) // false
|
|
175
|
-
maxHeap.add(obj6);
|
|
176
|
-
maxHeap.has(obj6) // true
|
|
177
|
-
maxHeap.add(obj5);
|
|
178
|
-
maxHeap.add(obj2);
|
|
179
|
-
maxHeap.add(obj0);
|
|
180
|
-
maxHeap.add(obj9);
|
|
181
|
-
maxHeap.has(obj9) // true
|
|
182
|
-
|
|
183
|
-
const peek9 = maxHeap.peek();
|
|
184
|
-
console.log(peek9.keyA) // 'a9'
|
|
185
|
-
|
|
186
|
-
const heapToArr = maxHeap.toArray();
|
|
187
|
-
console.log(heapToArr.map(ele => ele?.keyA)); // ['a9', 'a2', 'a6', 'a1', 'a0', 'a5']
|
|
188
|
-
|
|
189
|
-
const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0'];
|
|
190
|
-
let i = 0;
|
|
191
|
-
while (maxHeap.size > 0) {
|
|
192
|
-
const polled = maxHeap.poll();
|
|
193
|
-
console.log(polled.keyA) // values[i]
|
|
194
|
-
i++;
|
|
195
|
-
}
|
|
196
|
-
```
|
|
171
|
+
type Task = [string, number];
|
|
172
|
+
|
|
173
|
+
function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
174
|
+
const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
175
|
+
const allocation = new Map<number, Task[]>();
|
|
176
|
+
|
|
177
|
+
// Initialize the load on each machine
|
|
178
|
+
for (let i = 0; i < machines; i++) {
|
|
179
|
+
machineHeap.add({ id: i, load: 0 });
|
|
180
|
+
allocation.set(i, []);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Assign tasks
|
|
184
|
+
tasks.forEach(([task, load]) => {
|
|
185
|
+
const machine = machineHeap.poll()!;
|
|
186
|
+
allocation.get(machine.id)!.push([task, load]);
|
|
187
|
+
machine.load += load;
|
|
188
|
+
machineHeap.add(machine); // The machine after updating the load is re-entered into the heap
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return allocation;
|
|
192
|
+
}
|
|
197
193
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
arrFromHeap[0] // 2
|
|
217
|
-
arrFromHeap[1] // 5
|
|
218
|
-
arrFromHeap[2] // 9
|
|
219
|
-
arrFromHeap[3] // 6
|
|
220
|
-
minNumHeap.sort() // [2, 5, 6, 9]
|
|
221
|
-
|
|
222
|
-
const maxHeap = new MaxHeap([], {comparator: (a, b) => b.keyA - a.keyA});
|
|
223
|
-
const obj1 = {keyA: 'a1'}, obj6 = {keyA: 'a6'}, obj5 = {keyA: 'a5'}, obj2 = {keyA: 'a2'},
|
|
224
|
-
obj0 = {keyA: 'a0'}, obj9 = {keyA: 'a9'};
|
|
225
|
-
|
|
226
|
-
maxHeap.add(obj1);
|
|
227
|
-
maxHeap.has(obj1) // true
|
|
228
|
-
maxHeap.has(obj9) // false
|
|
229
|
-
maxHeap.add(obj6);
|
|
230
|
-
maxHeap.has(obj6) // true
|
|
231
|
-
maxHeap.add(obj5);
|
|
232
|
-
maxHeap.add(obj2);
|
|
233
|
-
maxHeap.add(obj0);
|
|
234
|
-
maxHeap.add(obj9);
|
|
235
|
-
maxHeap.has(obj9) // true
|
|
236
|
-
|
|
237
|
-
const peek9 = maxHeap.peek();
|
|
238
|
-
console.log(peek9.keyA) // 'a9'
|
|
239
|
-
|
|
240
|
-
const heapToArr = maxHeap.toArray();
|
|
241
|
-
console.log(heapToArr.map(ele => ele?.keyA)); // ['a9', 'a2', 'a6', 'a1', 'a0', 'a5']
|
|
242
|
-
|
|
243
|
-
const values = ['a9', 'a6', 'a5', 'a2', 'a1', 'a0'];
|
|
244
|
-
let i = 0;
|
|
245
|
-
while (maxHeap.size > 0) {
|
|
246
|
-
const polled = maxHeap.poll();
|
|
247
|
-
console.log(polled.keyA) // values[i]
|
|
248
|
-
i++;
|
|
249
|
-
}
|
|
194
|
+
const tasks: Task[] = [
|
|
195
|
+
['Task1', 3],
|
|
196
|
+
['Task2', 1],
|
|
197
|
+
['Task3', 2],
|
|
198
|
+
['Task4', 5],
|
|
199
|
+
['Task5', 4]
|
|
200
|
+
];
|
|
201
|
+
const expectedMap = new Map<number, Task[]>();
|
|
202
|
+
expectedMap.set(0, [
|
|
203
|
+
['Task1', 3],
|
|
204
|
+
['Task4', 5]
|
|
205
|
+
]);
|
|
206
|
+
expectedMap.set(1, [
|
|
207
|
+
['Task2', 1],
|
|
208
|
+
['Task3', 2],
|
|
209
|
+
['Task5', 4]
|
|
210
|
+
]);
|
|
211
|
+
console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
250
212
|
```
|
|
251
213
|
|
|
214
|
+
[//]: # (No deletion!!! End of Example Replace Section)
|
|
215
|
+
|
|
216
|
+
|
|
252
217
|
|
|
253
218
|
## API docs & Examples
|
|
254
219
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare enum DFSOperation {
|
|
2
|
+
VISIT = 0,
|
|
3
|
+
PROCESS = 1
|
|
4
|
+
}
|
|
5
|
+
export declare class Range<K> {
|
|
6
|
+
low: K;
|
|
7
|
+
high: K;
|
|
8
|
+
includeLow: boolean;
|
|
9
|
+
includeHigh: boolean;
|
|
10
|
+
constructor(low: K, high: K, includeLow?: boolean, includeHigh?: boolean);
|
|
11
|
+
isInRange(key: K, comparator: (a: K, b: K) => number): boolean;
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Range = exports.DFSOperation = void 0;
|
|
4
|
+
var DFSOperation;
|
|
5
|
+
(function (DFSOperation) {
|
|
6
|
+
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
7
|
+
DFSOperation[DFSOperation["PROCESS"] = 1] = "PROCESS";
|
|
8
|
+
})(DFSOperation = exports.DFSOperation || (exports.DFSOperation = {}));
|
|
9
|
+
class Range {
|
|
10
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
11
|
+
this.low = low;
|
|
12
|
+
this.high = high;
|
|
13
|
+
this.includeLow = includeLow;
|
|
14
|
+
this.includeHigh = includeHigh;
|
|
15
|
+
}
|
|
16
|
+
// Determine whether a key is within the range
|
|
17
|
+
isInRange(key, comparator) {
|
|
18
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
19
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
20
|
+
return lowCheck && highCheck;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Range = Range;
|
|
@@ -96,7 +96,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
96
96
|
* object.
|
|
97
97
|
*/
|
|
98
98
|
createTree(options) {
|
|
99
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
99
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
102
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
@@ -132,17 +132,14 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
132
132
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
133
133
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
134
134
|
}
|
|
135
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
136
|
-
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
137
135
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
143
|
-
}
|
|
144
|
-
return [undefined, undefined];
|
|
136
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
137
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
138
|
+
if (this.isKey(key))
|
|
139
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
145
140
|
}
|
|
141
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
142
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
146
143
|
return [undefined, undefined];
|
|
147
144
|
}
|
|
148
145
|
/**
|
|
@@ -85,7 +85,7 @@ class AVLTree extends bst_1.BST {
|
|
|
85
85
|
* @returns a new AVLTree object.
|
|
86
86
|
*/
|
|
87
87
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
88
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
@@ -409,7 +409,7 @@ class AVLTree extends bst_1.BST {
|
|
|
409
409
|
*/
|
|
410
410
|
_balancePath(node) {
|
|
411
411
|
node = this.ensureNode(node);
|
|
412
|
-
const path = this.getPathToRoot(node => node,
|
|
412
|
+
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
413
413
|
for (let i = 0; i < path.length; i++) {
|
|
414
414
|
// second O(log n)
|
|
415
415
|
const A = path[i];
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, ToEntryFn } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
|
+
import { Range } from '../../common';
|
|
11
12
|
/**
|
|
12
13
|
* Represents a node in a binary tree.
|
|
13
14
|
* @template V - The type of data stored in the node.
|
|
@@ -120,6 +121,13 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
120
121
|
* is not a node.
|
|
121
122
|
*/
|
|
122
123
|
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
124
|
+
/**
|
|
125
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
126
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
127
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
128
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
129
|
+
* indicating that it is of type `R`.
|
|
130
|
+
*/
|
|
123
131
|
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R;
|
|
124
132
|
/**
|
|
125
133
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -150,6 +158,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
150
158
|
* property of the current object and returning a boolean value based on that comparison.
|
|
151
159
|
*/
|
|
152
160
|
isNIL(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): boolean;
|
|
161
|
+
isRange(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>): keyNodeEntryRawOrPredicate is Range<K>;
|
|
153
162
|
/**
|
|
154
163
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
155
164
|
* tree.
|
|
@@ -221,6 +230,15 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
221
230
|
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
222
231
|
*/
|
|
223
232
|
addMany(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE> | R>, values?: Iterable<V | undefined>): boolean[];
|
|
233
|
+
/**
|
|
234
|
+
* Time Complexity: O(k * n)
|
|
235
|
+
* Space Complexity: O(1)
|
|
236
|
+
*
|
|
237
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
238
|
+
* elements from the other tree.
|
|
239
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
240
|
+
*/
|
|
241
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>): void;
|
|
224
242
|
/**
|
|
225
243
|
* Time Complexity: O(k * n)
|
|
226
244
|
* Space Complexity: O(1)
|
|
@@ -250,6 +268,31 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
250
268
|
* need to be balanced (`needBalanced`).
|
|
251
269
|
*/
|
|
252
270
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
271
|
+
/**
|
|
272
|
+
* Time Complexity: O(n)
|
|
273
|
+
* Space Complexity: O(k + log n)
|
|
274
|
+
*
|
|
275
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
276
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
277
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
278
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
279
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
280
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
281
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
282
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
283
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
284
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
285
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
286
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
287
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
288
|
+
* provided, the search operation will start from the root
|
|
289
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
290
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
291
|
+
* two possible values:
|
|
292
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
293
|
+
* on the search algorithm implemented within the function.
|
|
294
|
+
*/
|
|
295
|
+
search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
|
|
253
296
|
/**
|
|
254
297
|
* Time Complexity: O(n)
|
|
255
298
|
* Space Complexity: O(k + log n)
|
|
@@ -293,20 +336,6 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
293
336
|
* or `null` if no matching node is found.
|
|
294
337
|
*/
|
|
295
338
|
getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): OptNodeOrNull<NODE>;
|
|
296
|
-
/**
|
|
297
|
-
* Time Complexity: O(n)
|
|
298
|
-
* Space Complexity: O(log n)
|
|
299
|
-
*
|
|
300
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
301
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
302
|
-
* structure.
|
|
303
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
304
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
305
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
306
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
307
|
-
* (`OptNodeOrNull<NODE>`).
|
|
308
|
-
*/
|
|
309
|
-
getNodeByKey(key: K, iterationType?: IterationType): OptNodeOrNull<NODE>;
|
|
310
339
|
/**
|
|
311
340
|
* Time Complexity: O(n)
|
|
312
341
|
* Space Complexity: O(log n)
|
|
@@ -478,7 +507,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
478
507
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
479
508
|
* parameter.
|
|
480
509
|
*/
|
|
481
|
-
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(
|
|
510
|
+
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(beginNode: BTNRep<K, V, NODE> | R, callback?: C, isReverse?: boolean): ReturnType<C>[];
|
|
482
511
|
/**
|
|
483
512
|
* Time Complexity: O(log n)
|
|
484
513
|
* Space Complexity: O(1)
|
|
@@ -825,16 +854,16 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
825
854
|
* Time Complexity: O(1)
|
|
826
855
|
* Space Complexity: O(1)
|
|
827
856
|
*
|
|
828
|
-
* The function `
|
|
857
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
829
858
|
* entry, raw data, or null/undefined.
|
|
830
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
859
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
831
860
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
832
861
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
833
|
-
* @returns The `
|
|
862
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
834
863
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
835
864
|
* the conditions checked in the method.
|
|
836
865
|
*/
|
|
837
|
-
protected
|
|
866
|
+
protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined;
|
|
838
867
|
/**
|
|
839
868
|
* Time Complexity: O(1)
|
|
840
869
|
* Space Complexity: O(1)
|
|
@@ -851,10 +880,16 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
851
880
|
*/
|
|
852
881
|
protected _setValue(key: K | null | undefined, value: V | undefined): false | Map<K, V | undefined>;
|
|
853
882
|
/**
|
|
883
|
+
* Time Complexity: O(1)
|
|
884
|
+
* Space Complexity: O(1)
|
|
885
|
+
*
|
|
854
886
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
855
887
|
*/
|
|
856
888
|
protected _clearNodes(): void;
|
|
857
889
|
/**
|
|
890
|
+
* Time Complexity: O(1)
|
|
891
|
+
* Space Complexity: O(1)
|
|
892
|
+
*
|
|
858
893
|
* The _clearValues function clears all values stored in the _store object.
|
|
859
894
|
*/
|
|
860
895
|
protected _clearValues(): void;
|