data-structure-typed 1.35.0 → 1.40.0-rc
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/.github/workflows/ci.yml +3 -0
- package/CHANGELOG.md +3 -1
- package/CONTRIBUTING.md +18 -0
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +269 -4
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +83 -0
- package/dist/data-structures/heap/heap.js +62 -0
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +23 -0
- package/dist/data-structures/heap/max-heap.js +16 -0
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +24 -0
- package/dist/data-structures/heap/min-heap.js +17 -0
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +18 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +19 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +180 -0
- package/dist/data-structures/priority-queue/priority-queue.js +141 -0
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/package.json +11 -7
- package/test/integration/avl-tree.test.ts +4 -4
- package/test/integration/bst.test.ts +8 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/linked-list/index.ts +0 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segment-tree.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/segment-tree.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"segment-tree.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/segment-tree.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAIH,MAAa,eAAe;IAC1B,YAAY,KAAa,EAAE,GAAW,EAAE,GAAW,EAAE,GAA+B;QAO5E,WAAM,GAAG,CAAC,CAAC;QASX,SAAI,GAAG,CAAC,CAAC;QAUT,SAAI,GAA8B,IAAI,CAAC;QAUvC,SAAI,GAAG,CAAC,CAAC;QAUT,UAAK,GAA2B,IAAI,CAAC;QAUrC,WAAM,GAA2B,IAAI,CAAC;QAvD5C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,IAAI,GAAG,GAAG,IAAI,IAAI,CAAC;IAC1B,CAAC;IAGD,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,CAAS;QACjB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,CAAS;QACf,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,CAA4B;QAClC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,CAAS;QACf,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAID,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAA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,CAAC,KAAa;QAC7B,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;IACpB,CAAC;IAES,QAAQ,CAAC,CAAyB;QAC1C,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;CACF;AAlLD,kCAkLC"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
|
|
10
|
+
import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
|
|
11
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
+
export declare class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, NEIGHBOR> implements ITreeMultisetNode<V, NEIGHBOR> {
|
|
13
|
+
/**
|
|
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
|
+
* of the binary tree node.
|
|
17
|
+
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
18
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
19
|
+
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
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.
|
|
22
|
+
*/
|
|
23
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count?: number);
|
|
24
|
+
count: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
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.
|
|
28
|
+
*/
|
|
29
|
+
export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements ITreeMultiset<N> {
|
|
30
|
+
/**
|
|
31
|
+
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
32
|
+
* merge duplicated values.
|
|
33
|
+
* @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
|
|
34
|
+
* TreeMultiset.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options?: TreeMultisetOptions);
|
|
37
|
+
private _count;
|
|
38
|
+
get count(): number;
|
|
39
|
+
/**
|
|
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
|
|
42
|
+
* distinguish one node from another in the tree.
|
|
43
|
+
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
44
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
|
+
*/
|
|
48
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
49
|
+
/**
|
|
50
|
+
* The function swaps the location of two nodes in a tree data structure.
|
|
51
|
+
* @param {N} srcNode - The source node that we want to swap with the destination node.
|
|
52
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
53
|
+
* be swapped with.
|
|
54
|
+
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
55
|
+
*/
|
|
56
|
+
swapLocation(srcNode: N, destNode: N): N;
|
|
57
|
+
/**
|
|
58
|
+
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
59
|
+
* necessary.
|
|
60
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
61
|
+
* represents a `BinaryTreeNode`).
|
|
62
|
+
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
63
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
64
|
+
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
65
|
+
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
66
|
+
*/
|
|
67
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
68
|
+
/**
|
|
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
|
|
70
|
+
* node.
|
|
71
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
|
|
72
|
+
* be either a node object (`N`) or `null`.
|
|
73
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
74
|
+
* child.
|
|
75
|
+
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
76
|
+
*/
|
|
77
|
+
_addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
80
|
+
* the inserted nodes.
|
|
81
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
82
|
+
* objects, or null values.
|
|
83
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
84
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
85
|
+
* method. If provided, the `data` array should
|
|
86
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
87
|
+
*/
|
|
88
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
89
|
+
/**
|
|
90
|
+
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
91
|
+
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
92
|
+
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
93
|
+
*/
|
|
94
|
+
perfectlyBalance(): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
97
|
+
* node that needs to be balanced.
|
|
98
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
99
|
+
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
100
|
+
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
101
|
+
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
102
|
+
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
|
+
*/
|
|
104
|
+
remove(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
|
+
/**
|
|
106
|
+
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
107
|
+
* recursive or iterative traversal.
|
|
108
|
+
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
109
|
+
* binary tree.
|
|
110
|
+
* @returns The function `getSubTreeCount` returns an array `[number, number]`.
|
|
111
|
+
*/
|
|
112
|
+
getSubTreeCount(subTreeRoot: N | null | undefined): [number, number];
|
|
113
|
+
/**
|
|
114
|
+
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
115
|
+
* recursively or iteratively.
|
|
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
|
|
118
|
+
* `null` if the subtree is empty.
|
|
119
|
+
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
120
|
+
*/
|
|
121
|
+
subTreeSumCount(subTreeRoot: N | BinaryTreeNodeKey | null): number;
|
|
122
|
+
/**
|
|
123
|
+
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
124
|
+
* the `count` property of each node.
|
|
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
|
|
127
|
+
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
128
|
+
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
129
|
+
* in the subtree should be increased or decreased.
|
|
130
|
+
* @returns a boolean value.
|
|
131
|
+
*/
|
|
132
|
+
subTreeAddCount(subTreeRoot: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
135
|
+
* using a queue.
|
|
136
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
137
|
+
* `N`. It represents the property of the nodes that you want to search for.
|
|
138
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
139
|
+
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
140
|
+
* to `true`, the function will return only one node. If `onlyOne`
|
|
141
|
+
* @returns an array of nodes that match the given nodeProperty.
|
|
142
|
+
*/
|
|
143
|
+
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[];
|
|
144
|
+
/**
|
|
145
|
+
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
146
|
+
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
147
|
+
* bfs traversal.
|
|
148
|
+
*/
|
|
149
|
+
BFSCount(): number[];
|
|
150
|
+
/**
|
|
151
|
+
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
152
|
+
* count property of each node at that level.
|
|
153
|
+
* @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
|
|
154
|
+
* the class `N` or `null`.
|
|
155
|
+
* @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
|
|
156
|
+
* array represents the count property of a node in that level.
|
|
157
|
+
*/
|
|
158
|
+
listLevelsCount(node: N | null): number[][];
|
|
159
|
+
/**
|
|
160
|
+
* The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
|
|
161
|
+
* pattern.
|
|
162
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
|
|
163
|
+
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
164
|
+
* @returns The function `morrisCount` returns an array of numbers.
|
|
165
|
+
*/
|
|
166
|
+
morrisCount(pattern?: DFSOrderPattern): number[];
|
|
167
|
+
/**
|
|
168
|
+
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
169
|
+
* the specified traversal pattern.
|
|
170
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
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.
|
|
174
|
+
*/
|
|
175
|
+
dfsCountIterative(pattern?: DFSOrderPattern): number[];
|
|
176
|
+
/**
|
|
177
|
+
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
178
|
+
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
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
|
|
181
|
+
* traversal.
|
|
182
|
+
*/
|
|
183
|
+
dfsCount(pattern?: DFSOrderPattern): number[];
|
|
184
|
+
/**
|
|
185
|
+
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
186
|
+
* value than a given node.
|
|
187
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
188
|
+
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
189
|
+
*/
|
|
190
|
+
lesserSumCount(beginNode: N | BinaryTreeNodeKey | null): number;
|
|
191
|
+
/**
|
|
192
|
+
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
193
|
+
* greater than a given ID by a specified delta value.
|
|
194
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
195
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
196
|
+
* of each node should be increased.
|
|
197
|
+
* @returns a boolean value.
|
|
198
|
+
*/
|
|
199
|
+
allGreaterNodesAddCount(node: N | BinaryTreeNodeKey | null, delta: number): boolean;
|
|
200
|
+
/**
|
|
201
|
+
* The clear() function clears the data and sets the count to 0.
|
|
202
|
+
*/
|
|
203
|
+
clear(): void;
|
|
204
|
+
/**
|
|
205
|
+
* The function "_setCount" is used to set the value of the "_count" property.
|
|
206
|
+
* @param {number} v - number
|
|
207
|
+
*/
|
|
208
|
+
protected _setCount(v: number): void;
|
|
209
|
+
}
|
|
@@ -4,13 +4,32 @@ exports.TreeMultiset = exports.TreeMultisetNode = void 0;
|
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const avl_tree_1 = require("./avl-tree");
|
|
6
6
|
class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
7
|
+
/**
|
|
8
|
+
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
10
|
+
* of the binary tree node.
|
|
11
|
+
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
13
|
+
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
14
|
+
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
15
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
16
|
+
*/
|
|
7
17
|
constructor(key, val, count = 1) {
|
|
8
18
|
super(key, val);
|
|
9
19
|
this.count = count;
|
|
10
20
|
}
|
|
11
21
|
}
|
|
12
22
|
exports.TreeMultisetNode = TreeMultisetNode;
|
|
23
|
+
/**
|
|
24
|
+
* 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.
|
|
25
|
+
*/
|
|
13
26
|
class TreeMultiset extends avl_tree_1.AVLTree {
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
29
|
+
* merge duplicated values.
|
|
30
|
+
* @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
|
|
31
|
+
* TreeMultiset.
|
|
32
|
+
*/
|
|
14
33
|
constructor(options) {
|
|
15
34
|
super(options);
|
|
16
35
|
this._count = 0;
|
|
@@ -18,9 +37,25 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
18
37
|
get count() {
|
|
19
38
|
return this._count;
|
|
20
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
42
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
43
|
+
* distinguish one node from another in the tree.
|
|
44
|
+
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
45
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
46
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
47
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
48
|
+
*/
|
|
21
49
|
createNode(key, val, count) {
|
|
22
50
|
return new TreeMultisetNode(key, val, count);
|
|
23
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* The function swaps the location of two nodes in a tree data structure.
|
|
54
|
+
* @param {N} srcNode - The source node that we want to swap with the destination node.
|
|
55
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
56
|
+
* be swapped with.
|
|
57
|
+
* @returns the `destNode` after swapping its values with the `srcNode`.
|
|
58
|
+
*/
|
|
24
59
|
swapLocation(srcNode, destNode) {
|
|
25
60
|
const { key, val, count, height } = destNode;
|
|
26
61
|
const tempNode = this.createNode(key, val, count);
|
|
@@ -37,6 +72,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
37
72
|
}
|
|
38
73
|
return destNode;
|
|
39
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
|
77
|
+
* necessary.
|
|
78
|
+
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
|
79
|
+
* represents a `BinaryTreeNode`).
|
|
80
|
+
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
|
81
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
|
82
|
+
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
|
83
|
+
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
84
|
+
*/
|
|
40
85
|
add(keyOrNode, val, count = 1) {
|
|
41
86
|
let inserted = undefined, newNode;
|
|
42
87
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
@@ -68,7 +113,9 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
68
113
|
inserted = cur;
|
|
69
114
|
}
|
|
70
115
|
else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
|
|
116
|
+
// Traverse left of the node
|
|
71
117
|
if (cur.left === undefined) {
|
|
118
|
+
//Add to the left of the current node
|
|
72
119
|
cur.left = newNode;
|
|
73
120
|
this._setSize(this.size + 1);
|
|
74
121
|
this._setCount(this.count + newNode.count);
|
|
@@ -76,12 +123,15 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
76
123
|
inserted = cur.left;
|
|
77
124
|
}
|
|
78
125
|
else {
|
|
126
|
+
//Traverse the left of the current node
|
|
79
127
|
if (cur.left)
|
|
80
128
|
cur = cur.left;
|
|
81
129
|
}
|
|
82
130
|
}
|
|
83
131
|
else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
|
|
132
|
+
// Traverse right of the node
|
|
84
133
|
if (cur.right === undefined) {
|
|
134
|
+
//Add to the right of the current node
|
|
85
135
|
cur.right = newNode;
|
|
86
136
|
this._setSize(this.size + 1);
|
|
87
137
|
this._setCount(this.count + newNode.count);
|
|
@@ -89,12 +139,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
89
139
|
inserted = cur.right;
|
|
90
140
|
}
|
|
91
141
|
else {
|
|
142
|
+
//Traverse the left of the current node
|
|
92
143
|
if (cur.right)
|
|
93
144
|
cur = cur.right;
|
|
94
145
|
}
|
|
95
146
|
}
|
|
96
147
|
}
|
|
97
148
|
else {
|
|
149
|
+
// TODO may need to support null inserted
|
|
98
150
|
}
|
|
99
151
|
}
|
|
100
152
|
else {
|
|
@@ -106,6 +158,15 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
106
158
|
this._balancePath(inserted);
|
|
107
159
|
return inserted;
|
|
108
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* 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
|
|
163
|
+
* node.
|
|
164
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
|
|
165
|
+
* be either a node object (`N`) or `null`.
|
|
166
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
167
|
+
* child.
|
|
168
|
+
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
169
|
+
*/
|
|
109
170
|
_addTo(newNode, parent) {
|
|
110
171
|
if (parent) {
|
|
111
172
|
if (parent.left === undefined) {
|
|
@@ -132,6 +193,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
132
193
|
return;
|
|
133
194
|
}
|
|
134
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
|
|
198
|
+
* the inserted nodes.
|
|
199
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
200
|
+
* objects, or null values.
|
|
201
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
202
|
+
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
|
203
|
+
* method. If provided, the `data` array should
|
|
204
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
205
|
+
*/
|
|
135
206
|
addMany(keysOrNodes, data) {
|
|
136
207
|
const inserted = [];
|
|
137
208
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
@@ -148,6 +219,11 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
148
219
|
}
|
|
149
220
|
return inserted;
|
|
150
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
224
|
+
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
225
|
+
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
226
|
+
*/
|
|
151
227
|
perfectlyBalance() {
|
|
152
228
|
const sorted = this.dfs('in', 'node'), n = sorted.length;
|
|
153
229
|
if (sorted.length < 1)
|
|
@@ -184,6 +260,15 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
184
260
|
return true;
|
|
185
261
|
}
|
|
186
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
265
|
+
* node that needs to be balanced.
|
|
266
|
+
* @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
|
|
267
|
+
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
268
|
+
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
269
|
+
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
270
|
+
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
271
|
+
*/
|
|
187
272
|
remove(nodeOrKey, ignoreCount = false) {
|
|
188
273
|
const bstDeletedResult = [];
|
|
189
274
|
if (!this.root)
|
|
@@ -231,6 +316,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
231
316
|
}
|
|
232
317
|
}
|
|
233
318
|
this._setSize(this.size - 1);
|
|
319
|
+
// TODO How to handle when the count of target node is lesser than current node's count
|
|
234
320
|
this._setCount(this.count - orgCurrent.count);
|
|
235
321
|
}
|
|
236
322
|
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
@@ -239,6 +325,13 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
239
325
|
}
|
|
240
326
|
return bstDeletedResult;
|
|
241
327
|
}
|
|
328
|
+
/**
|
|
329
|
+
* The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
|
|
330
|
+
* recursive or iterative traversal.
|
|
331
|
+
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
332
|
+
* binary tree.
|
|
333
|
+
* @returns The function `getSubTreeCount` returns an array `[number, number]`.
|
|
334
|
+
*/
|
|
242
335
|
getSubTreeCount(subTreeRoot) {
|
|
243
336
|
const res = [0, 0];
|
|
244
337
|
if (!subTreeRoot)
|
|
@@ -265,6 +358,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
265
358
|
return res;
|
|
266
359
|
}
|
|
267
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
|
|
363
|
+
* recursively or iteratively.
|
|
364
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
365
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree) or
|
|
366
|
+
* `null` if the subtree is empty.
|
|
367
|
+
* @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
|
|
368
|
+
*/
|
|
268
369
|
subTreeSumCount(subTreeRoot) {
|
|
269
370
|
if (typeof subTreeRoot === 'number')
|
|
270
371
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
@@ -290,6 +391,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
290
391
|
}
|
|
291
392
|
return sum;
|
|
292
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
|
|
396
|
+
* the `count` property of each node.
|
|
397
|
+
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
|
|
398
|
+
* in a binary tree. It can be either a `BinaryTreeNodeKey` (a unique identifier for a node in the binary tree), a
|
|
399
|
+
* `BinaryTreeNode` object, or `null` if the subtree is empty.
|
|
400
|
+
* @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
|
|
401
|
+
* in the subtree should be increased or decreased.
|
|
402
|
+
* @returns a boolean value.
|
|
403
|
+
*/
|
|
293
404
|
subTreeAddCount(subTreeRoot, delta) {
|
|
294
405
|
if (typeof subTreeRoot === 'number')
|
|
295
406
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
@@ -318,6 +429,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
318
429
|
}
|
|
319
430
|
return true;
|
|
320
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
434
|
+
* using a queue.
|
|
435
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
436
|
+
* `N`. It represents the property of the nodes that you want to search for.
|
|
437
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
438
|
+
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
439
|
+
* to `true`, the function will return only one node. If `onlyOne`
|
|
440
|
+
* @returns an array of nodes that match the given nodeProperty.
|
|
441
|
+
*/
|
|
321
442
|
getNodesByCount(nodeProperty, onlyOne = false) {
|
|
322
443
|
if (!this.root)
|
|
323
444
|
return [];
|
|
@@ -353,26 +474,67 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
353
474
|
}
|
|
354
475
|
return result;
|
|
355
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
479
|
+
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
480
|
+
* bfs traversal.
|
|
481
|
+
*/
|
|
356
482
|
BFSCount() {
|
|
357
483
|
const nodes = super.bfs('node');
|
|
358
484
|
return nodes.map(node => node.count);
|
|
359
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
488
|
+
* count property of each node at that level.
|
|
489
|
+
* @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
|
|
490
|
+
* the class `N` or `null`.
|
|
491
|
+
* @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
|
|
492
|
+
* array represents the count property of a node in that level.
|
|
493
|
+
*/
|
|
360
494
|
listLevelsCount(node) {
|
|
361
495
|
const levels = super.listLevels(node, 'node');
|
|
362
496
|
return levels.map(level => level.map(node => node.count));
|
|
363
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
|
|
500
|
+
* pattern.
|
|
501
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
|
|
502
|
+
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
503
|
+
* @returns The function `morrisCount` returns an array of numbers.
|
|
504
|
+
*/
|
|
364
505
|
morrisCount(pattern = 'in') {
|
|
365
506
|
const nodes = super.morris(pattern, 'node');
|
|
366
507
|
return nodes.map(node => node.count);
|
|
367
508
|
}
|
|
509
|
+
/**
|
|
510
|
+
* The function dfsCountIterative performs an iterative depth-first search and returns an array of node counts based on
|
|
511
|
+
* the specified traversal pattern.
|
|
512
|
+
* @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
|
|
513
|
+
* the Depth-First Search (dfs) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
|
|
514
|
+
* @returns The dfsCountIterative function returns an array of numbers, which represents the count property of each node
|
|
515
|
+
* in the dfs traversal.
|
|
516
|
+
*/
|
|
368
517
|
dfsCountIterative(pattern = 'in') {
|
|
369
518
|
const nodes = super.dfsIterative(pattern, 'node');
|
|
370
519
|
return nodes.map(node => node.count);
|
|
371
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* The dfsCount function returns an array of counts for each node in a depth-first search traversal.
|
|
523
|
+
* @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
|
|
524
|
+
* the Depth-First Search (dfs) algorithm should traverse the nodes. It can have one of the following values:
|
|
525
|
+
* @returns The dfsCount function returns an array of numbers, specifically the count property of each node in the dfs
|
|
526
|
+
* traversal.
|
|
527
|
+
*/
|
|
372
528
|
dfsCount(pattern = 'in') {
|
|
373
529
|
const nodes = super.dfs(pattern, 'node');
|
|
374
530
|
return nodes.map(node => node.count);
|
|
375
531
|
}
|
|
532
|
+
/**
|
|
533
|
+
* The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
|
|
534
|
+
* value than a given node.
|
|
535
|
+
* @param {N | BinaryTreeNodeKey | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
536
|
+
* @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
|
|
537
|
+
*/
|
|
376
538
|
lesserSumCount(beginNode) {
|
|
377
539
|
if (typeof beginNode === 'number')
|
|
378
540
|
beginNode = this.get(beginNode, 'key');
|
|
@@ -420,6 +582,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
420
582
|
return sum;
|
|
421
583
|
}
|
|
422
584
|
else if (compared === types_1.CP.lt) {
|
|
585
|
+
// todo maybe a bug
|
|
423
586
|
if (cur.left)
|
|
424
587
|
sum += this.subTreeSumCount(cur.left);
|
|
425
588
|
sum += cur.count;
|
|
@@ -439,6 +602,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
439
602
|
}
|
|
440
603
|
return sum;
|
|
441
604
|
}
|
|
605
|
+
/**
|
|
606
|
+
* The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
|
|
607
|
+
* greater than a given ID by a specified delta value.
|
|
608
|
+
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be one of the following:
|
|
609
|
+
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
|
|
610
|
+
* of each node should be increased.
|
|
611
|
+
* @returns a boolean value.
|
|
612
|
+
*/
|
|
442
613
|
allGreaterNodesAddCount(node, delta) {
|
|
443
614
|
if (typeof node === 'number')
|
|
444
615
|
node = this.get(node, 'key');
|
|
@@ -479,10 +650,17 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
479
650
|
return true;
|
|
480
651
|
}
|
|
481
652
|
}
|
|
653
|
+
/**
|
|
654
|
+
* The clear() function clears the data and sets the count to 0.
|
|
655
|
+
*/
|
|
482
656
|
clear() {
|
|
483
657
|
super.clear();
|
|
484
658
|
this._setCount(0);
|
|
485
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* The function "_setCount" is used to set the value of the "_count" property.
|
|
662
|
+
* @param {number} v - number
|
|
663
|
+
*/
|
|
486
664
|
_setCount(v) {
|
|
487
665
|
this._count = v;
|
|
488
666
|
}
|