data-structure-typed 1.34.7 → 1.34.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +70 -70
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +59 -59
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +39 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +49 -49
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +33 -33
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +4 -4
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/tree/tree.js +5 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +71 -71
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +110 -110
- package/lib/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/lib/data-structures/binary-tree/avl-tree.js +13 -13
- package/lib/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/lib/data-structures/binary-tree/binary-tree.js +7 -7
- package/lib/data-structures/binary-tree/bst.d.ts +34 -34
- package/lib/data-structures/binary-tree/bst.js +80 -80
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/rb-tree.js +4 -4
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +27 -27
- package/lib/data-structures/binary-tree/tree-multiset.js +55 -55
- package/lib/data-structures/graph/abstract-graph.d.ts +60 -60
- package/lib/data-structures/graph/abstract-graph.js +81 -81
- package/lib/data-structures/graph/directed-graph.d.ts +51 -51
- package/lib/data-structures/graph/directed-graph.js +63 -63
- package/lib/data-structures/graph/map-graph.d.ts +13 -13
- package/lib/data-structures/graph/map-graph.js +12 -12
- package/lib/data-structures/graph/undirected-graph.d.ts +30 -30
- package/lib/data-structures/graph/undirected-graph.js +32 -32
- package/lib/data-structures/tree/tree.d.ts +4 -4
- package/lib/data-structures/tree/tree.js +6 -6
- package/lib/interfaces/abstract-binary-tree.d.ts +24 -24
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +3 -3
- package/lib/interfaces/bst.d.ts +8 -8
- package/lib/interfaces/directed-graph.d.ts +5 -5
- package/lib/interfaces/rb-tree.d.ts +2 -2
- package/lib/interfaces/undirected-graph.d.ts +2 -2
- package/lib/types/data-structures/abstract-binary-tree.d.ts +3 -3
- package/lib/types/data-structures/abstract-graph.d.ts +2 -2
- package/lib/types/data-structures/bst.d.ts +2 -2
- package/lib/types/data-structures/tree-multiset.d.ts +1 -1
- package/lib/types/utils/validate-type.d.ts +8 -8
- package/package.json +1 -1
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +147 -147
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +98 -90
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +62 -62
- package/src/data-structures/graph/abstract-graph.ts +109 -104
- package/src/data-structures/graph/directed-graph.ts +77 -77
- package/src/data-structures/graph/map-graph.ts +20 -15
- package/src/data-structures/graph/undirected-graph.ts +39 -39
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +24 -24
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +3 -3
- package/src/interfaces/bst.ts +8 -8
- package/src/interfaces/directed-graph.ts +5 -5
- package/src/interfaces/rb-tree.ts +2 -2
- package/src/interfaces/undirected-graph.ts +2 -2
- package/src/types/data-structures/abstract-binary-tree.ts +3 -3
- package/src/types/data-structures/abstract-graph.ts +2 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/tree-multiset.ts +1 -1
- package/src/types/utils/validate-type.ts +10 -10
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/overall.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +72 -72
- package/test/unit/data-structures/graph/directed-graph.test.ts +8 -8
- package/test/unit/data-structures/graph/map-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult,
|
|
9
|
+
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
|
|
10
10
|
import {IAVLTree, IAVLTreeNode} from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AVLTreeNodeNested<V>>
|
|
13
13
|
extends BSTNode<V, NEIGHBOR>
|
|
14
14
|
implements IAVLTreeNode<V, NEIGHBOR>
|
|
15
15
|
{
|
|
16
|
-
constructor(
|
|
17
|
-
super(
|
|
16
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
17
|
+
super(key, val);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -30,27 +30,27 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* The function creates a new AVL tree node with the given
|
|
34
|
-
* @param {
|
|
33
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
34
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
35
35
|
* identify each node in the tree.
|
|
36
36
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
37
37
|
* that will be stored in the node.
|
|
38
|
-
* @returns a new AVLTreeNode object with the specified
|
|
38
|
+
* @returns a new AVLTreeNode object with the specified key and value.
|
|
39
39
|
*/
|
|
40
|
-
override createNode(
|
|
41
|
-
return new AVLTreeNode<N['val'], N>(
|
|
40
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
41
|
+
return new AVLTreeNode<N['val'], N>(key, val) as N;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
46
|
-
* @param {
|
|
46
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier of the binary tree node that we want to add.
|
|
47
47
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
48
48
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
49
49
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
50
50
|
*/
|
|
51
|
-
override add(
|
|
51
|
+
override add(key: BinaryTreeNodeKey, val?: N['val']): N | null | undefined {
|
|
52
52
|
// TODO support node as a param
|
|
53
|
-
const inserted = super.add(
|
|
53
|
+
const inserted = super.add(key, val);
|
|
54
54
|
if (inserted) this._balancePath(inserted);
|
|
55
55
|
return inserted;
|
|
56
56
|
}
|
|
@@ -58,12 +58,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
58
58
|
/**
|
|
59
59
|
* The function overrides the remove method of a binary tree and performs additional operations to balance the tree after
|
|
60
60
|
* deletion.
|
|
61
|
-
* @param {
|
|
61
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
62
62
|
* removed.
|
|
63
63
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
64
64
|
*/
|
|
65
|
-
override remove(
|
|
66
|
-
const deletedResults = super.remove(
|
|
65
|
+
override remove(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
66
|
+
const deletedResults = super.remove(key);
|
|
67
67
|
for (const {needBalanced} of deletedResults) {
|
|
68
68
|
if (needBalanced) {
|
|
69
69
|
this._balancePath(needBalanced);
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
|
|
10
10
|
import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
|
|
11
11
|
import {IBinaryTree, IBinaryTreeNode} from '../../interfaces';
|
|
12
12
|
|
|
@@ -14,8 +14,8 @@ export class BinaryTreeNode<V = any, NEIGHBOR extends BinaryTreeNode<V, NEIGHBOR
|
|
|
14
14
|
extends AbstractBinaryTreeNode<V, NEIGHBOR>
|
|
15
15
|
implements IBinaryTreeNode<V, NEIGHBOR>
|
|
16
16
|
{
|
|
17
|
-
constructor(
|
|
18
|
-
super(
|
|
17
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
18
|
+
super(key, val);
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -35,13 +35,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* The function creates a new binary tree node with an optional value.
|
|
38
|
-
* @param {
|
|
39
|
-
* `
|
|
38
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is of type
|
|
39
|
+
* `BinaryTreeNodeKey`, which represents the unique identifier for each node in the binary tree.
|
|
40
40
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
41
41
|
* stored in the node.
|
|
42
|
-
* @returns a new instance of a BinaryTreeNode with the specified
|
|
42
|
+
* @returns a new instance of a BinaryTreeNode with the specified key and value.
|
|
43
43
|
*/
|
|
44
|
-
createNode(
|
|
45
|
-
return new BinaryTreeNode<N['val'], N>(
|
|
44
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
45
|
+
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
BinaryTreeNodeKey,
|
|
10
|
+
BinaryTreeNodePropertyName,
|
|
11
|
+
BSTComparator,
|
|
12
|
+
BSTNodeNested,
|
|
13
|
+
BSTOptions
|
|
14
|
+
} from '../../types';
|
|
9
15
|
import {CP, LoopType} from '../../types';
|
|
10
16
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
11
17
|
import {IBST, IBSTNode} from '../../interfaces';
|
|
@@ -14,8 +20,8 @@ export class BSTNode<V = any, NEIGHBOR extends BSTNode<V, NEIGHBOR> = BSTNodeNes
|
|
|
14
20
|
extends BinaryTreeNode<V, NEIGHBOR>
|
|
15
21
|
implements IBSTNode<V, NEIGHBOR>
|
|
16
22
|
{
|
|
17
|
-
constructor(
|
|
18
|
-
super(
|
|
23
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
24
|
+
super(key, val);
|
|
19
25
|
}
|
|
20
26
|
}
|
|
21
27
|
|
|
@@ -35,35 +41,35 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
/**
|
|
38
|
-
* The function creates a new binary search tree node with the given
|
|
39
|
-
* @param {
|
|
44
|
+
* The function creates a new binary search tree node with the given key and value.
|
|
45
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
40
46
|
* identify each node in the binary tree.
|
|
41
47
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
42
48
|
* that will be stored in the node.
|
|
43
|
-
* @returns a new instance of the BSTNode class with the specified
|
|
49
|
+
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
44
50
|
*/
|
|
45
|
-
override createNode(
|
|
46
|
-
return new BSTNode<N['val'], N>(
|
|
51
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
52
|
+
return new BSTNode<N['val'], N>(key, val) as N;
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
/**
|
|
50
56
|
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
|
51
57
|
* node with the same ID.
|
|
52
|
-
* @param {
|
|
58
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
|
53
59
|
* (which represents a binary tree node) or `null`.
|
|
54
60
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
55
61
|
* being added to the binary search tree.
|
|
56
62
|
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
|
57
63
|
*/
|
|
58
|
-
override add(
|
|
64
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
59
65
|
// TODO support node as a param
|
|
60
66
|
let inserted: N | null = null;
|
|
61
67
|
let newNode: N | null = null;
|
|
62
|
-
if (
|
|
63
|
-
newNode =
|
|
64
|
-
} else if (typeof
|
|
65
|
-
newNode = this.createNode(
|
|
66
|
-
} else if (
|
|
68
|
+
if (keyOrNode instanceof BSTNode) {
|
|
69
|
+
newNode = keyOrNode;
|
|
70
|
+
} else if (typeof keyOrNode === 'number') {
|
|
71
|
+
newNode = this.createNode(keyOrNode, val);
|
|
72
|
+
} else if (keyOrNode === null) {
|
|
67
73
|
newNode = null;
|
|
68
74
|
}
|
|
69
75
|
if (this.root === null) {
|
|
@@ -75,14 +81,14 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
75
81
|
let traversing = true;
|
|
76
82
|
while (traversing) {
|
|
77
83
|
if (cur !== null && newNode !== null) {
|
|
78
|
-
if (this._compare(cur.
|
|
84
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
79
85
|
if (newNode) {
|
|
80
86
|
cur.val = newNode.val;
|
|
81
87
|
}
|
|
82
88
|
//Duplicates are not accepted.
|
|
83
89
|
traversing = false;
|
|
84
90
|
inserted = cur;
|
|
85
|
-
} else if (this._compare(cur.
|
|
91
|
+
} else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
86
92
|
// Traverse left of the node
|
|
87
93
|
if (cur.left === undefined) {
|
|
88
94
|
if (newNode) {
|
|
@@ -97,7 +103,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
97
103
|
//Traverse the left of the current node
|
|
98
104
|
if (cur.left) cur = cur.left;
|
|
99
105
|
}
|
|
100
|
-
} else if (this._compare(cur.
|
|
106
|
+
} else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
101
107
|
// Traverse right of the node
|
|
102
108
|
if (cur.right === undefined) {
|
|
103
109
|
if (newNode) {
|
|
@@ -124,48 +130,50 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
124
130
|
/**
|
|
125
131
|
* The `addMany` function overrides the base class method to add multiple nodes to a binary search tree in a balanced
|
|
126
132
|
* manner.
|
|
127
|
-
* @param {[
|
|
128
|
-
* `
|
|
133
|
+
* @param {[BinaryTreeNodeKey | N , N['val']][]} idsOrNodes - The `idsOrNodes` parameter in the `addMany` function is an array of
|
|
134
|
+
* `BinaryTreeNodeKey` or `N` (node) objects, or `null` values. It represents the nodes or node IDs that need to be added
|
|
129
135
|
* to the binary search tree.
|
|
130
136
|
* @param {N['val'][]} data - The values of tree nodes
|
|
131
137
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
132
138
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
133
139
|
*/
|
|
134
140
|
override addMany(
|
|
135
|
-
idsOrNodes: (
|
|
141
|
+
idsOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
136
142
|
data?: N['val'][],
|
|
137
143
|
isBalanceAdd = false
|
|
138
144
|
): (N | null | undefined)[] {
|
|
139
|
-
function hasNoNull(arr: (
|
|
145
|
+
function hasNoNull(arr: (BinaryTreeNodeKey | null)[] | (N | null)[]): arr is BinaryTreeNodeKey[] | N[] {
|
|
140
146
|
return arr.indexOf(null) === -1;
|
|
141
147
|
}
|
|
142
148
|
if (!isBalanceAdd || !hasNoNull(idsOrNodes)) {
|
|
143
149
|
return super.addMany(idsOrNodes, data);
|
|
144
150
|
}
|
|
145
151
|
const inserted: (N | null | undefined)[] = [];
|
|
146
|
-
const combinedArr: [
|
|
152
|
+
const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = idsOrNodes.map((value, index) => [value, data?.[index]]);
|
|
147
153
|
let sorted = [];
|
|
148
|
-
function isNodeOrNullTuple(arr: [
|
|
149
|
-
for (const [
|
|
154
|
+
function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
|
|
155
|
+
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
150
156
|
return false;
|
|
151
157
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
158
|
+
function isBinaryTreeKeyOrNullTuple(
|
|
159
|
+
arr: [BinaryTreeNodeKey | N, N['val']][]
|
|
160
|
+
): arr is [BinaryTreeNodeKey, N['val']][] {
|
|
161
|
+
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
154
162
|
return false;
|
|
155
163
|
}
|
|
156
|
-
let
|
|
164
|
+
let sortedKeysOrNodes: (number | N | null)[] = [],
|
|
157
165
|
sortedData: (N['val'] | undefined)[] | undefined = [];
|
|
158
166
|
|
|
159
167
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
160
|
-
sorted = combinedArr.sort((a, b) => a[0].
|
|
161
|
-
} else if (
|
|
168
|
+
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
169
|
+
} else if (isBinaryTreeKeyOrNullTuple(combinedArr)) {
|
|
162
170
|
sorted = combinedArr.sort((a, b) => a[0] - b[0]);
|
|
163
171
|
} else {
|
|
164
172
|
throw new Error('Invalid input idsOrNodes');
|
|
165
173
|
}
|
|
166
|
-
|
|
174
|
+
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
167
175
|
sortedData = sorted.map(([, val]) => val);
|
|
168
|
-
const recursive = (arr: (
|
|
176
|
+
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => {
|
|
169
177
|
if (arr.length === 0) return;
|
|
170
178
|
|
|
171
179
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -183,7 +191,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
183
191
|
const [l, r] = popped;
|
|
184
192
|
if (l <= r) {
|
|
185
193
|
const m = l + Math.floor((r - l) / 2);
|
|
186
|
-
const newNode = this.add(
|
|
194
|
+
const newNode = this.add(sortedKeysOrNodes[m], sortedData?.[m]);
|
|
187
195
|
inserted.push(newNode);
|
|
188
196
|
stack.push([m + 1, r]);
|
|
189
197
|
stack.push([l, m - 1]);
|
|
@@ -192,7 +200,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
192
200
|
}
|
|
193
201
|
};
|
|
194
202
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
195
|
-
recursive(
|
|
203
|
+
recursive(sortedKeysOrNodes, sortedData);
|
|
196
204
|
} else {
|
|
197
205
|
iterative();
|
|
198
206
|
}
|
|
@@ -202,44 +210,44 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
202
210
|
|
|
203
211
|
/**
|
|
204
212
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
205
|
-
* @param {
|
|
213
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
206
214
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
207
215
|
* @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 `'
|
|
209
|
-
* @returns The method is returning either a
|
|
216
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
|
|
217
|
+
* @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
|
|
210
218
|
*/
|
|
211
|
-
override get(nodeProperty:
|
|
212
|
-
propertyName = propertyName ?? '
|
|
219
|
+
override get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null {
|
|
220
|
+
propertyName = propertyName ?? 'key';
|
|
213
221
|
return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
|
|
214
222
|
}
|
|
215
223
|
|
|
216
224
|
/**
|
|
217
|
-
* The function returns the
|
|
218
|
-
* leftmost node if the comparison is greater than, and the
|
|
219
|
-
* @returns The method `lastKey()` returns the
|
|
220
|
-
* the values at index 0 and 1 is less than, otherwise it returns the
|
|
221
|
-
* equal, it returns the
|
|
225
|
+
* The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
|
|
226
|
+
* leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
|
|
227
|
+
* @returns The method `lastKey()` returns the key of the rightmost node in the binary tree if the comparison between
|
|
228
|
+
* the values at index 0 and 1 is less than, otherwise it returns the key of the leftmost node. If the comparison is
|
|
229
|
+
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
222
230
|
*/
|
|
223
|
-
lastKey():
|
|
224
|
-
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.
|
|
225
|
-
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.
|
|
226
|
-
else return this.getRightMost()?.
|
|
231
|
+
lastKey(): BinaryTreeNodeKey {
|
|
232
|
+
if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.key ?? 0;
|
|
233
|
+
else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.key ?? 0;
|
|
234
|
+
else return this.getRightMost()?.key ?? 0;
|
|
227
235
|
}
|
|
228
236
|
|
|
229
237
|
/**
|
|
230
238
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
231
|
-
* @param {
|
|
239
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
232
240
|
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
233
241
|
* @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 `'
|
|
242
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
|
|
235
243
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
236
244
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
237
245
|
* is set to `true`, the function will return an array with only one node (if
|
|
238
246
|
* @returns an array of nodes (type N).
|
|
239
247
|
*/
|
|
240
248
|
override getNodes(
|
|
241
|
-
nodeProperty:
|
|
242
|
-
propertyName: BinaryTreeNodePropertyName = '
|
|
249
|
+
nodeProperty: BinaryTreeNodeKey | N,
|
|
250
|
+
propertyName: BinaryTreeNodePropertyName = 'key',
|
|
243
251
|
onlyOne?: boolean
|
|
244
252
|
): N[] {
|
|
245
253
|
if (!this.root) return [];
|
|
@@ -250,9 +258,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
250
258
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
|
|
251
259
|
|
|
252
260
|
if (!cur.left && !cur.right) return;
|
|
253
|
-
if (propertyName === '
|
|
254
|
-
if (this._compare(cur.
|
|
255
|
-
if (this._compare(cur.
|
|
261
|
+
if (propertyName === 'key') {
|
|
262
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
|
|
263
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
|
|
256
264
|
} else {
|
|
257
265
|
cur.left && _traverse(cur.left);
|
|
258
266
|
cur.right && _traverse(cur.right);
|
|
@@ -266,9 +274,9 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
266
274
|
const cur = queue.shift();
|
|
267
275
|
if (cur) {
|
|
268
276
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
|
|
269
|
-
if (propertyName === '
|
|
270
|
-
if (this._compare(cur.
|
|
271
|
-
if (this._compare(cur.
|
|
277
|
+
if (propertyName === 'key') {
|
|
278
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
|
|
279
|
+
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
|
|
272
280
|
} else {
|
|
273
281
|
cur.left && queue.push(cur.left);
|
|
274
282
|
cur.right && queue.push(cur.right);
|
|
@@ -284,26 +292,26 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
284
292
|
/**
|
|
285
293
|
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
286
294
|
* less than a given node.
|
|
287
|
-
* @param {N |
|
|
295
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
288
296
|
* @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 `'
|
|
297
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'key'`.
|
|
290
298
|
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
291
299
|
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
292
300
|
*/
|
|
293
|
-
lesserSum(beginNode: N |
|
|
294
|
-
propertyName = propertyName ?? '
|
|
295
|
-
if (typeof beginNode === 'number') beginNode = this.get(beginNode, '
|
|
301
|
+
lesserSum(beginNode: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number {
|
|
302
|
+
propertyName = propertyName ?? 'key';
|
|
303
|
+
if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'key');
|
|
296
304
|
if (!beginNode) return 0;
|
|
297
305
|
if (!this.root) return 0;
|
|
298
|
-
const
|
|
306
|
+
const key = beginNode.key;
|
|
299
307
|
const getSumByPropertyName = (cur: N) => {
|
|
300
308
|
let needSum: number;
|
|
301
309
|
switch (propertyName) {
|
|
302
|
-
case '
|
|
303
|
-
needSum = cur.
|
|
310
|
+
case 'key':
|
|
311
|
+
needSum = cur.key;
|
|
304
312
|
break;
|
|
305
313
|
default:
|
|
306
|
-
needSum = cur.
|
|
314
|
+
needSum = cur.key;
|
|
307
315
|
break;
|
|
308
316
|
}
|
|
309
317
|
return needSum;
|
|
@@ -313,7 +321,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
313
321
|
|
|
314
322
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
315
323
|
const _traverse = (cur: N): void => {
|
|
316
|
-
const compared = this._compare(cur.
|
|
324
|
+
const compared = this._compare(cur.key, key);
|
|
317
325
|
if (compared === CP.eq) {
|
|
318
326
|
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
319
327
|
return;
|
|
@@ -334,7 +342,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
334
342
|
while (queue.length > 0) {
|
|
335
343
|
const cur = queue.shift();
|
|
336
344
|
if (cur) {
|
|
337
|
-
const compared = this._compare(cur.
|
|
345
|
+
const compared = this._compare(cur.key, key);
|
|
338
346
|
if (compared === CP.eq) {
|
|
339
347
|
if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
|
|
340
348
|
return sum;
|
|
@@ -358,44 +366,44 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
358
366
|
/**
|
|
359
367
|
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
360
368
|
* have a greater value than a given node.
|
|
361
|
-
* @param {N |
|
|
362
|
-
* `
|
|
369
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
370
|
+
* `BinaryTreeNodeKey`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
363
371
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
364
372
|
* each greater node should be increased.
|
|
365
373
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
366
374
|
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
367
|
-
* '
|
|
375
|
+
* 'key'.
|
|
368
376
|
* @returns a boolean value.
|
|
369
377
|
*/
|
|
370
378
|
allGreaterNodesAdd(
|
|
371
|
-
node: N |
|
|
379
|
+
node: N | BinaryTreeNodeKey | null,
|
|
372
380
|
delta: number,
|
|
373
381
|
propertyName?: BinaryTreeNodePropertyName
|
|
374
382
|
): boolean {
|
|
375
|
-
propertyName = propertyName ?? '
|
|
376
|
-
if (typeof node === 'number') node = this.get(node, '
|
|
383
|
+
propertyName = propertyName ?? 'key';
|
|
384
|
+
if (typeof node === 'number') node = this.get(node, 'key');
|
|
377
385
|
if (!node) return false;
|
|
378
|
-
const
|
|
386
|
+
const key = node.key;
|
|
379
387
|
if (!this.root) return false;
|
|
380
388
|
|
|
381
389
|
const _sumByPropertyName = (cur: N) => {
|
|
382
390
|
switch (propertyName) {
|
|
383
|
-
case '
|
|
384
|
-
cur.
|
|
391
|
+
case 'key':
|
|
392
|
+
cur.key += delta;
|
|
385
393
|
break;
|
|
386
394
|
default:
|
|
387
|
-
cur.
|
|
395
|
+
cur.key += delta;
|
|
388
396
|
break;
|
|
389
397
|
}
|
|
390
398
|
};
|
|
391
399
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
392
400
|
const _traverse = (cur: N) => {
|
|
393
|
-
const compared = this._compare(cur.
|
|
401
|
+
const compared = this._compare(cur.key, key);
|
|
394
402
|
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
395
403
|
|
|
396
404
|
if (!cur.left && !cur.right) return;
|
|
397
|
-
if (cur.left && this._compare(cur.left.
|
|
398
|
-
if (cur.right && this._compare(cur.right.
|
|
405
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) _traverse(cur.left);
|
|
406
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) _traverse(cur.right);
|
|
399
407
|
};
|
|
400
408
|
|
|
401
409
|
_traverse(this.root);
|
|
@@ -405,11 +413,11 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
405
413
|
while (queue.length > 0) {
|
|
406
414
|
const cur = queue.shift();
|
|
407
415
|
if (cur) {
|
|
408
|
-
const compared = this._compare(cur.
|
|
416
|
+
const compared = this._compare(cur.key, key);
|
|
409
417
|
if (compared === CP.gt) _sumByPropertyName(cur);
|
|
410
418
|
|
|
411
|
-
if (cur.left && this._compare(cur.left.
|
|
412
|
-
if (cur.right && this._compare(cur.right.
|
|
419
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt) queue.push(cur.left);
|
|
420
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt) queue.push(cur.right);
|
|
413
421
|
}
|
|
414
422
|
}
|
|
415
423
|
return true;
|
|
@@ -442,7 +450,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
442
450
|
if (l > r) return;
|
|
443
451
|
const m = l + Math.floor((r - l) / 2);
|
|
444
452
|
const midNode = sorted[m];
|
|
445
|
-
this.add(midNode.
|
|
453
|
+
this.add(midNode.key, midNode.val);
|
|
446
454
|
buildBalanceBST(l, m - 1);
|
|
447
455
|
buildBalanceBST(m + 1, r);
|
|
448
456
|
};
|
|
@@ -458,7 +466,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
458
466
|
if (l <= r) {
|
|
459
467
|
const m = l + Math.floor((r - l) / 2);
|
|
460
468
|
const midNode = sorted[m];
|
|
461
|
-
this.add(midNode.
|
|
469
|
+
this.add(midNode.key, midNode.val);
|
|
462
470
|
stack.push([m + 1, r]);
|
|
463
471
|
stack.push([l, m - 1]);
|
|
464
472
|
}
|
|
@@ -521,12 +529,12 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
521
529
|
/**
|
|
522
530
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|
|
523
531
|
* greater than, less than, or equal to the second ID.
|
|
524
|
-
* @param {
|
|
525
|
-
* @param {
|
|
532
|
+
* @param {BinaryTreeNodeKey} a - a is a BinaryTreeNodeKey, which represents the identifier of a binary tree node.
|
|
533
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code refers to a BinaryTreeNodeKey.
|
|
526
534
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
|
|
527
535
|
* than), or CP.eq (equal).
|
|
528
536
|
*/
|
|
529
|
-
protected _compare(a:
|
|
537
|
+
protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP {
|
|
530
538
|
const compared = this._comparator(a, b);
|
|
531
539
|
if (compared > 0) return CP.gt;
|
|
532
540
|
else if (compared < 0) return CP.lt;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
|
|
2
2
|
import {IRBTree, IRBTreeNode} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
@@ -8,8 +8,8 @@ export class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTr
|
|
|
8
8
|
{
|
|
9
9
|
private _color: RBColor;
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
12
|
-
super(
|
|
11
|
+
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
12
|
+
super(key, val);
|
|
13
13
|
this._color = RBColor.RED;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -27,12 +27,12 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
override createNode(
|
|
31
|
-
return new RBTreeNode(
|
|
30
|
+
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
31
|
+
return new RBTreeNode(key, val) as N;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
// override add(
|
|
35
|
-
// const inserted = super.add(
|
|
34
|
+
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
35
|
+
// const inserted = super.add(keyOrNode, val);
|
|
36
36
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
37
|
// return inserted;
|
|
38
38
|
// }
|
|
@@ -205,8 +205,8 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|
|
205
205
|
// node.right = null;
|
|
206
206
|
// }
|
|
207
207
|
//
|
|
208
|
-
// override remove(
|
|
209
|
-
// const node = this.get(
|
|
208
|
+
// override remove(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
|
|
209
|
+
// const node = this.get(nodeOrKey);
|
|
210
210
|
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
|
|
211
211
|
// if (!node) return result; // Node does not exist
|
|
212
212
|
//
|