data-structure-typed 1.34.7 → 1.34.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -0
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +0 -0
- package/README.md +8 -8
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/aa-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +314 -457
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +64 -90
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/b-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +12 -31
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.js +157 -244
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +20 -44
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.js +80 -122
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/splay-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +168 -239
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/binary-tree/two-three-tree.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +351 -585
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.js +152 -282
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/map-graph.js +47 -88
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.js +91 -180
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.js +85 -247
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.js +87 -128
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/pair.js.map +1 -1
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-map.js.map +1 -1
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/hash/tree-set.js.map +1 -1
- package/dist/data-structures/heap/heap.js +56 -80
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +149 -218
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +119 -218
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +50 -70
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.js +57 -70
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.js +63 -84
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +13 -41
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/queue.js +57 -157
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/tree.js +33 -46
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/utils/utils.js +22 -107
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +124 -123
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +136 -216
- package/lib/data-structures/binary-tree/avl-tree.d.ts +19 -10
- package/lib/data-structures/binary-tree/avl-tree.js +35 -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 +86 -89
- 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 +40 -42
- package/lib/data-structures/binary-tree/tree-multiset.js +76 -86
- package/lib/data-structures/graph/abstract-graph.d.ts +62 -62
- package/lib/data-structures/graph/abstract-graph.js +86 -86
- 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/priority-queue/priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.js +3 -3
- 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 +37 -42
- package/lib/interfaces/abstract-graph.d.ts +13 -13
- package/lib/interfaces/avl-tree.d.ts +6 -4
- package/lib/interfaces/bst.d.ts +10 -9
- 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-binary-tree.js +0 -1
- 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 +6 -6
- package/scripts/rename_clear_files.sh +29 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +241 -293
- package/src/data-structures/binary-tree/avl-tree.ts +43 -14
- package/src/data-structures/binary-tree/binary-tree.ts +8 -8
- package/src/data-structures/binary-tree/bst.ts +101 -96
- package/src/data-structures/binary-tree/rb-tree.ts +9 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +82 -94
- package/src/data-structures/graph/abstract-graph.ts +114 -109
- 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/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/tree/tree.ts +7 -7
- package/src/interfaces/abstract-binary-tree.ts +37 -50
- package/src/interfaces/abstract-graph.ts +13 -13
- package/src/interfaces/avl-tree.ts +6 -4
- package/src/interfaces/bst.ts +9 -9
- 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 -4
- 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/integration/avl-tree.test.ts +24 -24
- package/test/integration/bst.test.ts +71 -71
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +4 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -79
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +80 -80
- 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 +4 -5
- 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
|
@@ -5,25 +5,23 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
9
|
import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
|
|
10
10
|
import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, NEIGHBOR> implements ITreeMultisetNode<V, NEIGHBOR> {
|
|
13
13
|
/**
|
|
14
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
15
|
-
* @param {
|
|
14
|
+
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
15
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
16
16
|
* of the binary tree node.
|
|
17
17
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
18
18
|
* tree node. If no value is provided, it will be `undefined`.
|
|
19
19
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
20
20
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
21
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class
|
|
21
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
22
22
|
*/
|
|
23
|
-
constructor(
|
|
24
|
-
|
|
25
|
-
get count(): number;
|
|
26
|
-
set count(v: number);
|
|
23
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count?: number);
|
|
24
|
+
count: number;
|
|
27
25
|
}
|
|
28
26
|
/**
|
|
29
27
|
* 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.
|
|
@@ -39,15 +37,15 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
39
37
|
private _count;
|
|
40
38
|
get count(): number;
|
|
41
39
|
/**
|
|
42
|
-
* The function creates a new BSTNode with the given
|
|
43
|
-
* @param {
|
|
40
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
44
42
|
* distinguish one node from another in the tree.
|
|
45
43
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
46
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
47
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
48
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
46
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
49
47
|
*/
|
|
50
|
-
createNode(
|
|
48
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
51
49
|
/**
|
|
52
50
|
* The function swaps the location of two nodes in a tree data structure.
|
|
53
51
|
* @param {N} srcNode - The source node that we want to swap with the destination node.
|
|
@@ -59,14 +57,14 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
59
57
|
/**
|
|
60
58
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
61
59
|
* necessary.
|
|
62
|
-
* @param {
|
|
60
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
63
61
|
* represents a `BinaryTreeNode`).
|
|
64
62
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
65
63
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
66
64
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
67
65
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
68
66
|
*/
|
|
69
|
-
add(
|
|
67
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
70
68
|
/**
|
|
71
69
|
* The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
|
|
72
70
|
* node.
|
|
@@ -80,14 +78,14 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
80
78
|
/**
|
|
81
79
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
82
80
|
* the inserted nodes.
|
|
83
|
-
* @param {(
|
|
81
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
84
82
|
* objects, or null values.
|
|
85
83
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
86
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
84
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
87
85
|
* method. If provided, the `data` array should
|
|
88
86
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
89
87
|
*/
|
|
90
|
-
addMany(
|
|
88
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
91
89
|
/**
|
|
92
90
|
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
93
91
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
@@ -97,13 +95,13 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
97
95
|
/**
|
|
98
96
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
99
97
|
* node that needs to be balanced.
|
|
100
|
-
* @param {N |
|
|
98
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
101
99
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
102
100
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
103
101
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
104
102
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
105
103
|
*/
|
|
106
|
-
remove(
|
|
104
|
+
remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
107
105
|
/**
|
|
108
106
|
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
109
107
|
* recursive or iterative traversal.
|
|
@@ -115,38 +113,38 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
115
113
|
/**
|
|
116
114
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
117
115
|
* recursively or iteratively.
|
|
118
|
-
* @param {N |
|
|
119
|
-
* in a binary tree. It can be either a `
|
|
116
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
117
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
120
118
|
* `null` if the subtree is empty.
|
|
121
119
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
122
120
|
*/
|
|
123
|
-
subTreeSumCount(subTreeRoot: N |
|
|
121
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number;
|
|
124
122
|
/**
|
|
125
123
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
126
124
|
* the `count` property of each node.
|
|
127
|
-
* @param {N |
|
|
128
|
-
* in a binary tree. It can be either a `
|
|
125
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
126
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
129
127
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
130
128
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
131
129
|
* in the subtree should be increased or decreased.
|
|
132
130
|
* @returns a boolean value.
|
|
133
131
|
*/
|
|
134
|
-
subTreeAddCount(subTreeRoot: N |
|
|
132
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
135
133
|
/**
|
|
136
134
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
137
135
|
* using a queue.
|
|
138
|
-
* @param {
|
|
136
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
139
137
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
140
138
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
141
139
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
142
140
|
* to `true`, the function will return only one node. If `onlyOne`
|
|
143
141
|
* @returns an array of nodes that match the given nodeProperty.
|
|
144
142
|
*/
|
|
145
|
-
getNodesByCount(nodeProperty:
|
|
143
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[];
|
|
146
144
|
/**
|
|
147
145
|
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
148
146
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
149
|
-
*
|
|
147
|
+
* bfs traversal.
|
|
150
148
|
*/
|
|
151
149
|
BFSCount(): number[];
|
|
152
150
|
/**
|
|
@@ -165,40 +163,40 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
165
163
|
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
166
164
|
* @returns The function `morrisCount` returns an array of numbers.
|
|
167
165
|
*/
|
|
168
|
-
morrisCount(pattern?:
|
|
166
|
+
morrisCount(pattern?: DFSOrderPattern): number[];
|
|
169
167
|
/**
|
|
170
|
-
* The function
|
|
168
|
+
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
171
169
|
* the specified traversal pattern.
|
|
172
170
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
173
|
-
* the Depth-First Search (
|
|
174
|
-
* @returns The
|
|
175
|
-
* in the
|
|
171
|
+
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
172
|
+
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
173
|
+
* in the dfs traversal.
|
|
176
174
|
*/
|
|
177
|
-
|
|
175
|
+
dfsCountIterative(pattern?: DFSOrderPattern): number[];
|
|
178
176
|
/**
|
|
179
|
-
* The
|
|
177
|
+
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
180
178
|
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
181
|
-
* the Depth-First Search (
|
|
182
|
-
* @returns The
|
|
179
|
+
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
180
|
+
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
183
181
|
* traversal.
|
|
184
182
|
*/
|
|
185
|
-
|
|
183
|
+
dfsCount(pattern?: DFSOrderPattern): number[];
|
|
186
184
|
/**
|
|
187
185
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
188
186
|
* value than a given node.
|
|
189
|
-
* @param {N |
|
|
187
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
190
188
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
191
189
|
*/
|
|
192
|
-
lesserSumCount(beginNode: N |
|
|
190
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number;
|
|
193
191
|
/**
|
|
194
192
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
195
193
|
* greater than a given ID by a specified delta value.
|
|
196
|
-
* @param {N |
|
|
194
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
197
195
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
198
196
|
* of each node should be increased.
|
|
199
197
|
* @returns a boolean value.
|
|
200
198
|
*/
|
|
201
|
-
allGreaterNodesAddCount(node: N |
|
|
199
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
202
200
|
/**
|
|
203
201
|
* The clear() function clears the data and sets the count to 0.
|
|
204
202
|
*/
|
|
@@ -2,24 +2,18 @@ import { CP, FamilyPosition, LoopType } from '../../types';
|
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultisetNode extends AVLTreeNode {
|
|
4
4
|
/**
|
|
5
|
-
* The constructor function initializes a BinaryTreeNode object with
|
|
6
|
-
* @param {
|
|
5
|
+
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
6
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
7
7
|
* of the binary tree node.
|
|
8
8
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
9
9
|
* tree node. If no value is provided, it will be `undefined`.
|
|
10
10
|
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
11
11
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
12
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class
|
|
12
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
13
13
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
super(
|
|
16
|
-
this.
|
|
17
|
-
}
|
|
18
|
-
get count() {
|
|
19
|
-
return this._count;
|
|
20
|
-
}
|
|
21
|
-
set count(v) {
|
|
22
|
-
this._count = v;
|
|
14
|
+
constructor(key, val, count = 1) {
|
|
15
|
+
super(key, val);
|
|
16
|
+
this.count = count;
|
|
23
17
|
}
|
|
24
18
|
}
|
|
25
19
|
/**
|
|
@@ -33,23 +27,23 @@ export class TreeMultiset extends AVLTree {
|
|
|
33
27
|
* TreeMultiset.
|
|
34
28
|
*/
|
|
35
29
|
constructor(options) {
|
|
36
|
-
super(
|
|
30
|
+
super(options);
|
|
37
31
|
this._count = 0;
|
|
38
32
|
}
|
|
39
33
|
get count() {
|
|
40
34
|
return this._count;
|
|
41
35
|
}
|
|
42
36
|
/**
|
|
43
|
-
* The function creates a new BSTNode with the given
|
|
44
|
-
* @param {
|
|
37
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
38
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
45
39
|
* distinguish one node from another in the tree.
|
|
46
40
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
47
41
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
48
42
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
49
|
-
* @returns A new instance of the BSTNode class with the specified
|
|
43
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
50
44
|
*/
|
|
51
|
-
createNode(
|
|
52
|
-
return new TreeMultisetNode(
|
|
45
|
+
createNode(key, val, count) {
|
|
46
|
+
return new TreeMultisetNode(key, val, count);
|
|
53
47
|
}
|
|
54
48
|
/**
|
|
55
49
|
* The function swaps the location of two nodes in a tree data structure.
|
|
@@ -59,15 +53,15 @@ export class TreeMultiset extends AVLTree {
|
|
|
59
53
|
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
60
54
|
*/
|
|
61
55
|
swapLocation(srcNode, destNode) {
|
|
62
|
-
const {
|
|
63
|
-
const tempNode = this.createNode(
|
|
56
|
+
const { key, val, count, height } = destNode;
|
|
57
|
+
const tempNode = this.createNode(key, val, count);
|
|
64
58
|
if (tempNode) {
|
|
65
59
|
tempNode.height = height;
|
|
66
|
-
destNode.
|
|
60
|
+
destNode.key = srcNode.key;
|
|
67
61
|
destNode.val = srcNode.val;
|
|
68
62
|
destNode.count = srcNode.count;
|
|
69
63
|
destNode.height = srcNode.height;
|
|
70
|
-
srcNode.
|
|
64
|
+
srcNode.key = tempNode.key;
|
|
71
65
|
srcNode.val = tempNode.val;
|
|
72
66
|
srcNode.count = tempNode.count;
|
|
73
67
|
srcNode.height = tempNode.height;
|
|
@@ -77,24 +71,23 @@ export class TreeMultiset extends AVLTree {
|
|
|
77
71
|
/**
|
|
78
72
|
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
79
73
|
* necessary.
|
|
80
|
-
* @param {
|
|
74
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
81
75
|
* represents a `BinaryTreeNode`).
|
|
82
76
|
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
83
77
|
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
84
78
|
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
85
79
|
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
86
80
|
*/
|
|
87
|
-
add(
|
|
88
|
-
count = count !== null && count !== void 0 ? count : 1;
|
|
81
|
+
add(keyOrNode, val, count = 1) {
|
|
89
82
|
let inserted = undefined, newNode;
|
|
90
|
-
if (
|
|
91
|
-
newNode = this.createNode(
|
|
83
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
84
|
+
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
|
92
85
|
}
|
|
93
|
-
else if (
|
|
86
|
+
else if (keyOrNode === null) {
|
|
94
87
|
newNode = null;
|
|
95
88
|
}
|
|
96
89
|
else {
|
|
97
|
-
newNode = this.createNode(
|
|
90
|
+
newNode = this.createNode(keyOrNode, val, count);
|
|
98
91
|
}
|
|
99
92
|
if (!this.root) {
|
|
100
93
|
this._setRoot(newNode);
|
|
@@ -108,14 +101,14 @@ export class TreeMultiset extends AVLTree {
|
|
|
108
101
|
while (traversing) {
|
|
109
102
|
if (cur) {
|
|
110
103
|
if (newNode) {
|
|
111
|
-
if (this._compare(cur.
|
|
104
|
+
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
112
105
|
cur.val = newNode.val;
|
|
113
106
|
cur.count += newNode.count;
|
|
114
107
|
this._setCount(this.count + newNode.count);
|
|
115
108
|
traversing = false;
|
|
116
109
|
inserted = cur;
|
|
117
110
|
}
|
|
118
|
-
else if (this._compare(cur.
|
|
111
|
+
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
119
112
|
// Traverse left of the node
|
|
120
113
|
if (cur.left === undefined) {
|
|
121
114
|
//Add to the left of the current node
|
|
@@ -131,7 +124,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
131
124
|
cur = cur.left;
|
|
132
125
|
}
|
|
133
126
|
}
|
|
134
|
-
else if (this._compare(cur.
|
|
127
|
+
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
135
128
|
// Traverse right of the node
|
|
136
129
|
if (cur.right === undefined) {
|
|
137
130
|
//Add to the right of the current node
|
|
@@ -199,26 +192,26 @@ export class TreeMultiset extends AVLTree {
|
|
|
199
192
|
/**
|
|
200
193
|
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
201
194
|
* the inserted nodes.
|
|
202
|
-
* @param {(
|
|
195
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
203
196
|
* objects, or null values.
|
|
204
197
|
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
205
|
-
* the nodes being added. It is used when adding nodes using the `
|
|
198
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
206
199
|
* method. If provided, the `data` array should
|
|
207
200
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
208
201
|
*/
|
|
209
|
-
addMany(
|
|
202
|
+
addMany(keysOrNodes, data) {
|
|
210
203
|
const inserted = [];
|
|
211
|
-
for (let i = 0; i <
|
|
212
|
-
const
|
|
213
|
-
if (
|
|
214
|
-
inserted.push(this.add(
|
|
204
|
+
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
205
|
+
const keyOrNode = keysOrNodes[i];
|
|
206
|
+
if (keyOrNode instanceof TreeMultisetNode) {
|
|
207
|
+
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
|
215
208
|
continue;
|
|
216
209
|
}
|
|
217
|
-
if (
|
|
210
|
+
if (keyOrNode === null) {
|
|
218
211
|
inserted.push(this.add(NaN, null, 0));
|
|
219
212
|
continue;
|
|
220
213
|
}
|
|
221
|
-
inserted.push(this.add(
|
|
214
|
+
inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
|
|
222
215
|
}
|
|
223
216
|
return inserted;
|
|
224
217
|
}
|
|
@@ -228,7 +221,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
228
221
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
229
222
|
*/
|
|
230
223
|
perfectlyBalance() {
|
|
231
|
-
const sorted = this.
|
|
224
|
+
const sorted = this.dfs('in', 'node'), n = sorted.length;
|
|
232
225
|
if (sorted.length < 1)
|
|
233
226
|
return false;
|
|
234
227
|
this.clear();
|
|
@@ -238,7 +231,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
238
231
|
return;
|
|
239
232
|
const m = l + Math.floor((r - l) / 2);
|
|
240
233
|
const midNode = sorted[m];
|
|
241
|
-
this.add(midNode.
|
|
234
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
242
235
|
buildBalanceBST(l, m - 1);
|
|
243
236
|
buildBalanceBST(m + 1, r);
|
|
244
237
|
};
|
|
@@ -254,7 +247,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
254
247
|
if (l <= r) {
|
|
255
248
|
const m = l + Math.floor((r - l) / 2);
|
|
256
249
|
const midNode = sorted[m];
|
|
257
|
-
this.add(midNode.
|
|
250
|
+
this.add(midNode.key, midNode.val, midNode.count);
|
|
258
251
|
stack.push([m + 1, r]);
|
|
259
252
|
stack.push([l, m - 1]);
|
|
260
253
|
}
|
|
@@ -266,17 +259,17 @@ export class TreeMultiset extends AVLTree {
|
|
|
266
259
|
/**
|
|
267
260
|
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
268
261
|
* node that needs to be balanced.
|
|
269
|
-
* @param {N |
|
|
262
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
270
263
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
271
264
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
272
265
|
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
273
266
|
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
274
267
|
*/
|
|
275
|
-
remove(
|
|
268
|
+
remove(nodeOrKey, ignoreCount = false) {
|
|
276
269
|
const bstDeletedResult = [];
|
|
277
270
|
if (!this.root)
|
|
278
271
|
return bstDeletedResult;
|
|
279
|
-
const curr = this.get(
|
|
272
|
+
const curr = this.get(nodeOrKey);
|
|
280
273
|
if (!curr)
|
|
281
274
|
return bstDeletedResult;
|
|
282
275
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -364,14 +357,14 @@ export class TreeMultiset extends AVLTree {
|
|
|
364
357
|
/**
|
|
365
358
|
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
366
359
|
* recursively or iteratively.
|
|
367
|
-
* @param {N |
|
|
368
|
-
* in a binary tree. It can be either a `
|
|
360
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
361
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
369
362
|
* `null` if the subtree is empty.
|
|
370
363
|
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
371
364
|
*/
|
|
372
365
|
subTreeSumCount(subTreeRoot) {
|
|
373
366
|
if (typeof subTreeRoot === 'number')
|
|
374
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
367
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
375
368
|
if (!subTreeRoot)
|
|
376
369
|
return 0;
|
|
377
370
|
let sum = 0;
|
|
@@ -397,8 +390,8 @@ export class TreeMultiset extends AVLTree {
|
|
|
397
390
|
/**
|
|
398
391
|
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
399
392
|
* the `count` property of each node.
|
|
400
|
-
* @param {N |
|
|
401
|
-
* in a binary tree. It can be either a `
|
|
393
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
394
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
402
395
|
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
403
396
|
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
404
397
|
* in the subtree should be increased or decreased.
|
|
@@ -406,7 +399,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
406
399
|
*/
|
|
407
400
|
subTreeAddCount(subTreeRoot, delta) {
|
|
408
401
|
if (typeof subTreeRoot === 'number')
|
|
409
|
-
subTreeRoot = this.get(subTreeRoot, '
|
|
402
|
+
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
410
403
|
if (!subTreeRoot)
|
|
411
404
|
return false;
|
|
412
405
|
const _addByProperty = (cur) => {
|
|
@@ -435,14 +428,14 @@ export class TreeMultiset extends AVLTree {
|
|
|
435
428
|
/**
|
|
436
429
|
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
437
430
|
* using a queue.
|
|
438
|
-
* @param {
|
|
431
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
439
432
|
* `N`. It represents the property of the nodes that you want to search for.
|
|
440
433
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
441
434
|
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
442
435
|
* to `true`, the function will return only one node. If `onlyOne`
|
|
443
436
|
* @returns an array of nodes that match the given nodeProperty.
|
|
444
437
|
*/
|
|
445
|
-
getNodesByCount(nodeProperty, onlyOne) {
|
|
438
|
+
getNodesByCount(nodeProperty, onlyOne = false) {
|
|
446
439
|
if (!this.root)
|
|
447
440
|
return [];
|
|
448
441
|
const result = [];
|
|
@@ -480,10 +473,10 @@ export class TreeMultiset extends AVLTree {
|
|
|
480
473
|
/**
|
|
481
474
|
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
482
475
|
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
483
|
-
*
|
|
476
|
+
* bfs traversal.
|
|
484
477
|
*/
|
|
485
478
|
BFSCount() {
|
|
486
|
-
const nodes = super.
|
|
479
|
+
const nodes = super.bfs('node');
|
|
487
480
|
return nodes.map(node => node.count);
|
|
488
481
|
}
|
|
489
482
|
/**
|
|
@@ -505,54 +498,51 @@ export class TreeMultiset extends AVLTree {
|
|
|
505
498
|
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
506
499
|
* @returns The function `morrisCount` returns an array of numbers.
|
|
507
500
|
*/
|
|
508
|
-
morrisCount(pattern) {
|
|
509
|
-
pattern = pattern || 'in';
|
|
501
|
+
morrisCount(pattern = 'in') {
|
|
510
502
|
const nodes = super.morris(pattern, 'node');
|
|
511
503
|
return nodes.map(node => node.count);
|
|
512
504
|
}
|
|
513
505
|
/**
|
|
514
|
-
* The function
|
|
506
|
+
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
515
507
|
* the specified traversal pattern.
|
|
516
508
|
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
517
|
-
* the Depth-First Search (
|
|
518
|
-
* @returns The
|
|
519
|
-
* in the
|
|
509
|
+
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
510
|
+
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
511
|
+
* in the dfs traversal.
|
|
520
512
|
*/
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const nodes = super.DFSIterative(pattern, 'node');
|
|
513
|
+
dfsCountIterative(pattern = 'in') {
|
|
514
|
+
const nodes = super.dfsIterative(pattern, 'node');
|
|
524
515
|
return nodes.map(node => node.count);
|
|
525
516
|
}
|
|
526
517
|
/**
|
|
527
|
-
* The
|
|
518
|
+
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
528
519
|
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
529
|
-
* the Depth-First Search (
|
|
530
|
-
* @returns The
|
|
520
|
+
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
521
|
+
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
531
522
|
* traversal.
|
|
532
523
|
*/
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
const nodes = super.DFS(pattern, 'node');
|
|
524
|
+
dfsCount(pattern = 'in') {
|
|
525
|
+
const nodes = super.dfs(pattern, 'node');
|
|
536
526
|
return nodes.map(node => node.count);
|
|
537
527
|
}
|
|
538
528
|
/**
|
|
539
529
|
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
540
530
|
* value than a given node.
|
|
541
|
-
* @param {N |
|
|
531
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
542
532
|
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
543
533
|
*/
|
|
544
534
|
lesserSumCount(beginNode) {
|
|
545
535
|
if (typeof beginNode === 'number')
|
|
546
|
-
beginNode = this.get(beginNode, '
|
|
536
|
+
beginNode = this.get(beginNode, 'key');
|
|
547
537
|
if (!beginNode)
|
|
548
538
|
return 0;
|
|
549
539
|
if (!this.root)
|
|
550
540
|
return 0;
|
|
551
|
-
const
|
|
541
|
+
const key = beginNode.key;
|
|
552
542
|
let sum = 0;
|
|
553
543
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
554
544
|
const _traverse = (cur) => {
|
|
555
|
-
const compared = this._compare(cur.
|
|
545
|
+
const compared = this._compare(cur.key, key);
|
|
556
546
|
if (compared === CP.eq) {
|
|
557
547
|
if (cur.right)
|
|
558
548
|
sum += this.subTreeSumCount(cur.right);
|
|
@@ -581,7 +571,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
581
571
|
while (queue.length > 0) {
|
|
582
572
|
const cur = queue.shift();
|
|
583
573
|
if (cur) {
|
|
584
|
-
const compared = this._compare(cur.
|
|
574
|
+
const compared = this._compare(cur.key, key);
|
|
585
575
|
if (compared === CP.eq) {
|
|
586
576
|
if (cur.right)
|
|
587
577
|
sum += this.subTreeSumCount(cur.right);
|
|
@@ -611,29 +601,29 @@ export class TreeMultiset extends AVLTree {
|
|
|
611
601
|
/**
|
|
612
602
|
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
613
603
|
* greater than a given ID by a specified delta value.
|
|
614
|
-
* @param {N |
|
|
604
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
615
605
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
616
606
|
* of each node should be increased.
|
|
617
607
|
* @returns a boolean value.
|
|
618
608
|
*/
|
|
619
609
|
allGreaterNodesAddCount(node, delta) {
|
|
620
610
|
if (typeof node === 'number')
|
|
621
|
-
node = this.get(node, '
|
|
611
|
+
node = this.get(node, 'key');
|
|
622
612
|
if (!node)
|
|
623
613
|
return false;
|
|
624
|
-
const
|
|
614
|
+
const key = node.key;
|
|
625
615
|
if (!this.root)
|
|
626
616
|
return false;
|
|
627
617
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
628
618
|
const _traverse = (cur) => {
|
|
629
|
-
const compared = this._compare(cur.
|
|
619
|
+
const compared = this._compare(cur.key, key);
|
|
630
620
|
if (compared === CP.gt)
|
|
631
621
|
cur.count += delta;
|
|
632
622
|
if (!cur.left && !cur.right)
|
|
633
623
|
return;
|
|
634
|
-
if (cur.left && this._compare(cur.left.
|
|
624
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
635
625
|
_traverse(cur.left);
|
|
636
|
-
if (cur.right && this._compare(cur.right.
|
|
626
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
637
627
|
_traverse(cur.right);
|
|
638
628
|
};
|
|
639
629
|
_traverse(this.root);
|
|
@@ -644,12 +634,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
644
634
|
while (queue.length > 0) {
|
|
645
635
|
const cur = queue.shift();
|
|
646
636
|
if (cur) {
|
|
647
|
-
const compared = this._compare(cur.
|
|
637
|
+
const compared = this._compare(cur.key, key);
|
|
648
638
|
if (compared === CP.gt)
|
|
649
639
|
cur.count += delta;
|
|
650
|
-
if (cur.left && this._compare(cur.left.
|
|
640
|
+
if (cur.left && this._compare(cur.left.key, key) === CP.gt)
|
|
651
641
|
queue.push(cur.left);
|
|
652
|
-
if (cur.right && this._compare(cur.right.
|
|
642
|
+
if (cur.right && this._compare(cur.right.key, key) === CP.gt)
|
|
653
643
|
queue.push(cur.right);
|
|
654
644
|
}
|
|
655
645
|
}
|