data-structure-typed 1.18.6 → 1.18.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/README.md +154 -366
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +72 -51
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +226 -158
- package/dist/data-structures/binary-tree/avl-tree.d.ts +7 -6
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +16 -6
- package/dist/data-structures/binary-tree/binary-tree.js +16 -7
- package/dist/data-structures/binary-tree/bst.d.ts +37 -28
- package/dist/data-structures/binary-tree/bst.js +44 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +30 -1
- package/dist/data-structures/binary-tree/rb-tree.js +27 -14
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +18 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -15
- package/dist/data-structures/graph/abstract-graph.d.ts +28 -27
- package/dist/data-structures/graph/abstract-graph.js +58 -36
- package/dist/data-structures/graph/directed-graph.d.ts +27 -24
- package/dist/data-structures/graph/directed-graph.js +65 -60
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -18
- package/dist/data-structures/graph/undirected-graph.js +52 -65
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +100 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.js +2 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +3 -8
- package/dist/data-structures/interfaces/avl-tree.d.ts +16 -1
- package/dist/data-structures/interfaces/binary-tree.d.ts +4 -24
- package/dist/data-structures/interfaces/bst.d.ts +18 -1
- package/dist/data-structures/interfaces/directed-graph.d.ts +4 -1
- package/dist/data-structures/interfaces/index.d.ts +1 -1
- package/dist/data-structures/interfaces/index.js +1 -1
- package/dist/data-structures/interfaces/rb-tree.d.ts +9 -0
- package/dist/data-structures/interfaces/rb-tree.js +2 -0
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
- package/dist/data-structures/interfaces/undirected-graph.d.ts +4 -1
- package/dist/data-structures/tree/tree.d.ts +1 -4
- package/dist/data-structures/tree/tree.js +1 -12
- package/dist/data-structures/types/abstract-binary-tree.d.ts +11 -7
- package/dist/data-structures/types/abstract-binary-tree.js +9 -5
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/bst.d.ts +1 -1
- package/dist/data-structures/types/bst.js +1 -1
- package/dist/data-structures/types/directed-graph.js +1 -1
- package/dist/data-structures/types/helpers.d.ts +1 -8
- package/dist/data-structures/types/rb-tree.d.ts +2 -0
- package/dist/data-structures/types/rb-tree.js +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +4 -4
- package/dist/utils/types/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts +6 -1
- package/dist/utils/utils.js +83 -1
- package/package.json +2 -2
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
- package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
- package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
- package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
- package/backup/recursive-type/src/data-structures/index.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
- package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
- package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
- package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
- package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
- package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
- package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
- package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/index.ts +0 -1
- package/backup/recursive-type/src/utils/index.ts +0 -2
- package/backup/recursive-type/src/utils/types/index.ts +0 -1
- package/backup/recursive-type/src/utils/types/utils.ts +0 -6
- package/backup/recursive-type/src/utils/utils.ts +0 -78
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2339
- package/docs/classes/AVLTreeNode.html +0 -450
- package/docs/classes/AaTree.html +0 -166
- package/docs/classes/AbstractBinaryTree.html +0 -2023
- package/docs/classes/AbstractBinaryTreeNode.html +0 -491
- package/docs/classes/AbstractEdge.html +0 -289
- package/docs/classes/AbstractGraph.html +0 -1046
- package/docs/classes/AbstractVertex.html +0 -252
- package/docs/classes/ArrayDeque.html +0 -433
- package/docs/classes/BST.html +0 -2196
- package/docs/classes/BSTNode.html +0 -453
- package/docs/classes/BTree.html +0 -166
- package/docs/classes/BinaryIndexedTree.html +0 -335
- package/docs/classes/BinaryTree.html +0 -2037
- package/docs/classes/BinaryTreeNode.html +0 -452
- package/docs/classes/Character.html +0 -214
- package/docs/classes/CoordinateMap.html +0 -477
- package/docs/classes/CoordinateSet.html +0 -438
- package/docs/classes/Deque.html +0 -969
- package/docs/classes/DirectedEdge.html +0 -360
- package/docs/classes/DirectedGraph.html +0 -1408
- package/docs/classes/DirectedVertex.html +0 -248
- package/docs/classes/DoublyLinkedList.html +0 -962
- package/docs/classes/DoublyLinkedListNode.html +0 -291
- package/docs/classes/HashTable.html +0 -166
- package/docs/classes/Heap.html +0 -417
- package/docs/classes/HeapItem.html +0 -249
- package/docs/classes/Matrix2D.html +0 -496
- package/docs/classes/MatrixNTI2D.html +0 -234
- package/docs/classes/MaxHeap.html +0 -430
- package/docs/classes/MaxPriorityQueue.html +0 -830
- package/docs/classes/MinHeap.html +0 -431
- package/docs/classes/MinPriorityQueue.html +0 -832
- package/docs/classes/Navigator.html +0 -307
- package/docs/classes/ObjectDeque.html +0 -449
- package/docs/classes/Pair.html +0 -166
- package/docs/classes/PriorityQueue.html +0 -754
- package/docs/classes/Queue.html +0 -386
- package/docs/classes/SegmentTree.html +0 -428
- package/docs/classes/SegmentTreeNode.html +0 -351
- package/docs/classes/SinglyLinkedList.html +0 -782
- package/docs/classes/SinglyLinkedListNode.html +0 -264
- package/docs/classes/SkipLinkedList.html +0 -166
- package/docs/classes/SplayTree.html +0 -166
- package/docs/classes/Stack.html +0 -362
- package/docs/classes/TreeMap.html +0 -166
- package/docs/classes/TreeMultiSet.html +0 -2196
- package/docs/classes/TreeMultiSetNode.html +0 -450
- package/docs/classes/TreeNode.html +0 -337
- package/docs/classes/TreeSet.html +0 -166
- package/docs/classes/Trie.html +0 -366
- package/docs/classes/TrieNode.html +0 -274
- package/docs/classes/TwoThreeTree.html +0 -166
- package/docs/classes/UndirectedEdge.html +0 -331
- package/docs/classes/UndirectedGraph.html +0 -1196
- package/docs/classes/UndirectedVertex.html +0 -248
- package/docs/classes/Vector2D.html +0 -799
- package/docs/enums/CP.html +0 -175
- package/docs/enums/FamilyPosition.html +0 -175
- package/docs/enums/LoopType.html +0 -176
- package/docs/enums/RBColor.html +0 -168
- package/docs/enums/TopologicalProperty.html +0 -175
- package/docs/index.html +0 -639
- package/docs/interfaces/IBinaryTree.html +0 -189
- package/docs/interfaces/IBinaryTreeNode.html +0 -396
- package/docs/interfaces/IDirectedGraph.html +0 -259
- package/docs/interfaces/IGraph.html +0 -471
- package/docs/interfaces/IUNDirectedGraph.html +0 -151
- package/docs/modules.html +0 -250
- package/docs/types/AVLTreeOptions.html +0 -144
- package/docs/types/AbstractBinaryTreeOptions.html +0 -150
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -146
- package/docs/types/AbstractResultByProperty.html +0 -146
- package/docs/types/AbstractResultsByProperty.html +0 -146
- package/docs/types/BSTComparator.html +0 -156
- package/docs/types/BSTOptions.html +0 -146
- package/docs/types/BinaryTreeDeletedResult.html +0 -153
- package/docs/types/BinaryTreeNodeId.html +0 -141
- package/docs/types/BinaryTreeNodePropertyName.html +0 -141
- package/docs/types/BinaryTreeOptions.html +0 -144
- package/docs/types/DFSOrderPattern.html +0 -141
- package/docs/types/DijkstraResult.html +0 -161
- package/docs/types/Direction.html +0 -141
- package/docs/types/EdgeId.html +0 -141
- package/docs/types/HeapOptions.html +0 -162
- package/docs/types/IdObject.html +0 -151
- package/docs/types/KeyValObject.html +0 -146
- package/docs/types/NavigatorParams.html +0 -175
- package/docs/types/NodeOrPropertyName.html +0 -141
- package/docs/types/PriorityQueueComparator.html +0 -161
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -141
- package/docs/types/PriorityQueueOptions.html +0 -155
- package/docs/types/RBTreeOptions.html +0 -144
- package/docs/types/RecursiveAVLTreeNode.html +0 -146
- package/docs/types/RecursiveBSTNode.html +0 -146
- package/docs/types/RecursiveBinaryTreeNode.html +0 -146
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -146
- package/docs/types/SegmentTreeNodeVal.html +0 -141
- package/docs/types/TopologicalStatus.html +0 -141
- package/docs/types/TreeMultiSetOptions.html +0 -146
- package/docs/types/Turning.html +0 -141
- package/docs/types/VertexId.html +0 -141
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId
|
|
10
|
-
import {
|
|
11
|
-
export declare class AVLTreeNode<T, FAMILY extends AVLTreeNode<T, FAMILY> =
|
|
9
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../types';
|
|
10
|
+
import { IAVLTree, IAVLTreeNode } from '../interfaces';
|
|
11
|
+
export declare class AVLTreeNode<T = any, FAMILY extends AVLTreeNode<T, FAMILY> = AVLTreeNodeNested<T>> extends BSTNode<T, FAMILY> implements IAVLTreeNode<T, FAMILY> {
|
|
12
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
12
13
|
}
|
|
13
|
-
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode
|
|
14
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
14
15
|
constructor(options?: AVLTreeOptions);
|
|
15
|
-
|
|
16
|
+
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
16
17
|
/**
|
|
17
18
|
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
18
19
|
* balances the tree.
|
|
@@ -25,7 +26,7 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode<nu
|
|
|
25
26
|
* to `1`, indicating that the value should be inserted once.
|
|
26
27
|
* @returns The method is returning either an N object or null.
|
|
27
28
|
*/
|
|
28
|
-
add(id: BinaryTreeNodeId, val
|
|
29
|
+
add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null;
|
|
29
30
|
/**
|
|
30
31
|
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
31
32
|
* then balances the tree if necessary.
|
|
@@ -40,6 +40,9 @@ var AVLTreeNode = /** @class */ (function (_super) {
|
|
|
40
40
|
function AVLTreeNode() {
|
|
41
41
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
42
42
|
}
|
|
43
|
+
AVLTreeNode.prototype.createNode = function (id, val, count) {
|
|
44
|
+
return new AVLTreeNode(id, val, count);
|
|
45
|
+
};
|
|
43
46
|
return AVLTreeNode;
|
|
44
47
|
}(bst_1.BSTNode));
|
|
45
48
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -48,9 +51,8 @@ var AVLTree = /** @class */ (function (_super) {
|
|
|
48
51
|
function AVLTree(options) {
|
|
49
52
|
return _super.call(this, options) || this;
|
|
50
53
|
}
|
|
51
|
-
AVLTree.prototype.
|
|
52
|
-
|
|
53
|
-
return node;
|
|
54
|
+
AVLTree.prototype.createNode = function (id, val, count) {
|
|
55
|
+
return new AVLTreeNode(id, val, count);
|
|
54
56
|
};
|
|
55
57
|
/**
|
|
56
58
|
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
@@ -5,17 +5,27 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId,
|
|
8
|
+
import type { BinaryTreeNodeId, BinaryTreeNodeNested } from '../types';
|
|
9
9
|
import { BinaryTreeOptions } from '../types';
|
|
10
|
-
import { IBinaryTree, IBinaryTreeNode } from '../interfaces';
|
|
11
10
|
import { AbstractBinaryTree, AbstractBinaryTreeNode } from './abstract-binary-tree';
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
import { IBinaryTree, IBinaryTreeNode } from '../interfaces/binary-tree';
|
|
12
|
+
export declare class BinaryTreeNode<T = any, FAMILY extends BinaryTreeNode<T, FAMILY> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, FAMILY> implements IBinaryTreeNode<T, FAMILY> {
|
|
13
|
+
/**
|
|
14
|
+
* The function creates a new binary tree node with an optional value and count, and returns it as a specified type.
|
|
15
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
16
|
+
* `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes.
|
|
17
|
+
* @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the
|
|
18
|
+
* value stored in the node.
|
|
19
|
+
* @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value
|
|
20
|
+
* appears in the binary tree node.
|
|
21
|
+
* @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type.
|
|
22
|
+
*/
|
|
23
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
14
24
|
}
|
|
15
25
|
export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
|
|
16
26
|
/**
|
|
17
27
|
* The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
|
|
18
|
-
*
|
|
28
|
+
* isMergeDuplicatedVal based on the provided options.
|
|
19
29
|
* @param [options] - An optional object that can contain the following properties:
|
|
20
30
|
*/
|
|
21
31
|
constructor(options?: BinaryTreeOptions);
|
|
@@ -30,5 +40,5 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
30
40
|
* of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`.
|
|
31
41
|
* @returns a BinaryTreeNode object if the value is not null, otherwise it returns null.
|
|
32
42
|
*/
|
|
33
|
-
|
|
43
|
+
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
34
44
|
}
|
|
@@ -29,8 +29,18 @@ var BinaryTreeNode = /** @class */ (function (_super) {
|
|
|
29
29
|
function BinaryTreeNode() {
|
|
30
30
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The function creates a new binary tree node with an optional value and count, and returns it as a specified type.
|
|
34
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
35
|
+
* `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes.
|
|
36
|
+
* @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the
|
|
37
|
+
* value stored in the node.
|
|
38
|
+
* @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value
|
|
39
|
+
* appears in the binary tree node.
|
|
40
|
+
* @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type.
|
|
41
|
+
*/
|
|
42
|
+
BinaryTreeNode.prototype.createNode = function (id, val, count) {
|
|
43
|
+
return new BinaryTreeNode(id, val, count);
|
|
34
44
|
};
|
|
35
45
|
return BinaryTreeNode;
|
|
36
46
|
}(abstract_binary_tree_1.AbstractBinaryTreeNode));
|
|
@@ -39,11 +49,11 @@ var BinaryTree = /** @class */ (function (_super) {
|
|
|
39
49
|
__extends(BinaryTree, _super);
|
|
40
50
|
/**
|
|
41
51
|
* The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
|
|
42
|
-
*
|
|
52
|
+
* isMergeDuplicatedVal based on the provided options.
|
|
43
53
|
* @param [options] - An optional object that can contain the following properties:
|
|
44
54
|
*/
|
|
45
55
|
function BinaryTree(options) {
|
|
46
|
-
return _super.call(this) || this;
|
|
56
|
+
return _super.call(this, options) || this;
|
|
47
57
|
}
|
|
48
58
|
/**
|
|
49
59
|
* The function creates a new binary tree node with the given id, value, and count if the value is not null, otherwise
|
|
@@ -56,9 +66,8 @@ var BinaryTree = /** @class */ (function (_super) {
|
|
|
56
66
|
* of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`.
|
|
57
67
|
* @returns a BinaryTreeNode object if the value is not null, otherwise it returns null.
|
|
58
68
|
*/
|
|
59
|
-
BinaryTree.prototype.
|
|
60
|
-
|
|
61
|
-
return node;
|
|
69
|
+
BinaryTree.prototype.createNode = function (id, val, count) {
|
|
70
|
+
return new BinaryTreeNode(id, val, count);
|
|
62
71
|
};
|
|
63
72
|
return BinaryTree;
|
|
64
73
|
}(abstract_binary_tree_1.AbstractBinaryTree));
|
|
@@ -5,19 +5,40 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator,
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested } from '../types';
|
|
9
|
+
import { BSTOptions, CP } from '../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
|
-
import {
|
|
12
|
-
export declare class BSTNode<T, FAMILY extends BSTNode<T, FAMILY> =
|
|
11
|
+
import { IBST, IBSTNode } from '../interfaces';
|
|
12
|
+
export declare class BSTNode<T = any, FAMILY extends BSTNode<T, FAMILY> = BSTNodeNested<T>> extends BinaryTreeNode<T, FAMILY> implements IBSTNode<T, FAMILY> {
|
|
13
|
+
/**
|
|
14
|
+
* The function creates a new binary search tree node with the specified id, value, and count.
|
|
15
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
|
|
16
|
+
* identify each node in the tree.
|
|
17
|
+
* @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an
|
|
18
|
+
* optional parameter, meaning it can be omitted when calling the "createNode" function.
|
|
19
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary
|
|
20
|
+
* search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
21
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
|
|
22
|
+
*/
|
|
23
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
13
24
|
}
|
|
14
|
-
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode
|
|
25
|
+
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
|
|
15
26
|
/**
|
|
16
27
|
* The constructor function accepts an optional options object and sets the comparator property if provided.
|
|
17
28
|
* @param [options] - An optional object that can contain the following properties:
|
|
18
29
|
*/
|
|
19
30
|
constructor(options?: BSTOptions);
|
|
20
|
-
|
|
31
|
+
/**
|
|
32
|
+
* The function creates a new binary search tree node with the given id, value, and count.
|
|
33
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
34
|
+
* `BinaryTreeNodeId`.
|
|
35
|
+
* @param {N['val'] | null} [val] - The `val` parameter is the value that will be stored in the node. It can be of any
|
|
36
|
+
* type `N['val']` or `null`.
|
|
37
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that represents the number of occurrences
|
|
38
|
+
* of a particular value in the binary search tree node.
|
|
39
|
+
* @returns a new instance of the BSTNode class, casted as type N.
|
|
40
|
+
*/
|
|
41
|
+
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
21
42
|
/**
|
|
22
43
|
* The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
23
44
|
* the ID matches, and returns the inserted node.
|
|
@@ -30,7 +51,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
30
51
|
* inserted once.
|
|
31
52
|
* @returns The method `add` returns a `N` object or `null`.
|
|
32
53
|
*/
|
|
33
|
-
add(id: BinaryTreeNodeId, val
|
|
54
|
+
add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null;
|
|
34
55
|
/**
|
|
35
56
|
* The `get` function returns the first node in a binary search tree that matches the given property value or name.
|
|
36
57
|
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
@@ -49,18 +70,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
49
70
|
* greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
|
|
50
71
|
* there are no nodes in
|
|
51
72
|
*/
|
|
52
|
-
lastKey():
|
|
53
|
-
/**
|
|
54
|
-
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
55
|
-
* the deleted node and any nodes that need to be balanced.
|
|
56
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
57
|
-
* from the binary search tree.
|
|
58
|
-
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
59
|
-
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
60
|
-
* set to false or not provided, the count of the node will be taken into account and the
|
|
61
|
-
* @returns an array of `BSTDeletedResult<N>` objects.
|
|
62
|
-
*/
|
|
63
|
-
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
73
|
+
lastKey(): BinaryTreeNodeId;
|
|
64
74
|
/**
|
|
65
75
|
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
66
76
|
* option to specify the property name and whether to return only one node.
|
|
@@ -76,16 +86,15 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
76
86
|
*/
|
|
77
87
|
getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
|
|
78
88
|
/**
|
|
79
|
-
* The `lesserSum` function calculates the sum of
|
|
80
|
-
* a
|
|
81
|
-
* @param {BinaryTreeNodeId}
|
|
82
|
-
* calculate the lesser sum.
|
|
89
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a lesser value
|
|
90
|
+
* than a given node.
|
|
91
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
83
92
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
84
|
-
* specifies the property
|
|
85
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
86
|
-
*
|
|
93
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
94
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in a binary
|
|
95
|
+
* tree that have a lesser value than the specified `beginNode` based on the specified `propertyName`.
|
|
87
96
|
*/
|
|
88
|
-
lesserSum(
|
|
97
|
+
lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number;
|
|
89
98
|
/**
|
|
90
99
|
* The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
|
|
91
100
|
* that have a greater value than a given node.
|
|
@@ -98,7 +107,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> exten
|
|
|
98
107
|
* defaults to 'id'.
|
|
99
108
|
* @returns a boolean value.
|
|
100
109
|
*/
|
|
101
|
-
allGreaterNodesAdd(node: N, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
110
|
+
allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
102
111
|
/**
|
|
103
112
|
* The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
|
|
104
113
|
* recursive or iterative approach.
|
|
@@ -39,6 +39,19 @@ var BSTNode = /** @class */ (function (_super) {
|
|
|
39
39
|
function BSTNode() {
|
|
40
40
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* The function creates a new binary search tree node with the specified id, value, and count.
|
|
44
|
+
* @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
|
|
45
|
+
* identify each node in the tree.
|
|
46
|
+
* @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an
|
|
47
|
+
* optional parameter, meaning it can be omitted when calling the "createNode" function.
|
|
48
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary
|
|
49
|
+
* search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
50
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
|
|
51
|
+
*/
|
|
52
|
+
BSTNode.prototype.createNode = function (id, val, count) {
|
|
53
|
+
return new BSTNode(id, val, count);
|
|
54
|
+
};
|
|
42
55
|
return BSTNode;
|
|
43
56
|
}(binary_tree_1.BinaryTreeNode));
|
|
44
57
|
exports.BSTNode = BSTNode;
|
|
@@ -59,9 +72,18 @@ var BST = /** @class */ (function (_super) {
|
|
|
59
72
|
}
|
|
60
73
|
return _this;
|
|
61
74
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
75
|
+
/**
|
|
76
|
+
* The function creates a new binary search tree node with the given id, value, and count.
|
|
77
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
78
|
+
* `BinaryTreeNodeId`.
|
|
79
|
+
* @param {N['val'] | null} [val] - The `val` parameter is the value that will be stored in the node. It can be of any
|
|
80
|
+
* type `N['val']` or `null`.
|
|
81
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that represents the number of occurrences
|
|
82
|
+
* of a particular value in the binary search tree node.
|
|
83
|
+
* @returns a new instance of the BSTNode class, casted as type N.
|
|
84
|
+
*/
|
|
85
|
+
BST.prototype.createNode = function (id, val, count) {
|
|
86
|
+
return new BSTNode(id, val, count);
|
|
65
87
|
};
|
|
66
88
|
/**
|
|
67
89
|
* The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
@@ -78,7 +100,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
78
100
|
BST.prototype.add = function (id, val, count) {
|
|
79
101
|
if (count === void 0) { count = 1; }
|
|
80
102
|
var inserted = null;
|
|
81
|
-
var newNode = this.
|
|
103
|
+
var newNode = this.createNode(id, val, count);
|
|
82
104
|
if (this.root === null) {
|
|
83
105
|
this._setRoot(newNode);
|
|
84
106
|
this._setSize(this.size + 1);
|
|
@@ -105,7 +127,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
105
127
|
if (cur.left === undefined) {
|
|
106
128
|
if (newNode) {
|
|
107
129
|
newNode.parent = cur;
|
|
108
|
-
newNode.familyPosition = types_1.FamilyPosition.LEFT;
|
|
109
130
|
}
|
|
110
131
|
//Add to the left of the current node
|
|
111
132
|
cur.left = newNode;
|
|
@@ -125,7 +146,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
125
146
|
if (cur.right === undefined) {
|
|
126
147
|
if (newNode) {
|
|
127
148
|
newNode.parent = cur;
|
|
128
|
-
newNode.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
129
149
|
}
|
|
130
150
|
//Add to the right of the current node
|
|
131
151
|
cur.right = newNode;
|
|
@@ -179,67 +199,6 @@ var BST = /** @class */ (function (_super) {
|
|
|
179
199
|
else
|
|
180
200
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
181
201
|
};
|
|
182
|
-
/**
|
|
183
|
-
* The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
|
|
184
|
-
* the deleted node and any nodes that need to be balanced.
|
|
185
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
|
|
186
|
-
* from the binary search tree.
|
|
187
|
-
* @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
|
|
188
|
-
* set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
|
|
189
|
-
* set to false or not provided, the count of the node will be taken into account and the
|
|
190
|
-
* @returns an array of `BSTDeletedResult<N>` objects.
|
|
191
|
-
*/
|
|
192
|
-
BST.prototype.remove = function (id, ignoreCount) {
|
|
193
|
-
var bstDeletedResult = [];
|
|
194
|
-
if (!this.root)
|
|
195
|
-
return bstDeletedResult;
|
|
196
|
-
var curr = this.get(id);
|
|
197
|
-
if (!curr)
|
|
198
|
-
return bstDeletedResult;
|
|
199
|
-
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
200
|
-
var needBalanced = null, orgCurrent = curr;
|
|
201
|
-
if (curr.count > 1 && !ignoreCount) {
|
|
202
|
-
curr.count--;
|
|
203
|
-
this._setCount(this.count - 1);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
if (!curr.left) {
|
|
207
|
-
if (!parent) {
|
|
208
|
-
if (curr.right !== undefined)
|
|
209
|
-
this._setRoot(curr.right);
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
switch (curr.familyPosition) {
|
|
213
|
-
case types_1.FamilyPosition.LEFT:
|
|
214
|
-
parent.left = curr.right;
|
|
215
|
-
break;
|
|
216
|
-
case types_1.FamilyPosition.RIGHT:
|
|
217
|
-
parent.right = curr.right;
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
needBalanced = parent;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
225
|
-
if (leftSubTreeMax) {
|
|
226
|
-
var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
227
|
-
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
228
|
-
if (parentOfLeftSubTreeMax) {
|
|
229
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
230
|
-
parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
|
|
231
|
-
else
|
|
232
|
-
parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
|
|
233
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
this._setSize(this.size - 1);
|
|
238
|
-
this._setCount(this.count - curr.count);
|
|
239
|
-
}
|
|
240
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
241
|
-
return bstDeletedResult;
|
|
242
|
-
};
|
|
243
202
|
/**
|
|
244
203
|
* The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
|
|
245
204
|
* option to specify the property name and whether to return only one node.
|
|
@@ -302,20 +261,24 @@ var BST = /** @class */ (function (_super) {
|
|
|
302
261
|
};
|
|
303
262
|
// --- start additional functions
|
|
304
263
|
/**
|
|
305
|
-
* The `lesserSum` function calculates the sum of
|
|
306
|
-
* a
|
|
307
|
-
* @param {BinaryTreeNodeId}
|
|
308
|
-
* calculate the lesser sum.
|
|
264
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a lesser value
|
|
265
|
+
* than a given node.
|
|
266
|
+
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
309
267
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
310
|
-
* specifies the property
|
|
311
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
312
|
-
*
|
|
268
|
+
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
269
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in a binary
|
|
270
|
+
* tree that have a lesser value than the specified `beginNode` based on the specified `propertyName`.
|
|
313
271
|
*/
|
|
314
|
-
BST.prototype.lesserSum = function (
|
|
272
|
+
BST.prototype.lesserSum = function (beginNode, propertyName) {
|
|
315
273
|
var _this = this;
|
|
316
274
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
275
|
+
if (typeof beginNode === 'number')
|
|
276
|
+
beginNode = this.get(beginNode, 'id');
|
|
277
|
+
if (!beginNode)
|
|
278
|
+
return 0;
|
|
317
279
|
if (!this.root)
|
|
318
280
|
return 0;
|
|
281
|
+
var id = beginNode.id;
|
|
319
282
|
var getSumByPropertyName = function (cur) {
|
|
320
283
|
var needSum;
|
|
321
284
|
switch (propertyName) {
|
|
@@ -404,6 +367,11 @@ var BST = /** @class */ (function (_super) {
|
|
|
404
367
|
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
405
368
|
var _this = this;
|
|
406
369
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
370
|
+
if (typeof node === 'number')
|
|
371
|
+
node = this.get(node, 'id');
|
|
372
|
+
if (!node)
|
|
373
|
+
return false;
|
|
374
|
+
var id = node.id;
|
|
407
375
|
if (!this.root)
|
|
408
376
|
return false;
|
|
409
377
|
var _sumByPropertyName = function (cur) {
|
|
@@ -421,7 +389,7 @@ var BST = /** @class */ (function (_super) {
|
|
|
421
389
|
};
|
|
422
390
|
if (this.loopType === types_1.LoopType.RECURSIVE) {
|
|
423
391
|
var _traverse_3 = function (cur) {
|
|
424
|
-
var compared = _this._compare(cur.id,
|
|
392
|
+
var compared = _this._compare(cur.id, id);
|
|
425
393
|
_sumByPropertyName(cur);
|
|
426
394
|
if (!cur.left && !cur.right)
|
|
427
395
|
return;
|
|
@@ -1 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../types';
|
|
2
|
+
import { IRBTree, IRBTreeNode } from '../interfaces/rb-tree';
|
|
3
|
+
import { BST, BSTNode } from './bst';
|
|
4
|
+
export declare class RBTreeNode<T = any, FAMILY extends RBTreeNode<T, FAMILY> = RBTreeNodeNested<T>> extends BSTNode<T, FAMILY> implements IRBTreeNode<T, FAMILY> {
|
|
5
|
+
constructor(id: BinaryTreeNodeId, val?: T, count?: number);
|
|
6
|
+
private _color;
|
|
7
|
+
get color(): RBColor;
|
|
8
|
+
set color(value: RBColor);
|
|
9
|
+
/**
|
|
10
|
+
* The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
|
|
11
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
12
|
+
* identify each node in the tree.
|
|
13
|
+
* @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
|
|
14
|
+
* (generic type) or null.
|
|
15
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
16
|
+
* node.
|
|
17
|
+
* @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
|
|
18
|
+
*/
|
|
19
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
20
|
+
}
|
|
21
|
+
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
22
|
+
constructor(options?: RBTreeOptions);
|
|
23
|
+
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
24
|
+
insert(id: number, val?: N | null): void;
|
|
25
|
+
private leftRotate;
|
|
26
|
+
private rightRotate;
|
|
27
|
+
private insertFixup;
|
|
28
|
+
private deleteFixup;
|
|
29
|
+
private transplant;
|
|
30
|
+
}
|
|
@@ -15,19 +15,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
|
|
18
|
+
exports.RBTree = exports.RBTreeNode = void 0;
|
|
19
19
|
var types_1 = require("../types");
|
|
20
|
-
var
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// }
|
|
25
|
-
function RBNode(id, val, count) {
|
|
20
|
+
var bst_1 = require("./bst");
|
|
21
|
+
var RBTreeNode = /** @class */ (function (_super) {
|
|
22
|
+
__extends(RBTreeNode, _super);
|
|
23
|
+
function RBTreeNode(id, val, count) {
|
|
26
24
|
var _this = _super.call(this, id, val, count) || this;
|
|
27
25
|
_this._color = types_1.RBColor.RED;
|
|
28
26
|
return _this;
|
|
29
27
|
}
|
|
30
|
-
Object.defineProperty(
|
|
28
|
+
Object.defineProperty(RBTreeNode.prototype, "color", {
|
|
31
29
|
get: function () {
|
|
32
30
|
return this._color;
|
|
33
31
|
},
|
|
@@ -37,16 +35,30 @@ var RBNode = /** @class */ (function (_super) {
|
|
|
37
35
|
enumerable: false,
|
|
38
36
|
configurable: true
|
|
39
37
|
});
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
/**
|
|
39
|
+
* The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
|
|
40
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
41
|
+
* identify each node in the tree.
|
|
42
|
+
* @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
|
|
43
|
+
* (generic type) or null.
|
|
44
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
45
|
+
* node.
|
|
46
|
+
* @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
|
|
47
|
+
*/
|
|
48
|
+
RBTreeNode.prototype.createNode = function (id, val, count) {
|
|
49
|
+
return new RBTreeNode(id, val, count);
|
|
50
|
+
};
|
|
51
|
+
return RBTreeNode;
|
|
52
|
+
}(bst_1.BSTNode));
|
|
53
|
+
exports.RBTreeNode = RBTreeNode;
|
|
42
54
|
var RBTree = /** @class */ (function (_super) {
|
|
43
55
|
__extends(RBTree, _super);
|
|
44
56
|
function RBTree(options) {
|
|
45
57
|
return _super.call(this, options) || this;
|
|
46
58
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
59
|
+
RBTree.prototype.createNode = function (id, val, count) {
|
|
60
|
+
return new RBTreeNode(id, val, count);
|
|
61
|
+
};
|
|
50
62
|
// private override _root: BinaryTreeNode<N> | null = null;
|
|
51
63
|
//
|
|
52
64
|
// override get root(): BinaryTreeNode<N> | null {
|
|
@@ -65,4 +77,5 @@ var RBTree = /** @class */ (function (_super) {
|
|
|
65
77
|
RBTree.prototype.transplant = function (u, v) {
|
|
66
78
|
};
|
|
67
79
|
return RBTree;
|
|
68
|
-
}(
|
|
80
|
+
}(bst_1.BST));
|
|
81
|
+
exports.RBTree = RBTree;
|
|
@@ -5,15 +5,26 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
export declare class TreeMultiSetNode<T, FAMILY extends TreeMultiSetNode<T, FAMILY> =
|
|
8
|
+
import type { BinaryTreeNodeId, TreeMultiSetNodeNested, TreeMultiSetOptions } from '../types';
|
|
9
|
+
import { ITreeMultiSet, ITreeMultiSetNode } from '../interfaces';
|
|
10
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
+
export declare class TreeMultiSetNode<T = any, FAMILY extends TreeMultiSetNode<T, FAMILY> = TreeMultiSetNodeNested<T>> extends AVLTreeNode<T, FAMILY> implements ITreeMultiSetNode<T, FAMILY> {
|
|
12
|
+
/**
|
|
13
|
+
* The function creates a new node in a binary tree with an optional value and count.
|
|
14
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
15
|
+
* identify each node in the tree.
|
|
16
|
+
* @param {T} [val] - The `val` parameter represents the value to be stored in the node. It is an optional parameter,
|
|
17
|
+
* meaning it can be omitted when calling the `createNode` method.
|
|
18
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
19
|
+
* node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
20
|
+
* @returns The method is returning a new instance of the TreeMultiSetNode class, casted as the FAMILY type.
|
|
21
|
+
*/
|
|
22
|
+
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
12
23
|
}
|
|
13
24
|
/**
|
|
14
|
-
* The only distinction between a TreeMultiSet and a
|
|
25
|
+
* The only distinction between a TreeMultiSet and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
15
26
|
*/
|
|
16
|
-
export declare class TreeMultiSet<N extends
|
|
27
|
+
export declare class TreeMultiSet<N extends TreeMultiSetNode<N['val'], N> = TreeMultiSetNode> extends AVLTree<N> implements ITreeMultiSet<N> {
|
|
17
28
|
constructor(options?: TreeMultiSetOptions);
|
|
18
29
|
/**
|
|
19
30
|
* The function creates a new BSTNode with the given id, value, and count.
|
|
@@ -24,5 +35,5 @@ export declare class TreeMultiSet<N extends BSTNode<N['val'], N> = BSTNode<numbe
|
|
|
24
35
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
25
36
|
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
26
37
|
*/
|
|
27
|
-
|
|
38
|
+
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
28
39
|
}
|
|
@@ -27,29 +27,35 @@ var __assign = (this && this.__assign) || function () {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.TreeMultiSet = exports.TreeMultiSetNode = void 0;
|
|
30
|
-
|
|
31
|
-
* data-structure-typed
|
|
32
|
-
*
|
|
33
|
-
* @author Tyler Zeng
|
|
34
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
35
|
-
* @license MIT License
|
|
36
|
-
*/
|
|
37
|
-
var bst_1 = require("./bst");
|
|
30
|
+
var avl_tree_1 = require("./avl-tree");
|
|
38
31
|
var TreeMultiSetNode = /** @class */ (function (_super) {
|
|
39
32
|
__extends(TreeMultiSetNode, _super);
|
|
40
33
|
function TreeMultiSetNode() {
|
|
41
34
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
42
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* The function creates a new node in a binary tree with an optional value and count.
|
|
38
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
39
|
+
* identify each node in the tree.
|
|
40
|
+
* @param {T} [val] - The `val` parameter represents the value to be stored in the node. It is an optional parameter,
|
|
41
|
+
* meaning it can be omitted when calling the `createNode` method.
|
|
42
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
|
|
43
|
+
* node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
44
|
+
* @returns The method is returning a new instance of the TreeMultiSetNode class, casted as the FAMILY type.
|
|
45
|
+
*/
|
|
46
|
+
TreeMultiSetNode.prototype.createNode = function (id, val, count) {
|
|
47
|
+
return new TreeMultiSetNode(id, val, count);
|
|
48
|
+
};
|
|
43
49
|
return TreeMultiSetNode;
|
|
44
|
-
}(
|
|
50
|
+
}(avl_tree_1.AVLTreeNode));
|
|
45
51
|
exports.TreeMultiSetNode = TreeMultiSetNode;
|
|
46
52
|
/**
|
|
47
|
-
* The only distinction between a TreeMultiSet and a
|
|
53
|
+
* The only distinction between a TreeMultiSet and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
48
54
|
*/
|
|
49
55
|
var TreeMultiSet = /** @class */ (function (_super) {
|
|
50
56
|
__extends(TreeMultiSet, _super);
|
|
51
57
|
function TreeMultiSet(options) {
|
|
52
|
-
return _super.call(this, __assign(__assign({}, options), {
|
|
58
|
+
return _super.call(this, __assign(__assign({}, options), { isMergeDuplicatedVal: true })) || this;
|
|
53
59
|
}
|
|
54
60
|
/**
|
|
55
61
|
* The function creates a new BSTNode with the given id, value, and count.
|
|
@@ -60,10 +66,9 @@ var TreeMultiSet = /** @class */ (function (_super) {
|
|
|
60
66
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
61
67
|
* @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
|
|
62
68
|
*/
|
|
63
|
-
TreeMultiSet.prototype.
|
|
64
|
-
|
|
65
|
-
return node;
|
|
69
|
+
TreeMultiSet.prototype.createNode = function (id, val, count) {
|
|
70
|
+
return new TreeMultiSetNode(id, val, count);
|
|
66
71
|
};
|
|
67
72
|
return TreeMultiSet;
|
|
68
|
-
}(
|
|
73
|
+
}(avl_tree_1.AVLTree));
|
|
69
74
|
exports.TreeMultiSet = TreeMultiSet;
|