data-structure-typed 1.33.7 → 1.33.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +25 -14
- package/package.json +22 -22
- package/.prettierrc.js +0 -16
- package/coverage/coverage-final.json +0 -68
- package/coverage/coverage-summary.json +0 -69
- package/docs/.nojekyll +0 -1
- package/docs/index.html +0 -875
- package/docs/modules.html +0 -336
- package/jest.config.js +0 -8
- package/src/data-structures/binary-tree/aa-tree.ts +0 -1
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
- package/src/data-structures/binary-tree/avl-tree.ts +0 -307
- package/src/data-structures/binary-tree/b-tree.ts +0 -1
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
- package/src/data-structures/binary-tree/binary-tree.ts +0 -47
- package/src/data-structures/binary-tree/bst.ts +0 -537
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -366
- package/src/data-structures/binary-tree/segment-tree.ts +0 -260
- package/src/data-structures/binary-tree/splay-tree.ts +0 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
- package/src/data-structures/graph/abstract-graph.ts +0 -1040
- package/src/data-structures/graph/directed-graph.ts +0 -470
- package/src/data-structures/graph/index.ts +0 -4
- package/src/data-structures/graph/map-graph.ts +0 -129
- package/src/data-structures/graph/undirected-graph.ts +0 -274
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-map.ts +0 -203
- package/src/data-structures/hash/hash-table.ts +0 -277
- package/src/data-structures/hash/index.ts +0 -7
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -212
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -32
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -213
- package/src/data-structures/matrix/navigator.ts +0 -121
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
- package/src/data-structures/priority-queue/priority-queue.ts +0 -359
- package/src/data-structures/queue/deque.ts +0 -297
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -191
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/index.ts +0 -4
- package/src/interfaces/abstract-binary-tree.ts +0 -189
- package/src/interfaces/abstract-graph.ts +0 -31
- package/src/interfaces/avl-tree.ts +0 -25
- package/src/interfaces/binary-tree.ts +0 -6
- package/src/interfaces/bst.ts +0 -31
- package/src/interfaces/directed-graph.ts +0 -20
- package/src/interfaces/doubly-linked-list.ts +0 -1
- package/src/interfaces/heap.ts +0 -1
- package/src/interfaces/index.ts +0 -15
- package/src/interfaces/navigator.ts +0 -1
- package/src/interfaces/priority-queue.ts +0 -1
- package/src/interfaces/rb-tree.ts +0 -9
- package/src/interfaces/segment-tree.ts +0 -1
- package/src/interfaces/singly-linked-list.ts +0 -1
- package/src/interfaces/tree-multiset.ts +0 -7
- package/src/interfaces/undirected-graph.ts +0 -6
- package/src/types/data-structures/abstract-binary-tree.ts +0 -50
- package/src/types/data-structures/abstract-graph.ts +0 -11
- package/src/types/data-structures/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree.ts +0 -5
- package/src/types/data-structures/bst.ts +0 -13
- package/src/types/data-structures/directed-graph.ts +0 -8
- package/src/types/data-structures/doubly-linked-list.ts +0 -1
- package/src/types/data-structures/hash.ts +0 -1
- package/src/types/data-structures/heap.ts +0 -5
- package/src/types/data-structures/index.ts +0 -16
- package/src/types/data-structures/map-graph.ts +0 -1
- package/src/types/data-structures/navigator.ts +0 -13
- package/src/types/data-structures/priority-queue.ts +0 -9
- package/src/types/data-structures/rb-tree.ts +0 -8
- package/src/types/data-structures/segment-tree.ts +0 -1
- package/src/types/data-structures/singly-linked-list.ts +0 -1
- package/src/types/data-structures/tree-multiset.ts +0 -6
- package/src/types/helpers.ts +0 -1
- package/src/types/index.ts +0 -3
- package/src/types/utils/index.ts +0 -2
- package/src/types/utils/utils.ts +0 -6
- package/src/types/utils/validate-type.ts +0 -35
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -79
- package/test/integration/avl-tree.test.ts +0 -108
- package/test/integration/bst.test.ts +0 -380
- package/test/integration/heap.test.js +0 -16
- package/test/integration/index.html +0 -44
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
- package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
- package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
- package/test/unit/data-structures/graph/overall.test.ts +0 -49
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
- package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
- package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
- package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
- package/test/unit/data-structures/heap/heap.test.ts +0 -55
- package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
- package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
- package/test/unit/data-structures/linked-list/index.ts +0 -4
- package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
- package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
- package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
- package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
- package/test/unit/data-structures/queue/deque.test.ts +0 -130
- package/test/unit/data-structures/queue/queue.test.ts +0 -199
- package/test/unit/data-structures/stack/stack.test.ts +0 -67
- package/test/unit/data-structures/tree/tree.test.ts +0 -39
- package/test/unit/data-structures/trie/trie.test.ts +0 -95
- package/test/utils/index.ts +0 -2
- package/test/utils/magnitude.ts +0 -21
- package/test/utils/number.ts +0 -3
- package/tsconfig.json +0 -29
|
@@ -1,537 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions} from '../../types';
|
|
9
|
-
import {CP, LoopType} from '../../types';
|
|
10
|
-
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
|
-
import {IBST, IBSTNode} from '../../interfaces';
|
|
12
|
-
|
|
13
|
-
export class BSTNode<V = any, NEIGHBOR extends BSTNode<V, NEIGHBOR> = BSTNodeNested<V>>
|
|
14
|
-
extends BinaryTreeNode<V, NEIGHBOR>
|
|
15
|
-
implements IBSTNode<V, NEIGHBOR>
|
|
16
|
-
{
|
|
17
|
-
constructor(id: BinaryTreeNodeId, val?: V) {
|
|
18
|
-
super(id, val);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
|
|
23
|
-
/**
|
|
24
|
-
* The constructor function initializes a binary search tree object with an optional comparator function.
|
|
25
|
-
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
|
|
26
|
-
*/
|
|
27
|
-
constructor(options?: BSTOptions) {
|
|
28
|
-
super(options);
|
|
29
|
-
if (options !== undefined) {
|
|
30
|
-
const {comparator} = options;
|
|
31
|
-
if (comparator !== undefined) {
|
|
32
|
-
this._comparator = comparator;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The function creates a new binary search tree node with the given id and value.
|
|
39
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
40
|
-
* identify each node in the binary tree.
|
|
41
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
42
|
-
* that will be stored in the node.
|
|
43
|
-
* @returns a new instance of the BSTNode class with the specified id and value.
|
|
44
|
-
*/
|
|
45
|
-
override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
|
|
46
|
-
return new BSTNode<N['val'], N>(id, val) as N;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
|
51
|
-
* node with the same ID.
|
|
52
|
-
* @param {BinaryTreeNodeId | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N`
|
|
53
|
-
* (which represents a binary tree node) or `null`.
|
|
54
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
55
|
-
* being added to the binary search tree.
|
|
56
|
-
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
|
57
|
-
*/
|
|
58
|
-
override add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val']): N | null | undefined {
|
|
59
|
-
// TODO support node as a param
|
|
60
|
-
let inserted: N | null = null;
|
|
61
|
-
let newNode: N | null = null;
|
|
62
|
-
if (idOrNode instanceof BSTNode) {
|
|
63
|
-
newNode = idOrNode;
|
|
64
|
-
} else if (typeof idOrNode === 'number') {
|
|
65
|
-
newNode = this.createNode(idOrNode, val);
|
|
66
|
-
} else if (idOrNode === null) {
|
|
67
|
-
newNode = null;
|
|
68
|
-
}
|
|
69
|
-
if (this.root === null) {
|
|
70
|
-
this._setRoot(newNode);
|
|
71
|
-
this._setSize(this.size + 1);
|
|
72
|
-
inserted = this.root;
|
|
73
|
-
} else {
|
|
74
|
-
let cur = this.root;
|
|
75
|
-
let traversing = true;
|
|
76
|
-
while (traversing) {
|
|
77
|
-
if (cur !== null && newNode !== null) {
|
|
78
|
-
if (this._compare(cur.id, newNode.id) === CP.eq) {
|
|
79
|
-
if (newNode) {
|
|
80
|
-
cur.val = newNode.val;
|
|
81
|
-
}
|
|
82
|
-
//Duplicates are not accepted.
|
|
83
|
-
traversing = false;
|
|
84
|
-
inserted = cur;
|
|
85
|
-
} else if (this._compare(cur.id, newNode.id) === CP.gt) {
|
|
86
|
-
// Traverse left of the node
|
|
87
|
-
if (cur.left === undefined) {
|
|
88
|
-
if (newNode) {
|
|
89
|
-
newNode.parent = cur;
|
|
90
|
-
}
|
|
91
|
-
//Add to the left of the current node
|
|
92
|
-
cur.left = newNode;
|
|
93
|
-
this._setSize(this.size + 1);
|
|
94
|
-
traversing = false;
|
|
95
|
-
inserted = cur.left;
|
|
96
|
-
} else {
|
|
97
|
-
//Traverse the left of the current node
|
|
98
|
-
if (cur.left) cur = cur.left;
|
|
99
|
-
}
|
|
100
|
-
} else if (this._compare(cur.id, newNode.id) === CP.lt) {
|
|
101
|
-
// Traverse right of the node
|
|
102
|
-
if (cur.right === undefined) {
|
|
103
|
-
if (newNode) {
|
|
104
|
-
newNode.parent = cur;
|
|
105
|
-
}
|
|
106
|
-
//Add to the right of the current node
|
|
107
|
-
cur.right = newNode;
|
|
108
|
-
this._setSize(this.size + 1);
|
|
109
|
-
traversing = false;
|
|
110
|
-
inserted = cur.right;
|
|
111
|
-
} else {
|
|
112
|
-
//Traverse the left of the current node
|
|
113
|
-
if (cur.right) cur = cur.right;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
} else {
|
|
117
|
-
traversing = false;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return inserted;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
126
|
-
* manner.
|
|
127
|
-
* @param {[BinaryTreeNodeId | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
|
|
128
|
-
* `BinaryTreeNodeId` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
129
|
-
* to the binary search tree.
|
|
130
|
-
* @param {N['val'][]} data - The values of tree nodes
|
|
131
|
-
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
132
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
133
|
-
*/
|
|
134
|
-
override addMany(
|
|
135
|
-
idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[],
|
|
136
|
-
data?: N['val'][],
|
|
137
|
-
isBalanceAdd = false
|
|
138
|
-
): (N | null | undefined)[] {
|
|
139
|
-
function hasNoNull(arr: (BinaryTreeNodeId | null)[] | (N | null)[]): arr is BinaryTreeNodeId[] | N[] {
|
|
140
|
-
return arr.indexOf(null) === -1;
|
|
141
|
-
}
|
|
142
|
-
if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
|
|
143
|
-
return super.addMany(idsOrNodes, data);
|
|
144
|
-
}
|
|
145
|
-
const inserted: (N | null | undefined)[] = [];
|
|
146
|
-
const combinedArr: [BinaryTreeNodeId | N, N['val']][] = idsOrNodes.map((value, index) => [value, data?.[index]]);
|
|
147
|
-
let sorted = [];
|
|
148
|
-
function isNodeOrNullTuple(arr: [BinaryTreeNodeId | N, N['val']][]): arr is [N, N['val']][] {
|
|
149
|
-
for (const [idOrNode] of arr) if (idOrNode instanceof BSTNode) return true;
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
function isBinaryTreeIdOrNullTuple(arr: [BinaryTreeNodeId | N, N['val']][]): arr is [BinaryTreeNodeId, N['val']][] {
|
|
153
|
-
for (const [idOrNode] of arr) if (typeof idOrNode === 'number') return true;
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
let sortedIdsOrNodes: (number | N | null)[] = [],
|
|
157
|
-
sortedData: (N['val'] | undefined)[] | undefined = [];
|
|
158
|
-
|
|
159
|
-
if (isNodeOrNullTuple(combinedArr)) {
|
|
160
|
-
sorted = combinedArr.sort((a, b) => a[0].id - b[0].id);
|
|
161
|
-
} else if (isBinaryTreeIdOrNullTuple(combinedArr)) {
|
|
162
|
-
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
163
|
-
} else {
|
|
164
|
-
throw new Error('Invalid input idsOrNodes');
|
|
165
|
-
}
|
|
166
|
-
sortedIdsOrNodes = sorted.map(([idOrNode]) => idOrNode);
|
|
167
|
-
sortedData = sorted.map(([, val]) => val);
|
|
168
|
-
const recursive = (arr: (BinaryTreeNodeId | null | N)[], data?: N['val'][]) => {
|
|
169
|
-
if (arr.length === 0) return;
|
|
170
|
-
|
|
171
|
-
const mid = Math.floor((arr.length - 1) / 2);
|
|
172
|
-
const newNode = this.add(arr[mid], data?.[mid]);
|
|
173
|
-
inserted.push(newNode);
|
|
174
|
-
recursive(arr.slice(0, mid), data?.slice(0, mid));
|
|
175
|
-
recursive(arr.slice(mid + 1), data?.slice(mid + 1));
|
|
176
|
-
};
|
|
177
|
-
const iterative = () => {
|
|
178
|
-
const n = sorted.length;
|
|
179
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
180
|
-
while (stack.length > 0) {
|
|
181
|
-
const popped = stack.pop();
|
|
182
|
-
if (popped) {
|
|
183
|
-
const [l, r] = popped;
|
|
184
|
-
if (l <= r) {
|
|
185
|
-
const m = l + Math.floor((r - l) / 2);
|
|
186
|
-
const newNode = this.add(sortedIdsOrNodes[m], sortedData?.[m]);
|
|
187
|
-
inserted.push(newNode);
|
|
188
|
-
stack.push([m + 1, r]);
|
|
189
|
-
stack.push([l, m - 1]);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
195
|
-
recursive(sortedIdsOrNodes, sortedData);
|
|
196
|
-
} else {
|
|
197
|
-
iterative();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return inserted;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
205
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
206
|
-
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
207
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
208
|
-
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
|
|
209
|
-
* @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
|
|
210
|
-
*/
|
|
211
|
-
override get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null {
|
|
212
|
-
propertyName = propertyName ?? 'id';
|
|
213
|
-
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
218
|
-
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
219
|
-
* @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
|
|
220
|
-
* the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
|
|
221
|
-
* equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
222
|
-
*/
|
|
223
|
-
lastKey(): BinaryTreeNodeId {
|
|
224
|
-
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.id ?? 0;
|
|
225
|
-
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.id ?? 0;
|
|
226
|
-
else return this.getRightMost()?.id ?? 0;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
231
|
-
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
|
|
232
|
-
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
233
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
234
|
-
* specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
|
|
235
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
236
|
-
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
237
|
-
* is set to `true`, the function will return an array with only one node (if
|
|
238
|
-
* @returns an array of nodes (type N).
|
|
239
|
-
*/
|
|
240
|
-
override getNodes(
|
|
241
|
-
nodeProperty: BinaryTreeNodeId | N,
|
|
242
|
-
propertyName: BinaryTreeNodePropertyName = 'id',
|
|
243
|
-
onlyOne?: boolean
|
|
244
|
-
): N[] {
|
|
245
|
-
if (!this.root) return [];
|
|
246
|
-
const result: N[] = [];
|
|
247
|
-
|
|
248
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
249
|
-
const _traverse = (cur: N) => {
|
|
250
|
-
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
|
|
251
|
-
|
|
252
|
-
if (!cur.left && !cur.right) return;
|
|
253
|
-
if (propertyName === 'id') {
|
|
254
|
-
if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
|
|
255
|
-
if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
|
|
256
|
-
} else {
|
|
257
|
-
cur.left && _traverse(cur.left);
|
|
258
|
-
cur.right && _traverse(cur.right);
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
_traverse(this.root);
|
|
263
|
-
} else {
|
|
264
|
-
const queue: N[] = [this.root];
|
|
265
|
-
while (queue.length > 0) {
|
|
266
|
-
const cur = queue.shift();
|
|
267
|
-
if (cur) {
|
|
268
|
-
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
|
|
269
|
-
if (propertyName === 'id') {
|
|
270
|
-
if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
|
|
271
|
-
if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
|
|
272
|
-
} else {
|
|
273
|
-
cur.left && queue.push(cur.left);
|
|
274
|
-
cur.right && queue.push(cur.right);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return result;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// --- start additional functions
|
|
284
|
-
/**
|
|
285
|
-
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
286
|
-
* less than a given node.
|
|
287
|
-
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
288
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
289
|
-
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
290
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
291
|
-
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
292
|
-
*/
|
|
293
|
-
lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number {
|
|
294
|
-
propertyName = propertyName ?? 'id';
|
|
295
|
-
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'id');
|
|
296
|
-
if (!beginNode) return 0;
|
|
297
|
-
if (!this.root) return 0;
|
|
298
|
-
const id = beginNode.id;
|
|
299
|
-
const getSumByPropertyName = (cur: N) => {
|
|
300
|
-
let needSum: number;
|
|
301
|
-
switch (propertyName) {
|
|
302
|
-
case 'id':
|
|
303
|
-
needSum = cur.id;
|
|
304
|
-
break;
|
|
305
|
-
default:
|
|
306
|
-
needSum = cur.id;
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
return needSum;
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
let sum = 0;
|
|
313
|
-
|
|
314
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
315
|
-
const _traverse = (cur: N): void => {
|
|
316
|
-
const compared = this._compare(cur.id, id);
|
|
317
|
-
if (compared === CP.eq) {
|
|
318
|
-
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
319
|
-
return;
|
|
320
|
-
} else if (compared === CP.lt) {
|
|
321
|
-
if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
|
|
322
|
-
sum += getSumByPropertyName(cur);
|
|
323
|
-
if (cur.right) _traverse(cur.right);
|
|
324
|
-
else return;
|
|
325
|
-
} else {
|
|
326
|
-
if (cur.left) _traverse(cur.left);
|
|
327
|
-
else return;
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
_traverse(this.root);
|
|
332
|
-
} else {
|
|
333
|
-
const queue: N[] = [this.root];
|
|
334
|
-
while (queue.length > 0) {
|
|
335
|
-
const cur = queue.shift();
|
|
336
|
-
if (cur) {
|
|
337
|
-
const compared = this._compare(cur.id, id);
|
|
338
|
-
if (compared === CP.eq) {
|
|
339
|
-
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
340
|
-
return sum;
|
|
341
|
-
} else if (compared === CP.lt) {
|
|
342
|
-
// todo maybe a bug
|
|
343
|
-
if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
|
|
344
|
-
sum += getSumByPropertyName(cur);
|
|
345
|
-
if (cur.right) queue.push(cur.right);
|
|
346
|
-
else return sum;
|
|
347
|
-
} else {
|
|
348
|
-
if (cur.left) queue.push(cur.left);
|
|
349
|
-
else return sum;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
return sum;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
360
|
-
* have a greater value than a given node.
|
|
361
|
-
* @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
362
|
-
* `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
363
|
-
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
364
|
-
* each greater node should be increased.
|
|
365
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
366
|
-
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
367
|
-
* 'id'.
|
|
368
|
-
* @returns a boolean value.
|
|
369
|
-
*/
|
|
370
|
-
allGreaterNodesAdd(
|
|
371
|
-
node: N | BinaryTreeNodeId | null,
|
|
372
|
-
delta: number,
|
|
373
|
-
propertyName?: BinaryTreeNodePropertyName
|
|
374
|
-
): boolean {
|
|
375
|
-
propertyName = propertyName ?? 'id';
|
|
376
|
-
if (typeof node === 'number') node = this.get(node, 'id');
|
|
377
|
-
if (!node) return false;
|
|
378
|
-
const id = node.id;
|
|
379
|
-
if (!this.root) return false;
|
|
380
|
-
|
|
381
|
-
const _sumByPropertyName = (cur: N) => {
|
|
382
|
-
switch (propertyName) {
|
|
383
|
-
case 'id':
|
|
384
|
-
cur.id += delta;
|
|
385
|
-
break;
|
|
386
|
-
default:
|
|
387
|
-
cur.id += delta;
|
|
388
|
-
break;
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
392
|
-
const _traverse = (cur: N) => {
|
|
393
|
-
const compared = this._compare(cur.id, id);
|
|
394
|
-
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
395
|
-
|
|
396
|
-
if (!cur.left && !cur.right) return;
|
|
397
|
-
if (cur.left && this._compare(cur.left.id, id) === CP.gt) _traverse(cur.left);
|
|
398
|
-
if (cur.right && this._compare(cur.right.id, id) === CP.gt) _traverse(cur.right);
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
_traverse(this.root);
|
|
402
|
-
return true;
|
|
403
|
-
} else {
|
|
404
|
-
const queue: N[] = [this.root];
|
|
405
|
-
while (queue.length > 0) {
|
|
406
|
-
const cur = queue.shift();
|
|
407
|
-
if (cur) {
|
|
408
|
-
const compared = this._compare(cur.id, id);
|
|
409
|
-
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
410
|
-
|
|
411
|
-
if (cur.left && this._compare(cur.left.id, id) === CP.gt) queue.push(cur.left);
|
|
412
|
-
if (cur.right && this._compare(cur.right.id, id) === CP.gt) queue.push(cur.right);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
return true;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Balancing Adjustment:
|
|
421
|
-
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
422
|
-
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
423
|
-
*
|
|
424
|
-
* Use Cases and Efficiency:
|
|
425
|
-
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
426
|
-
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
427
|
-
*/
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
431
|
-
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
432
|
-
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
433
|
-
*/
|
|
434
|
-
perfectlyBalance(): boolean {
|
|
435
|
-
const sorted = this.DFS('in', 'node'),
|
|
436
|
-
n = sorted.length;
|
|
437
|
-
this.clear();
|
|
438
|
-
|
|
439
|
-
if (sorted.length < 1) return false;
|
|
440
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
441
|
-
const buildBalanceBST = (l: number, r: number) => {
|
|
442
|
-
if (l > r) return;
|
|
443
|
-
const m = l + Math.floor((r - l) / 2);
|
|
444
|
-
const midNode = sorted[m];
|
|
445
|
-
this.add(midNode.id, midNode.val);
|
|
446
|
-
buildBalanceBST(l, m - 1);
|
|
447
|
-
buildBalanceBST(m + 1, r);
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
buildBalanceBST(0, n - 1);
|
|
451
|
-
return true;
|
|
452
|
-
} else {
|
|
453
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
454
|
-
while (stack.length > 0) {
|
|
455
|
-
const popped = stack.pop();
|
|
456
|
-
if (popped) {
|
|
457
|
-
const [l, r] = popped;
|
|
458
|
-
if (l <= r) {
|
|
459
|
-
const m = l + Math.floor((r - l) / 2);
|
|
460
|
-
const midNode = sorted[m];
|
|
461
|
-
this.add(midNode.id, midNode.val);
|
|
462
|
-
stack.push([m + 1, r]);
|
|
463
|
-
stack.push([l, m - 1]);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
return true;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
473
|
-
* @returns a boolean value.
|
|
474
|
-
*/
|
|
475
|
-
isAVLBalanced(): boolean {
|
|
476
|
-
if (!this.root) return true;
|
|
477
|
-
|
|
478
|
-
let balanced = true;
|
|
479
|
-
|
|
480
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
481
|
-
const _height = (cur: N | null | undefined): number => {
|
|
482
|
-
if (!cur) return 0;
|
|
483
|
-
const leftHeight = _height(cur.left),
|
|
484
|
-
rightHeight = _height(cur.right);
|
|
485
|
-
if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
|
|
486
|
-
return Math.max(leftHeight, rightHeight) + 1;
|
|
487
|
-
};
|
|
488
|
-
_height(this.root);
|
|
489
|
-
} else {
|
|
490
|
-
const stack: N[] = [];
|
|
491
|
-
let node: N | null | undefined = this.root,
|
|
492
|
-
last: N | null = null;
|
|
493
|
-
const depths: Map<N, number> = new Map();
|
|
494
|
-
|
|
495
|
-
while (stack.length > 0 || node) {
|
|
496
|
-
if (node) {
|
|
497
|
-
stack.push(node);
|
|
498
|
-
node = node.left;
|
|
499
|
-
} else {
|
|
500
|
-
node = stack[stack.length - 1];
|
|
501
|
-
if (!node.right || last === node.right) {
|
|
502
|
-
node = stack.pop();
|
|
503
|
-
if (node) {
|
|
504
|
-
const left = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
505
|
-
const right = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
506
|
-
if (Math.abs(left - right) > 1) return false;
|
|
507
|
-
depths.set(node, 1 + Math.max(left, right));
|
|
508
|
-
last = node;
|
|
509
|
-
node = null;
|
|
510
|
-
}
|
|
511
|
-
} else node = node.right;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return balanced;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
protected _comparator: BSTComparator = (a, b) => a - b;
|
|
520
|
-
|
|
521
|
-
/**
|
|
522
|
-
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
523
|
-
* greater than, less than, or equal to the second ID.
|
|
524
|
-
* @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
|
|
525
|
-
* @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
|
|
526
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
527
|
-
* than), or CP.eq (equal).
|
|
528
|
-
*/
|
|
529
|
-
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
|
|
530
|
-
const compared = this._comparator(a, b);
|
|
531
|
-
if (compared > 0) return CP.gt;
|
|
532
|
-
else if (compared < 0) return CP.lt;
|
|
533
|
-
else return CP.eq;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
// --- end additional functions
|
|
537
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export * from './abstract-binary-tree';
|
|
2
|
-
export * from './binary-tree';
|
|
3
|
-
export * from './bst';
|
|
4
|
-
export * from './binary-indexed-tree';
|
|
5
|
-
export * from './segment-tree';
|
|
6
|
-
export * from './avl-tree';
|
|
7
|
-
export * from './b-tree';
|
|
8
|
-
export * from './rb-tree';
|
|
9
|
-
export * from './splay-tree';
|
|
10
|
-
export * from './aa-tree';
|
|
11
|
-
export * from './tree-multiset';
|
|
12
|
-
export * from './two-three-tree';
|