min-heap-typed 1.50.2 → 1.50.4
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/base/iterable-base.d.ts +6 -0
- package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +49 -11
- package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/data-structures/binary-tree/avl-tree.js +33 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/data-structures/binary-tree/bst.js +51 -20
- package/dist/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/data-structures/binary-tree/index.js +2 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/data-structures/binary-tree/rb-tree.js +90 -24
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/data-structures/graph/abstract-graph.js +0 -189
- package/dist/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/data-structures/graph/directed-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/data-structures/graph/undirected-graph.js +126 -18
- package/dist/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/data-structures/hash/hash-map.js +196 -62
- package/dist/data-structures/heap/heap.d.ts +29 -19
- package/dist/data-structures/heap/heap.js +29 -20
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix.js +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +95 -21
- package/dist/data-structures/queue/deque.js +100 -16
- package/dist/data-structures/queue/queue.d.ts +65 -45
- package/dist/data-structures/queue/queue.js +65 -45
- package/dist/data-structures/stack/stack.d.ts +36 -22
- package/dist/data-structures/stack/stack.js +36 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +100 -36
- package/dist/data-structures/trie/trie.js +115 -36
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.js +2 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +51 -19
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +99 -28
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
|
@@ -11,18 +11,37 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
11
11
|
this._left = undefined;
|
|
12
12
|
this._right = undefined;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* The function returns the value of the `_left` property.
|
|
16
|
+
* @returns The `_left` property of the current object is being returned.
|
|
17
|
+
*/
|
|
14
18
|
get left() {
|
|
15
19
|
return this._left;
|
|
16
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
|
|
24
|
+
* instance of the `NODE` class or `undefined`.
|
|
25
|
+
*/
|
|
17
26
|
set left(v) {
|
|
18
27
|
if (v) {
|
|
19
28
|
v.parent = this;
|
|
20
29
|
}
|
|
21
30
|
this._left = v;
|
|
22
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
34
|
+
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
35
|
+
* `undefined`.
|
|
36
|
+
*/
|
|
23
37
|
get right() {
|
|
24
38
|
return this._right;
|
|
25
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* The function sets the right child of a node and updates the parent reference of the child.
|
|
42
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
|
|
43
|
+
* `NODE` object or `undefined`.
|
|
44
|
+
*/
|
|
26
45
|
set right(v) {
|
|
27
46
|
if (v) {
|
|
28
47
|
v.parent = this;
|
|
@@ -42,10 +61,10 @@ exports.BSTNode = BSTNode;
|
|
|
42
61
|
*/
|
|
43
62
|
class BST extends binary_tree_1.BinaryTree {
|
|
44
63
|
/**
|
|
45
|
-
* This is the constructor function for a
|
|
46
|
-
*
|
|
47
|
-
* @param
|
|
48
|
-
* binary search tree.
|
|
64
|
+
* This is the constructor function for a TypeScript class that initializes a binary search tree with
|
|
65
|
+
* optional keys or nodes or entries and options.
|
|
66
|
+
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
|
|
67
|
+
* to initialize the binary search tree with the provided keys, nodes, or entries.
|
|
49
68
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
50
69
|
* configuration options for the binary search tree. It can have the following properties:
|
|
51
70
|
*/
|
|
@@ -61,19 +80,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
61
80
|
if (keysOrNodesOrEntries)
|
|
62
81
|
this.addMany(keysOrNodesOrEntries);
|
|
63
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* The function returns the root node of a tree structure.
|
|
85
|
+
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
86
|
+
*/
|
|
64
87
|
get root() {
|
|
65
88
|
return this._root;
|
|
66
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* The function returns the value of the _variant property.
|
|
92
|
+
* @returns The value of the `_variant` property.
|
|
93
|
+
*/
|
|
67
94
|
get variant() {
|
|
68
95
|
return this._variant;
|
|
69
96
|
}
|
|
70
97
|
/**
|
|
71
|
-
* The function creates a new
|
|
72
|
-
* @param {K} key - The key parameter is the
|
|
73
|
-
*
|
|
74
|
-
* @param [value] - The parameter
|
|
75
|
-
*
|
|
76
|
-
* @returns a new instance of the BSTNode class
|
|
98
|
+
* The function creates a new BSTNode with the given key and value and returns it.
|
|
99
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
100
|
+
* being created.
|
|
101
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
102
|
+
* value associated with the key in the node being created.
|
|
103
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
77
104
|
*/
|
|
78
105
|
createNode(key, value) {
|
|
79
106
|
return new BSTNode(key, value);
|
|
@@ -81,9 +108,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
81
108
|
/**
|
|
82
109
|
* The function creates a new binary search tree with the specified options.
|
|
83
110
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
84
|
-
* behavior of the `createTree` method. It
|
|
85
|
-
*
|
|
86
|
-
* @returns a new instance of the BST class with the
|
|
111
|
+
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
|
|
112
|
+
* partial object of type `BSTOptions<K>`.
|
|
113
|
+
* @returns a new instance of the BST class, with the provided options merged with the default
|
|
114
|
+
* options. The returned value is casted as TREE.
|
|
87
115
|
*/
|
|
88
116
|
createTree(options) {
|
|
89
117
|
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
@@ -596,11 +624,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
596
624
|
const compared = this._compare(cur.key, targetKey);
|
|
597
625
|
if (compared === lesserOrGreater)
|
|
598
626
|
ans.push(callback(cur));
|
|
599
|
-
if (
|
|
600
|
-
return;
|
|
601
|
-
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
|
|
627
|
+
if (this.isRealNode(cur.left))
|
|
602
628
|
_traverse(cur.left);
|
|
603
|
-
if (
|
|
629
|
+
if (this.isRealNode(cur.right))
|
|
604
630
|
_traverse(cur.right);
|
|
605
631
|
};
|
|
606
632
|
_traverse(this.root);
|
|
@@ -610,13 +636,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
610
636
|
const queue = new queue_1.Queue([this.root]);
|
|
611
637
|
while (queue.size > 0) {
|
|
612
638
|
const cur = queue.shift();
|
|
613
|
-
if (cur) {
|
|
639
|
+
if (this.isRealNode(cur)) {
|
|
614
640
|
const compared = this._compare(cur.key, targetKey);
|
|
615
641
|
if (compared === lesserOrGreater)
|
|
616
642
|
ans.push(callback(cur));
|
|
617
|
-
if (
|
|
643
|
+
if (this.isRealNode(cur.left))
|
|
618
644
|
queue.push(cur.left);
|
|
619
|
-
if (
|
|
645
|
+
if (this.isRealNode(cur.right))
|
|
620
646
|
queue.push(cur.right);
|
|
621
647
|
}
|
|
622
648
|
}
|
|
@@ -742,6 +768,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
742
768
|
}
|
|
743
769
|
return balanced;
|
|
744
770
|
}
|
|
771
|
+
/**
|
|
772
|
+
* The function sets the root property of an object and updates the parent property of the new root.
|
|
773
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
|
|
774
|
+
* can either be an object of type `NODE` or it can be `undefined`.
|
|
775
|
+
*/
|
|
745
776
|
_setRoot(v) {
|
|
746
777
|
if (v) {
|
|
747
778
|
v.parent = undefined;
|
|
@@ -20,4 +20,5 @@ __exportStar(require("./binary-indexed-tree"), exports);
|
|
|
20
20
|
__exportStar(require("./segment-tree"), exports);
|
|
21
21
|
__exportStar(require("./avl-tree"), exports);
|
|
22
22
|
__exportStar(require("./rb-tree"), exports);
|
|
23
|
-
__exportStar(require("./tree-
|
|
23
|
+
__exportStar(require("./avl-tree-multi-map"), exports);
|
|
24
|
+
__exportStar(require("./tree-multi-map"), exports);
|
|
@@ -9,8 +9,29 @@ import { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, R
|
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
14
|
+
* color.
|
|
15
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
16
|
+
* Red-Black Tree.
|
|
17
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
18
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
19
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
20
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
21
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
22
|
+
*/
|
|
13
23
|
constructor(key: K, value?: V, color?: RBTNColor);
|
|
24
|
+
protected _color: RBTNColor;
|
|
25
|
+
/**
|
|
26
|
+
* The function returns the color value of a variable.
|
|
27
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
28
|
+
*/
|
|
29
|
+
get color(): RBTNColor;
|
|
30
|
+
/**
|
|
31
|
+
* The function sets the color property to the specified value.
|
|
32
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
33
|
+
*/
|
|
34
|
+
set color(value: RBTNColor);
|
|
14
35
|
}
|
|
15
36
|
/**
|
|
16
37
|
* 1. Each node is either red or black.
|
|
@@ -20,7 +41,6 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
20
41
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
42
|
*/
|
|
22
43
|
export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
23
|
-
Sentinel: NODE;
|
|
24
44
|
/**
|
|
25
45
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
46
|
* initializes the tree with optional nodes and options.
|
|
@@ -33,9 +53,23 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
33
53
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
54
|
*/
|
|
35
55
|
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K>);
|
|
56
|
+
protected _Sentinel: NODE;
|
|
57
|
+
/**
|
|
58
|
+
* The function returns the value of the `_Sentinel` property.
|
|
59
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
60
|
+
*/
|
|
61
|
+
get Sentinel(): NODE;
|
|
36
62
|
protected _root: NODE;
|
|
63
|
+
/**
|
|
64
|
+
* The function returns the root node.
|
|
65
|
+
* @returns The root node of the data structure.
|
|
66
|
+
*/
|
|
37
67
|
get root(): NODE;
|
|
38
68
|
protected _size: number;
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the size of an object.
|
|
71
|
+
* @returns The size of the object, which is a number.
|
|
72
|
+
*/
|
|
39
73
|
get size(): number;
|
|
40
74
|
/**
|
|
41
75
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
@@ -74,6 +108,12 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
74
108
|
* class.
|
|
75
109
|
*/
|
|
76
110
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
111
|
+
/**
|
|
112
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
113
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
114
|
+
* it can either be of type `NODE` or `undefined`.
|
|
115
|
+
* @returns a boolean value.
|
|
116
|
+
*/
|
|
77
117
|
isRealNode(node: NODE | undefined): node is NODE;
|
|
78
118
|
/**
|
|
79
119
|
* Time Complexity: O(log n)
|
|
@@ -143,6 +183,12 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
143
183
|
* Time Complexity: O(1)
|
|
144
184
|
* Space Complexity: O(1)
|
|
145
185
|
*/
|
|
186
|
+
/**
|
|
187
|
+
* Time Complexity: O(1)
|
|
188
|
+
* Space Complexity: O(1)
|
|
189
|
+
*
|
|
190
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
191
|
+
*/
|
|
146
192
|
clear(): void;
|
|
147
193
|
/**
|
|
148
194
|
* Time Complexity: O(log n)
|
|
@@ -158,6 +204,12 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
158
204
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
159
205
|
*/
|
|
160
206
|
getPredecessor(x: NODE): NODE;
|
|
207
|
+
/**
|
|
208
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
209
|
+
* root node.
|
|
210
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
211
|
+
* structure.
|
|
212
|
+
*/
|
|
161
213
|
protected _setRoot(v: NODE): void;
|
|
162
214
|
/**
|
|
163
215
|
* Time Complexity: O(1)
|
|
@@ -11,9 +11,34 @@ exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
|
11
11
|
const types_1 = require("../../types");
|
|
12
12
|
const bst_1 = require("./bst");
|
|
13
13
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
16
|
+
* color.
|
|
17
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
18
|
+
* Red-Black Tree.
|
|
19
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
20
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
21
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
22
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
23
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
24
|
+
*/
|
|
14
25
|
constructor(key, value, color = types_1.RBTNColor.BLACK) {
|
|
15
26
|
super(key, value);
|
|
16
|
-
this.
|
|
27
|
+
this._color = color;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The function returns the color value of a variable.
|
|
31
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
32
|
+
*/
|
|
33
|
+
get color() {
|
|
34
|
+
return this._color;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The function sets the color property to the specified value.
|
|
38
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
39
|
+
*/
|
|
40
|
+
set color(value) {
|
|
41
|
+
this._color = value;
|
|
17
42
|
}
|
|
18
43
|
}
|
|
19
44
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
@@ -38,15 +63,30 @@ class RedBlackTree extends bst_1.BST {
|
|
|
38
63
|
*/
|
|
39
64
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
40
65
|
super([], options);
|
|
41
|
-
this.
|
|
66
|
+
this._Sentinel = new RedBlackTreeNode(NaN);
|
|
42
67
|
this._size = 0;
|
|
43
|
-
this._root = this.
|
|
68
|
+
this._root = this._Sentinel;
|
|
44
69
|
if (keysOrNodesOrEntries)
|
|
45
70
|
super.addMany(keysOrNodesOrEntries);
|
|
46
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* The function returns the value of the `_Sentinel` property.
|
|
74
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
75
|
+
*/
|
|
76
|
+
get Sentinel() {
|
|
77
|
+
return this._Sentinel;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The function returns the root node.
|
|
81
|
+
* @returns The root node of the data structure.
|
|
82
|
+
*/
|
|
47
83
|
get root() {
|
|
48
84
|
return this._root;
|
|
49
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* The function returns the size of an object.
|
|
88
|
+
* @returns The size of the object, which is a number.
|
|
89
|
+
*/
|
|
50
90
|
get size() {
|
|
51
91
|
return this._size;
|
|
52
92
|
}
|
|
@@ -117,8 +157,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
117
157
|
isNode(keyOrNodeOrEntry) {
|
|
118
158
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
119
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
162
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
163
|
+
* it can either be of type `NODE` or `undefined`.
|
|
164
|
+
* @returns a boolean value.
|
|
165
|
+
*/
|
|
120
166
|
isRealNode(node) {
|
|
121
|
-
if (node === this.
|
|
167
|
+
if (node === this._Sentinel || node === undefined)
|
|
122
168
|
return false;
|
|
123
169
|
return node instanceof RedBlackTreeNode;
|
|
124
170
|
}
|
|
@@ -143,11 +189,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
143
189
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
144
190
|
if (newNode === undefined)
|
|
145
191
|
return false;
|
|
146
|
-
newNode.left = this.
|
|
147
|
-
newNode.right = this.
|
|
192
|
+
newNode.left = this._Sentinel;
|
|
193
|
+
newNode.right = this._Sentinel;
|
|
148
194
|
let y = undefined;
|
|
149
195
|
let x = this.root;
|
|
150
|
-
while (x !== this.
|
|
196
|
+
while (x !== this._Sentinel) {
|
|
151
197
|
y = x;
|
|
152
198
|
if (x) {
|
|
153
199
|
if (newNode.key < x.key) {
|
|
@@ -211,9 +257,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
211
257
|
if (identifier === null)
|
|
212
258
|
return ans;
|
|
213
259
|
const helper = (node) => {
|
|
214
|
-
let z = this.
|
|
260
|
+
let z = this._Sentinel;
|
|
215
261
|
let x, y;
|
|
216
|
-
while (node !== this.
|
|
262
|
+
while (node !== this._Sentinel) {
|
|
217
263
|
if (node && callback(node) === identifier) {
|
|
218
264
|
z = node;
|
|
219
265
|
}
|
|
@@ -224,17 +270,17 @@ class RedBlackTree extends bst_1.BST {
|
|
|
224
270
|
node = node === null || node === void 0 ? void 0 : node.left;
|
|
225
271
|
}
|
|
226
272
|
}
|
|
227
|
-
if (z === this.
|
|
273
|
+
if (z === this._Sentinel) {
|
|
228
274
|
this._size--;
|
|
229
275
|
return;
|
|
230
276
|
}
|
|
231
277
|
y = z;
|
|
232
278
|
let yOriginalColor = y.color;
|
|
233
|
-
if (z.left === this.
|
|
279
|
+
if (z.left === this._Sentinel) {
|
|
234
280
|
x = z.right;
|
|
235
281
|
this._rbTransplant(z, z.right);
|
|
236
282
|
}
|
|
237
|
-
else if (z.right === this.
|
|
283
|
+
else if (z.right === this._Sentinel) {
|
|
238
284
|
x = z.left;
|
|
239
285
|
this._rbTransplant(z, z.left);
|
|
240
286
|
}
|
|
@@ -300,8 +346,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
300
346
|
* Time Complexity: O(1)
|
|
301
347
|
* Space Complexity: O(1)
|
|
302
348
|
*/
|
|
349
|
+
/**
|
|
350
|
+
* Time Complexity: O(1)
|
|
351
|
+
* Space Complexity: O(1)
|
|
352
|
+
*
|
|
353
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
354
|
+
*/
|
|
303
355
|
clear() {
|
|
304
|
-
this._root = this.
|
|
356
|
+
this._root = this._Sentinel;
|
|
305
357
|
this._size = 0;
|
|
306
358
|
}
|
|
307
359
|
/**
|
|
@@ -328,6 +380,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
328
380
|
}
|
|
329
381
|
return y;
|
|
330
382
|
}
|
|
383
|
+
/**
|
|
384
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
385
|
+
* root node.
|
|
386
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
387
|
+
* structure.
|
|
388
|
+
*/
|
|
331
389
|
_setRoot(v) {
|
|
332
390
|
if (v) {
|
|
333
391
|
v.parent = undefined;
|
|
@@ -349,7 +407,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
349
407
|
if (x.right) {
|
|
350
408
|
const y = x.right;
|
|
351
409
|
x.right = y.left;
|
|
352
|
-
if (y.left !== this.
|
|
410
|
+
if (y.left !== this._Sentinel) {
|
|
353
411
|
if (y.left)
|
|
354
412
|
y.left.parent = x;
|
|
355
413
|
}
|
|
@@ -383,7 +441,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
383
441
|
if (x.left) {
|
|
384
442
|
const y = x.left;
|
|
385
443
|
x.left = y.right;
|
|
386
|
-
if (y.right !== this.
|
|
444
|
+
if (y.right !== this._Sentinel) {
|
|
387
445
|
if (y.right)
|
|
388
446
|
y.right.parent = x;
|
|
389
447
|
}
|
|
@@ -415,12 +473,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
415
473
|
*/
|
|
416
474
|
_fixInsert(k) {
|
|
417
475
|
let u;
|
|
418
|
-
while (k.parent && k.parent.color ===
|
|
476
|
+
while (k.parent && k.parent.color === types_1.RBTNColor.RED) {
|
|
419
477
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
420
478
|
u = k.parent.parent.left;
|
|
421
|
-
if (u && u.color ===
|
|
422
|
-
|
|
479
|
+
if (u && u.color === types_1.RBTNColor.RED) {
|
|
480
|
+
// Delay color flip
|
|
423
481
|
k.parent.color = types_1.RBTNColor.BLACK;
|
|
482
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
424
483
|
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
425
484
|
k = k.parent.parent;
|
|
426
485
|
}
|
|
@@ -429,16 +488,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
429
488
|
k = k.parent;
|
|
430
489
|
this._rightRotate(k);
|
|
431
490
|
}
|
|
432
|
-
|
|
433
|
-
k.parent.
|
|
491
|
+
// Check color before rotation
|
|
492
|
+
if (k.parent.color === types_1.RBTNColor.RED) {
|
|
493
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
494
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
495
|
+
}
|
|
434
496
|
this._leftRotate(k.parent.parent);
|
|
435
497
|
}
|
|
436
498
|
}
|
|
437
499
|
else {
|
|
438
500
|
u = k.parent.parent.right;
|
|
439
|
-
if (u && u.color ===
|
|
440
|
-
|
|
501
|
+
if (u && u.color === types_1.RBTNColor.RED) {
|
|
502
|
+
// Delay color flip
|
|
441
503
|
k.parent.color = types_1.RBTNColor.BLACK;
|
|
504
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
442
505
|
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
443
506
|
k = k.parent.parent;
|
|
444
507
|
}
|
|
@@ -447,8 +510,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
447
510
|
k = k.parent;
|
|
448
511
|
this._leftRotate(k);
|
|
449
512
|
}
|
|
450
|
-
|
|
451
|
-
k.parent.
|
|
513
|
+
// Check color before rotation
|
|
514
|
+
if (k.parent.color === types_1.RBTNColor.RED) {
|
|
515
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
516
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
517
|
+
}
|
|
452
518
|
this._rightRotate(k.parent.parent);
|
|
453
519
|
}
|
|
454
520
|
}
|
|
@@ -7,13 +7,90 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
12
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
13
|
+
* by this node in a segment tree.
|
|
14
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
15
|
+
* node in a segment tree.
|
|
16
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
17
|
+
* the segment tree node.
|
|
18
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
19
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
20
|
+
*/
|
|
16
21
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
|
|
22
|
+
protected _start: number;
|
|
23
|
+
/**
|
|
24
|
+
* The function returns the value of the protected variable _start.
|
|
25
|
+
* @returns The start value, which is of type number.
|
|
26
|
+
*/
|
|
27
|
+
get start(): number;
|
|
28
|
+
/**
|
|
29
|
+
* The above function sets the value of the "start" property.
|
|
30
|
+
* @param {number} value - The value parameter is of type number.
|
|
31
|
+
*/
|
|
32
|
+
set start(value: number);
|
|
33
|
+
protected _end: number;
|
|
34
|
+
/**
|
|
35
|
+
* The function returns the value of the protected variable `_end`.
|
|
36
|
+
* @returns The value of the protected property `_end`.
|
|
37
|
+
*/
|
|
38
|
+
get end(): number;
|
|
39
|
+
/**
|
|
40
|
+
* The above function sets the value of the "end" property.
|
|
41
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
42
|
+
* property.
|
|
43
|
+
*/
|
|
44
|
+
set end(value: number);
|
|
45
|
+
protected _value: SegmentTreeNodeVal | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The function returns the value of a segment tree node.
|
|
48
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
49
|
+
*/
|
|
50
|
+
get value(): SegmentTreeNodeVal | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The function sets the value of a segment tree node.
|
|
53
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
54
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
55
|
+
*/
|
|
56
|
+
set value(value: SegmentTreeNodeVal | undefined);
|
|
57
|
+
protected _sum: number;
|
|
58
|
+
/**
|
|
59
|
+
* The function returns the value of the sum property.
|
|
60
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
61
|
+
*/
|
|
62
|
+
get sum(): number;
|
|
63
|
+
/**
|
|
64
|
+
* The above function sets the value of the sum property.
|
|
65
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
66
|
+
*/
|
|
67
|
+
set sum(value: number);
|
|
68
|
+
protected _left: SegmentTreeNode | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the left child of a segment tree node.
|
|
71
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
72
|
+
* `SegmentTreeNode` or `undefined`.
|
|
73
|
+
*/
|
|
74
|
+
get left(): SegmentTreeNode | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
77
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
78
|
+
* undefined.
|
|
79
|
+
*/
|
|
80
|
+
set left(value: SegmentTreeNode | undefined);
|
|
81
|
+
protected _right: SegmentTreeNode | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* The function returns the right child of a segment tree node.
|
|
84
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
85
|
+
*/
|
|
86
|
+
get right(): SegmentTreeNode | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The function sets the right child of a segment tree node.
|
|
89
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
90
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
91
|
+
* value.
|
|
92
|
+
*/
|
|
93
|
+
set right(value: SegmentTreeNode | undefined);
|
|
17
94
|
}
|
|
18
95
|
export declare class SegmentTree {
|
|
19
96
|
/**
|
|
@@ -27,12 +104,28 @@ export declare class SegmentTree {
|
|
|
27
104
|
*/
|
|
28
105
|
constructor(values: number[], start?: number, end?: number);
|
|
29
106
|
protected _values: number[];
|
|
107
|
+
/**
|
|
108
|
+
* The function returns an array of numbers.
|
|
109
|
+
* @returns An array of numbers is being returned.
|
|
110
|
+
*/
|
|
30
111
|
get values(): number[];
|
|
31
112
|
protected _start: number;
|
|
113
|
+
/**
|
|
114
|
+
* The function returns the value of the protected variable _start.
|
|
115
|
+
* @returns The start value, which is of type number.
|
|
116
|
+
*/
|
|
32
117
|
get start(): number;
|
|
33
118
|
protected _end: number;
|
|
119
|
+
/**
|
|
120
|
+
* The function returns the value of the protected variable `_end`.
|
|
121
|
+
* @returns The value of the protected property `_end`.
|
|
122
|
+
*/
|
|
34
123
|
get end(): number;
|
|
35
124
|
protected _root: SegmentTreeNode | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The function returns the root node of a segment tree.
|
|
127
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
128
|
+
*/
|
|
36
129
|
get root(): SegmentTreeNode | undefined;
|
|
37
130
|
/**
|
|
38
131
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|