linked-list-typed 1.45.0 → 1.45.1
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/hash/hash-map.d.ts +58 -58
- package/dist/data-structures/hash/hash-map.js +73 -73
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +103 -103
- package/src/data-structures/hash/hash-table.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 +8 -5
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- 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/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +6 -6
- 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 +18 -4
- package/src/utils/utils.ts +6 -6
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode, HashMapOptions, IterateDirection } from
|
|
8
|
+
import { HashMapLinkedNode, HashMapOptions, IterateDirection } from '../../types';
|
|
9
9
|
/**
|
|
10
10
|
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
11
11
|
* these underlying structures have already dealt with dynamic expansion and hash collisions.
|
|
@@ -13,8 +13,8 @@ import { HashMapLinkedNode, HashMapOptions, IterateDirection } from "../../types
|
|
|
13
13
|
*/
|
|
14
14
|
export declare class HashMapIterator<K, V> {
|
|
15
15
|
readonly hashMap: HashMap<K, V>;
|
|
16
|
-
protected _node: HashMapLinkedNode<K, V>;
|
|
17
16
|
readonly iterateDirection: IterateDirection;
|
|
17
|
+
protected _node: HashMapLinkedNode<K, V>;
|
|
18
18
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
19
19
|
/**
|
|
20
20
|
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
@@ -47,14 +47,12 @@ export declare class HashMapIterator<K, V> {
|
|
|
47
47
|
next(): this;
|
|
48
48
|
}
|
|
49
49
|
export declare class HashMap<K = any, V = any> {
|
|
50
|
+
readonly OBJ_KEY_INDEX: symbol;
|
|
50
51
|
protected _nodes: HashMapLinkedNode<K, V>[];
|
|
51
52
|
protected _orgMap: Record<string, HashMapLinkedNode<K, V>>;
|
|
52
53
|
protected _head: HashMapLinkedNode<K, V>;
|
|
53
54
|
protected _tail: HashMapLinkedNode<K, V>;
|
|
54
55
|
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
55
|
-
readonly OBJ_KEY_INDEX: symbol;
|
|
56
|
-
protected _size: number;
|
|
57
|
-
get size(): number;
|
|
58
56
|
/**
|
|
59
57
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
60
58
|
* @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
@@ -62,6 +60,61 @@ export declare class HashMap<K = any, V = any> {
|
|
|
62
60
|
* `K` represents the type of the key and `V` represents the
|
|
63
61
|
*/
|
|
64
62
|
constructor(hashMap?: HashMapOptions<[K, V]>);
|
|
63
|
+
protected _size: number;
|
|
64
|
+
get size(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(1)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
69
|
+
* The function returns a new iterator object for a HashMap.
|
|
70
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
71
|
+
*/
|
|
72
|
+
get begin(): HashMapIterator<K, V>;
|
|
73
|
+
/**
|
|
74
|
+
* Time Complexity: O(1)
|
|
75
|
+
* Space Complexity: O(1)
|
|
76
|
+
*
|
|
77
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
78
|
+
* end values.
|
|
79
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
80
|
+
*/
|
|
81
|
+
get end(): HashMapIterator<K, V>;
|
|
82
|
+
/**
|
|
83
|
+
* Time Complexity: O(1)
|
|
84
|
+
* Space Complexity: O(1)
|
|
85
|
+
*
|
|
86
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
87
|
+
* a HashMap in reverse order.
|
|
88
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
89
|
+
*/
|
|
90
|
+
get reverseBegin(): HashMapIterator<K, V>;
|
|
91
|
+
/**
|
|
92
|
+
* Time Complexity: O(1)
|
|
93
|
+
* Space Complexity: O(1)
|
|
94
|
+
*
|
|
95
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
96
|
+
* HashMap in reverse order.
|
|
97
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
98
|
+
*/
|
|
99
|
+
get reverseEnd(): HashMapIterator<K, V>;
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(1)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*
|
|
104
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
105
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
106
|
+
* value (V).
|
|
107
|
+
*/
|
|
108
|
+
get front(): [K, V] | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(1)
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
*
|
|
113
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
114
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
115
|
+
* data structure.
|
|
116
|
+
*/
|
|
117
|
+
get back(): [K, V] | undefined;
|
|
65
118
|
/**
|
|
66
119
|
* Time Complexity: O(1)
|
|
67
120
|
* Space Complexity: O(1)
|
|
@@ -159,59 +212,6 @@ export declare class HashMap<K = any, V = any> {
|
|
|
159
212
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
160
213
|
*/
|
|
161
214
|
clear(): void;
|
|
162
|
-
/**
|
|
163
|
-
* Time Complexity: O(1)
|
|
164
|
-
* Space Complexity: O(1)
|
|
165
|
-
*
|
|
166
|
-
* The function returns a new iterator object for a HashMap.
|
|
167
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
168
|
-
*/
|
|
169
|
-
get begin(): HashMapIterator<K, V>;
|
|
170
|
-
/**
|
|
171
|
-
* Time Complexity: O(1)
|
|
172
|
-
* Space Complexity: O(1)
|
|
173
|
-
*
|
|
174
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
175
|
-
* end values.
|
|
176
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
177
|
-
*/
|
|
178
|
-
get end(): HashMapIterator<K, V>;
|
|
179
|
-
/**
|
|
180
|
-
* Time Complexity: O(1)
|
|
181
|
-
* Space Complexity: O(1)
|
|
182
|
-
*
|
|
183
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
184
|
-
* a HashMap in reverse order.
|
|
185
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
186
|
-
*/
|
|
187
|
-
get reverseBegin(): HashMapIterator<K, V>;
|
|
188
|
-
/**
|
|
189
|
-
* Time Complexity: O(1)
|
|
190
|
-
* Space Complexity: O(1)
|
|
191
|
-
*
|
|
192
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
193
|
-
* HashMap in reverse order.
|
|
194
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
195
|
-
*/
|
|
196
|
-
get reverseEnd(): HashMapIterator<K, V>;
|
|
197
|
-
/**
|
|
198
|
-
* Time Complexity: O(1)
|
|
199
|
-
* Space Complexity: O(1)
|
|
200
|
-
*
|
|
201
|
-
* The function returns the key-value pair at the front of a data structure.
|
|
202
|
-
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
203
|
-
* value (V).
|
|
204
|
-
*/
|
|
205
|
-
get front(): [K, V] | undefined;
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(1)
|
|
208
|
-
* Space Complexity: O(1)
|
|
209
|
-
*
|
|
210
|
-
* The function returns the key-value pair at the end of a data structure.
|
|
211
|
-
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
212
|
-
* data structure.
|
|
213
|
-
*/
|
|
214
|
-
get back(): [K, V] | undefined;
|
|
215
215
|
/**
|
|
216
216
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
217
217
|
* Space Complexity: O(1)
|
|
@@ -112,9 +112,6 @@ class HashMapIterator {
|
|
|
112
112
|
}
|
|
113
113
|
exports.HashMapIterator = HashMapIterator;
|
|
114
114
|
class HashMap {
|
|
115
|
-
get size() {
|
|
116
|
-
return this._size;
|
|
117
|
-
}
|
|
118
115
|
/**
|
|
119
116
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
120
117
|
* @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
@@ -122,9 +119,9 @@ class HashMap {
|
|
|
122
119
|
* `K` represents the type of the key and `V` represents the
|
|
123
120
|
*/
|
|
124
121
|
constructor(hashMap = []) {
|
|
122
|
+
this.OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
125
123
|
this._nodes = [];
|
|
126
124
|
this._orgMap = {};
|
|
127
|
-
this.OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
|
|
128
125
|
this._size = 0;
|
|
129
126
|
Object.setPrototypeOf(this._orgMap, null);
|
|
130
127
|
this._sentinel = {};
|
|
@@ -133,6 +130,78 @@ class HashMap {
|
|
|
133
130
|
this.set(el[0], el[1]);
|
|
134
131
|
});
|
|
135
132
|
}
|
|
133
|
+
get size() {
|
|
134
|
+
return this._size;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
140
|
+
* The function returns a new iterator object for a HashMap.
|
|
141
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
142
|
+
*/
|
|
143
|
+
get begin() {
|
|
144
|
+
return new HashMapIterator(this._head, this._sentinel, this);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Time Complexity: O(1)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*
|
|
150
|
+
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
151
|
+
* end values.
|
|
152
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
153
|
+
*/
|
|
154
|
+
get end() {
|
|
155
|
+
return new HashMapIterator(this._sentinel, this._sentinel, this);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(1)
|
|
159
|
+
* Space Complexity: O(1)
|
|
160
|
+
*
|
|
161
|
+
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
162
|
+
* a HashMap in reverse order.
|
|
163
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
164
|
+
*/
|
|
165
|
+
get reverseBegin() {
|
|
166
|
+
return new HashMapIterator(this._tail, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Time Complexity: O(1)
|
|
170
|
+
* Space Complexity: O(1)
|
|
171
|
+
*
|
|
172
|
+
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
173
|
+
* HashMap in reverse order.
|
|
174
|
+
* @returns A new instance of the HashMapIterator class is being returned.
|
|
175
|
+
*/
|
|
176
|
+
get reverseEnd() {
|
|
177
|
+
return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Time Complexity: O(1)
|
|
181
|
+
* Space Complexity: O(1)
|
|
182
|
+
*
|
|
183
|
+
* The function returns the key-value pair at the front of a data structure.
|
|
184
|
+
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
185
|
+
* value (V).
|
|
186
|
+
*/
|
|
187
|
+
get front() {
|
|
188
|
+
if (this._size === 0)
|
|
189
|
+
return;
|
|
190
|
+
return [this._head.key, this._head.value];
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(1)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
*
|
|
196
|
+
* The function returns the key-value pair at the end of a data structure.
|
|
197
|
+
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
198
|
+
* data structure.
|
|
199
|
+
*/
|
|
200
|
+
get back() {
|
|
201
|
+
if (this._size === 0)
|
|
202
|
+
return;
|
|
203
|
+
return [this._tail.key, this._tail.value];
|
|
204
|
+
}
|
|
136
205
|
/**
|
|
137
206
|
* Time Complexity: O(1)
|
|
138
207
|
* Space Complexity: O(1)
|
|
@@ -340,75 +409,6 @@ class HashMap {
|
|
|
340
409
|
this._size = 0;
|
|
341
410
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
342
411
|
}
|
|
343
|
-
/**
|
|
344
|
-
* Time Complexity: O(1)
|
|
345
|
-
* Space Complexity: O(1)
|
|
346
|
-
*
|
|
347
|
-
* The function returns a new iterator object for a HashMap.
|
|
348
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
349
|
-
*/
|
|
350
|
-
get begin() {
|
|
351
|
-
return new HashMapIterator(this._head, this._sentinel, this);
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Time Complexity: O(1)
|
|
355
|
-
* Space Complexity: O(1)
|
|
356
|
-
*
|
|
357
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
358
|
-
* end values.
|
|
359
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
360
|
-
*/
|
|
361
|
-
get end() {
|
|
362
|
-
return new HashMapIterator(this._sentinel, this._sentinel, this);
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Time Complexity: O(1)
|
|
366
|
-
* Space Complexity: O(1)
|
|
367
|
-
*
|
|
368
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
369
|
-
* a HashMap in reverse order.
|
|
370
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
371
|
-
*/
|
|
372
|
-
get reverseBegin() {
|
|
373
|
-
return new HashMapIterator(this._tail, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Time Complexity: O(1)
|
|
377
|
-
* Space Complexity: O(1)
|
|
378
|
-
*
|
|
379
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
380
|
-
* HashMap in reverse order.
|
|
381
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
382
|
-
*/
|
|
383
|
-
get reverseEnd() {
|
|
384
|
-
return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Time Complexity: O(1)
|
|
388
|
-
* Space Complexity: O(1)
|
|
389
|
-
*
|
|
390
|
-
* The function returns the key-value pair at the front of a data structure.
|
|
391
|
-
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
392
|
-
* value (V).
|
|
393
|
-
*/
|
|
394
|
-
get front() {
|
|
395
|
-
if (this._size === 0)
|
|
396
|
-
return;
|
|
397
|
-
return [this._head.key, this._head.value];
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Time Complexity: O(1)
|
|
401
|
-
* Space Complexity: O(1)
|
|
402
|
-
*
|
|
403
|
-
* The function returns the key-value pair at the end of a data structure.
|
|
404
|
-
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
405
|
-
* data structure.
|
|
406
|
-
*/
|
|
407
|
-
get back() {
|
|
408
|
-
if (this._size === 0)
|
|
409
|
-
return;
|
|
410
|
-
return [this._tail.key, this._tail.value];
|
|
411
|
-
}
|
|
412
412
|
/**
|
|
413
413
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
414
414
|
* Space Complexity: O(1)
|
package/package.json
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey} from '../../types';
|
|
10
|
-
import {BTNCallback} from '../../types';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
8
|
+
import { BST, BSTNode } from './bst';
|
|
9
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
|
+
import { BTNCallback } from '../../types';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
14
14
|
height: number;
|
|
@@ -95,7 +95,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
95
95
|
): BiTreeDeleteResult<N>[] {
|
|
96
96
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
97
97
|
const deletedResults = super.delete(identifier, callback);
|
|
98
|
-
for (const {needBalanced} of deletedResults) {
|
|
98
|
+
for (const { needBalanced } of deletedResults) {
|
|
99
99
|
if (needBalanced) {
|
|
100
100
|
this._balancePath(needBalanced);
|
|
101
101
|
}
|
|
@@ -118,7 +118,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
118
118
|
destNode = this.ensureNotKey(destNode);
|
|
119
119
|
|
|
120
120
|
if (srcNode && destNode) {
|
|
121
|
-
const {key, value, height} = destNode;
|
|
121
|
+
const { key, value, height } = destNode;
|
|
122
122
|
const tempNode = this.createNode(key, value);
|
|
123
123
|
|
|
124
124
|
if (tempNode) {
|
|
@@ -209,7 +209,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
209
209
|
// 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:
|
|
210
210
|
switch (
|
|
211
211
|
this._balanceFactor(A) // second O(1)
|
|
212
|
-
|
|
212
|
+
) {
|
|
213
213
|
case -2:
|
|
214
214
|
if (A && A.left) {
|
|
215
215
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {getMSB} from '../../utils';
|
|
8
|
+
import { getMSB } from '../../utils';
|
|
9
9
|
|
|
10
10
|
export class BinaryIndexedTree {
|
|
11
11
|
protected readonly _freq: number;
|
|
@@ -17,10 +17,10 @@ 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
|
-
this._freqMap = {0: 0};
|
|
23
|
+
this._freqMap = { 0: 0 };
|
|
24
24
|
this._msb = getMSB(max);
|
|
25
25
|
this._negativeCount = frequency < 0 ? max : 0;
|
|
26
26
|
}
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey} from '../../types';
|
|
10
|
-
import {BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
12
|
-
import {trampoline} from '../../utils';
|
|
13
|
-
import {Queue} from '../queue';
|
|
9
|
+
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
|
|
10
|
+
import { BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
import { trampoline } from '../../utils';
|
|
13
|
+
import { Queue } from '../queue';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
@@ -107,7 +107,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
107
107
|
* Represents a binary tree data structure.
|
|
108
108
|
* @template N - The type of the binary tree's nodes.
|
|
109
109
|
*/
|
|
110
|
-
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
110
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
|
|
111
|
+
implements IBinaryTree<V, N> {
|
|
111
112
|
iterationType: IterationType = IterationType.ITERATIVE;
|
|
112
113
|
|
|
113
114
|
/**
|
|
@@ -116,7 +117,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
116
117
|
*/
|
|
117
118
|
constructor(options?: BinaryTreeOptions) {
|
|
118
119
|
if (options) {
|
|
119
|
-
const {iterationType = IterationType.ITERATIVE} = options;
|
|
120
|
+
const { iterationType = IterationType.ITERATIVE } = options;
|
|
120
121
|
this.iterationType = iterationType;
|
|
121
122
|
}
|
|
122
123
|
this._size = 0;
|
|
@@ -315,7 +316,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
315
316
|
// Handle the case when there's only one root node
|
|
316
317
|
this._setRoot(null);
|
|
317
318
|
} else {
|
|
318
|
-
const {familyPosition: fp} = curr;
|
|
319
|
+
const { familyPosition: fp } = curr;
|
|
319
320
|
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
|
|
320
321
|
parent.left = curr.right;
|
|
321
322
|
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
|
|
@@ -330,7 +331,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
330
331
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
331
332
|
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
332
333
|
if (parentOfLeftSubTreeMax) {
|
|
333
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
334
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
335
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
334
336
|
else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
335
337
|
needBalanced = parentOfLeftSubTreeMax;
|
|
336
338
|
}
|
|
@@ -339,7 +341,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
339
341
|
}
|
|
340
342
|
this._size = this.size - 1;
|
|
341
343
|
|
|
342
|
-
deletedResult.push({deleted: orgCurrent, needBalanced});
|
|
344
|
+
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
343
345
|
return deletedResult;
|
|
344
346
|
}
|
|
345
347
|
|
|
@@ -409,14 +411,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
409
411
|
|
|
410
412
|
return _getMaxHeight(beginRoot);
|
|
411
413
|
} else {
|
|
412
|
-
const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
|
|
414
|
+
const stack: { node: N; depth: number }[] = [{ node: beginRoot, depth: 0 }];
|
|
413
415
|
let maxHeight = 0;
|
|
414
416
|
|
|
415
417
|
while (stack.length > 0) {
|
|
416
|
-
const {node, depth} = stack.pop()!;
|
|
418
|
+
const { node, depth } = stack.pop()!;
|
|
417
419
|
|
|
418
|
-
if (node.left) stack.push({node: node.left, depth: depth + 1});
|
|
419
|
-
if (node.right) stack.push({node: node.right, depth: depth + 1});
|
|
420
|
+
if (node.left) stack.push({ node: node.left, depth: depth + 1 });
|
|
421
|
+
if (node.right) stack.push({ node: node.right, depth: depth + 1 });
|
|
420
422
|
|
|
421
423
|
maxHeight = Math.max(maxHeight, depth);
|
|
422
424
|
}
|
|
@@ -912,7 +914,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
912
914
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
913
915
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
914
916
|
*/
|
|
915
|
-
getLeftMost(
|
|
917
|
+
getLeftMost(
|
|
918
|
+
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
919
|
+
iterationType = this.iterationType
|
|
920
|
+
): N | null | undefined {
|
|
916
921
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
917
922
|
|
|
918
923
|
if (!beginRoot) return beginRoot;
|
|
@@ -955,7 +960,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
955
960
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
956
961
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
957
962
|
*/
|
|
958
|
-
getRightMost(
|
|
963
|
+
getRightMost(
|
|
964
|
+
beginRoot: BTNKey | N | null | undefined = this.root,
|
|
965
|
+
iterationType = this.iterationType
|
|
966
|
+
): N | null | undefined {
|
|
959
967
|
// TODO support get right most by passing key in
|
|
960
968
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
961
969
|
if (!beginRoot) return beginRoot;
|
|
@@ -1284,7 +1292,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1284
1292
|
_traverse(beginRoot);
|
|
1285
1293
|
} else {
|
|
1286
1294
|
// 0: visit, 1: print
|
|
1287
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
1295
|
+
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{ opt: 0, node: beginRoot }];
|
|
1288
1296
|
|
|
1289
1297
|
while (stack.length > 0) {
|
|
1290
1298
|
const cur = stack.pop();
|
|
@@ -1299,24 +1307,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1299
1307
|
} else {
|
|
1300
1308
|
switch (pattern) {
|
|
1301
1309
|
case 'in':
|
|
1302
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1303
|
-
stack.push({opt: 1, node: cur.node});
|
|
1304
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1310
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1311
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1312
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1305
1313
|
break;
|
|
1306
1314
|
case 'pre':
|
|
1307
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1308
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1309
|
-
stack.push({opt: 1, node: cur.node});
|
|
1315
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1316
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1317
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1310
1318
|
break;
|
|
1311
1319
|
case 'post':
|
|
1312
|
-
stack.push({opt: 1, node: cur.node});
|
|
1313
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1314
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1320
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1321
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1322
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1315
1323
|
break;
|
|
1316
1324
|
default:
|
|
1317
|
-
cur.node && stack.push({opt: 0, node: cur.node.right});
|
|
1318
|
-
stack.push({opt: 1, node: cur.node});
|
|
1319
|
-
cur.node && stack.push({opt: 0, node: cur.node.left});
|
|
1325
|
+
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1326
|
+
stack.push({ opt: 1, node: cur.node });
|
|
1327
|
+
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1320
1328
|
break;
|
|
1321
1329
|
}
|
|
1322
1330
|
}
|
|
@@ -1686,7 +1694,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1686
1694
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1687
1695
|
* binary tree nodes in a specific order.
|
|
1688
1696
|
*/
|
|
1689
|
-
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1697
|
+
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
|
|
1690
1698
|
if (!node) {
|
|
1691
1699
|
return;
|
|
1692
1700
|
}
|
|
@@ -1798,7 +1806,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1798
1806
|
destNode = this.ensureNotKey(destNode);
|
|
1799
1807
|
|
|
1800
1808
|
if (srcNode && destNode) {
|
|
1801
|
-
const {key, value} = destNode;
|
|
1809
|
+
const { key, value } = destNode;
|
|
1802
1810
|
const tempNode = this.createNode(key, value);
|
|
1803
1811
|
|
|
1804
1812
|
if (tempNode) {
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey} from '../../types';
|
|
9
|
-
import {CP, IterationType} from '../../types';
|
|
10
|
-
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
|
-
import {IBinaryTree} from '../../interfaces';
|
|
12
|
-
import {Queue} from '../queue';
|
|
8
|
+
import type { BSTComparator, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
|
+
import { CP, IterationType } from '../../types';
|
|
10
|
+
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
import { Queue } from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
15
|
override parent?: N;
|
|
@@ -62,7 +62,9 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
65
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
|
|
66
|
+
extends BinaryTree<V, N>
|
|
67
|
+
implements IBinaryTree<V, N> {
|
|
66
68
|
/**
|
|
67
69
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
68
70
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
@@ -72,7 +74,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
72
74
|
super(options);
|
|
73
75
|
this._root = undefined;
|
|
74
76
|
if (options !== undefined) {
|
|
75
|
-
const {comparator} = options;
|
|
77
|
+
const { comparator } = options;
|
|
76
78
|
if (comparator !== undefined) {
|
|
77
79
|
this._comparator = comparator;
|
|
78
80
|
}
|
|
@@ -225,7 +227,9 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
225
227
|
}
|
|
226
228
|
|
|
227
229
|
const inserted: (N | undefined)[] = [];
|
|
228
|
-
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
230
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map(
|
|
231
|
+
(value: BTNKey | N, index) => [value, data?.[index]] as [BTNKey | N, V]
|
|
232
|
+
);
|
|
229
233
|
|
|
230
234
|
let sorted = [];
|
|
231
235
|
|
|
@@ -6,12 +6,23 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
import {
|
|
10
|
+
BiTreeDeleteResult,
|
|
11
|
+
BTNCallback,
|
|
12
|
+
BTNKey,
|
|
13
|
+
IterationType,
|
|
14
|
+
RBTNColor,
|
|
15
|
+
RBTreeOptions,
|
|
16
|
+
RedBlackTreeNodeNested
|
|
17
|
+
} from '../../types';
|
|
18
|
+
import { BST, BSTNode } from './bst';
|
|
19
|
+
import { IBinaryTree } from '../../interfaces';
|
|
20
|
+
import { BinaryTreeNode } from './binary-tree';
|
|
21
|
+
|
|
22
|
+
export class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<
|
|
23
|
+
V,
|
|
24
|
+
N
|
|
25
|
+
> {
|
|
15
26
|
color: RBTNColor;
|
|
16
27
|
|
|
17
28
|
constructor(key: BTNKey, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|