data-structure-typed 0.8.18 → 1.3.0
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/LICENSE +21 -0
- package/README.md +690 -2
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
- package/dist/data-structures/binary-tree/avl-tree.js +110 -37
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
- package/dist/data-structures/binary-tree/binary-tree.js +27 -979
- package/dist/data-structures/binary-tree/bst.d.ts +118 -28
- package/dist/data-structures/binary-tree/bst.js +162 -124
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
- package/dist/data-structures/binary-tree/segment-tree.js +80 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
- package/dist/data-structures/graph/abstract-graph.js +365 -92
- package/dist/data-structures/graph/directed-graph.d.ts +175 -26
- package/dist/data-structures/graph/directed-graph.js +249 -95
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
- package/dist/data-structures/graph/undirected-graph.js +154 -44
- package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
- package/dist/data-structures/hash/coordinate-map.js +44 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +34 -0
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -51
- package/dist/data-structures/heap/heap.js +106 -63
- package/dist/data-structures/heap/max-heap.d.ts +13 -4
- package/dist/data-structures/heap/max-heap.js +10 -2
- package/dist/data-structures/heap/min-heap.d.ts +14 -4
- package/dist/data-structures/heap/min-heap.js +11 -2
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
- package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
- package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
- package/dist/data-structures/matrix/matrix2d.js +91 -8
- package/dist/data-structures/matrix/navigator.d.ts +37 -16
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/vector2d.d.ts +156 -29
- package/dist/data-structures/matrix/vector2d.js +184 -55
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
- package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
- package/dist/data-structures/priority-queue/priority-queue.js +219 -75
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +151 -7
- package/dist/data-structures/queue/queue.d.ts +68 -42
- package/dist/data-structures/queue/queue.js +95 -51
- package/dist/data-structures/stack/stack.d.ts +30 -36
- package/dist/data-structures/stack/stack.js +31 -37
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
- package/dist/data-structures/trie/trie.d.ts +39 -6
- package/dist/data-structures/trie/trie.js +81 -12
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-binary-tree.js +2 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/abstract-graph.js +2 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/avl-tree.js +2 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/bst.js +2 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/dist/interfaces/directed-graph.js +2 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/index.js +31 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/rb-tree.js +2 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +13 -7
- package/dist/types/data-structures/index.js +31 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +19 -0
- package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
- package/package.json +106 -55
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.d.ts +0 -122
- package/dist/utils.js +0 -569
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -232
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
- package/src/data-structures/binary-tree/bst.ts +0 -404
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -164
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -789
- package/src/data-structures/graph/directed-graph.ts +0 -322
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -154
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/hash-table.ts +0 -1
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -136
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -22
- package/src/data-structures/heap/min-heap.ts +0 -24
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -99
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
- package/src/data-structures/priority-queue/priority-queue.ts +0 -208
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -123
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -104
- package/src/data-structures/trampoline.ts +0 -91
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -153
- package/src/index.ts +0 -1
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +0 -158
- package/src/utils.ts +0 -605
- package/tsconfig.json +0 -53
- /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
- /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
- /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
- /package/dist/{types/types → utils}/index.d.ts +0 -0
|
@@ -1,21 +1,88 @@
|
|
|
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
|
+
*/
|
|
1
8
|
import { BST, BSTNode } from './bst';
|
|
2
|
-
import { BinaryTreeNodeId } from '
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
9
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../../types';
|
|
10
|
+
import { IAVLTree, IAVLTreeNode } from '../../interfaces';
|
|
11
|
+
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
12
|
+
constructor(id: BinaryTreeNodeId, val?: T);
|
|
6
13
|
}
|
|
7
|
-
export declare class AVLTreeNode<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
15
|
+
/**
|
|
16
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
17
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
18
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
19
|
+
* options.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: AVLTreeOptions);
|
|
22
|
+
/**
|
|
23
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
24
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
25
|
+
* identify each node in the tree.
|
|
26
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
27
|
+
* that will be stored in the node.
|
|
28
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
29
|
+
*/
|
|
30
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
31
|
+
/**
|
|
32
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
33
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
34
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
35
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
36
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
37
|
+
*/
|
|
38
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
41
|
+
* then balances the tree if necessary.
|
|
42
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
43
|
+
* removed from the AVL tree.
|
|
44
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
45
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
46
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
47
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
48
|
+
*/
|
|
49
|
+
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[];
|
|
50
|
+
/**
|
|
51
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
52
|
+
* height of its right subtree.
|
|
53
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
54
|
+
* @returns The balance factor of the given AVL tree node.
|
|
55
|
+
*/
|
|
56
|
+
protected _balanceFactor(node: N): number;
|
|
57
|
+
/**
|
|
58
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
59
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
60
|
+
*/
|
|
61
|
+
protected _updateHeight(node: N): void;
|
|
62
|
+
/**
|
|
63
|
+
* The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
64
|
+
* each node in the path from the given node to the root.
|
|
65
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
66
|
+
*/
|
|
67
|
+
protected _balancePath(node: N): void;
|
|
68
|
+
/**
|
|
69
|
+
* The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
70
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
71
|
+
*/
|
|
72
|
+
protected _balanceLL(A: N): void;
|
|
73
|
+
/**
|
|
74
|
+
* The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
75
|
+
* @param A - A is an AVLTreeNode object.
|
|
76
|
+
*/
|
|
77
|
+
protected _balanceLR(A: N): void;
|
|
78
|
+
/**
|
|
79
|
+
* The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
80
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
81
|
+
*/
|
|
82
|
+
protected _balanceRR(A: N): void;
|
|
83
|
+
/**
|
|
84
|
+
* The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
85
|
+
* @param A - A is an AVLTreeNode object.
|
|
86
|
+
*/
|
|
87
|
+
protected _balanceRL(A: N): void;
|
|
21
88
|
}
|
|
@@ -1,33 +1,81 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
const bst_1 = require("./bst");
|
|
5
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
6
|
-
|
|
7
|
-
|
|
13
|
+
constructor(id, val) {
|
|
14
|
+
super(id, val);
|
|
8
15
|
}
|
|
9
16
|
}
|
|
10
17
|
exports.AVLTreeNode = AVLTreeNode;
|
|
11
18
|
class AVLTree extends bst_1.BST {
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
/**
|
|
20
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
21
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
22
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
23
|
+
* options.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options) {
|
|
26
|
+
super(options);
|
|
14
27
|
}
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
30
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
31
|
+
* identify each node in the tree.
|
|
32
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
33
|
+
* that will be stored in the node.
|
|
34
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
35
|
+
*/
|
|
36
|
+
createNode(id, val) {
|
|
37
|
+
return new AVLTreeNode(id, val);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
41
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
42
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
43
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
44
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
45
|
+
*/
|
|
46
|
+
add(id, val) {
|
|
47
|
+
// TODO support node as a param
|
|
48
|
+
const inserted = super.add(id, val);
|
|
17
49
|
if (inserted)
|
|
18
|
-
this.
|
|
50
|
+
this._balancePath(inserted);
|
|
19
51
|
return inserted;
|
|
20
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
55
|
+
* then balances the tree if necessary.
|
|
56
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
57
|
+
* removed from the AVL tree.
|
|
58
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
59
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
60
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
61
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
62
|
+
*/
|
|
21
63
|
remove(id, isUpdateAllLeftSum) {
|
|
22
64
|
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
23
65
|
for (const { needBalanced } of deletedResults) {
|
|
24
66
|
if (needBalanced) {
|
|
25
|
-
this.
|
|
67
|
+
this._balancePath(needBalanced);
|
|
26
68
|
}
|
|
27
69
|
}
|
|
28
70
|
return deletedResults;
|
|
29
71
|
}
|
|
30
|
-
|
|
72
|
+
/**
|
|
73
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
74
|
+
* height of its right subtree.
|
|
75
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
76
|
+
* @returns The balance factor of the given AVL tree node.
|
|
77
|
+
*/
|
|
78
|
+
_balanceFactor(node) {
|
|
31
79
|
if (!node.right) // node has no right subtree
|
|
32
80
|
return -node.height;
|
|
33
81
|
else if (!node.left) // node has no left subtree
|
|
@@ -35,7 +83,11 @@ class AVLTree extends bst_1.BST {
|
|
|
35
83
|
else
|
|
36
84
|
return node.right.height - node.left.height;
|
|
37
85
|
}
|
|
38
|
-
|
|
86
|
+
/**
|
|
87
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
88
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
89
|
+
*/
|
|
90
|
+
_updateHeight(node) {
|
|
39
91
|
if (!node.left && !node.right) // node is a leaf
|
|
40
92
|
node.height = 0;
|
|
41
93
|
else if (!node.left) {
|
|
@@ -48,35 +100,44 @@ class AVLTree extends bst_1.BST {
|
|
|
48
100
|
else
|
|
49
101
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
50
102
|
}
|
|
51
|
-
|
|
103
|
+
/**
|
|
104
|
+
* The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
105
|
+
* each node in the path from the given node to the root.
|
|
106
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
107
|
+
*/
|
|
108
|
+
_balancePath(node) {
|
|
52
109
|
const path = this.getPathToRoot(node);
|
|
53
110
|
for (let i = path.length - 1; i >= 0; i--) {
|
|
54
111
|
const A = path[i];
|
|
55
|
-
this.
|
|
56
|
-
switch (this.
|
|
112
|
+
this._updateHeight(A);
|
|
113
|
+
switch (this._balanceFactor(A)) {
|
|
57
114
|
case -2:
|
|
58
115
|
if (A && A.left) {
|
|
59
|
-
if (this.
|
|
60
|
-
this.
|
|
116
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
117
|
+
this._balanceLL(A); // Perform LL rotation
|
|
61
118
|
}
|
|
62
119
|
else {
|
|
63
|
-
this.
|
|
120
|
+
this._balanceLR(A); // Perform LR rotation
|
|
64
121
|
}
|
|
65
122
|
}
|
|
66
123
|
break;
|
|
67
124
|
case +2:
|
|
68
125
|
if (A && A.right) {
|
|
69
|
-
if (this.
|
|
70
|
-
this.
|
|
126
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
127
|
+
this._balanceRR(A); // Perform RR rotation
|
|
71
128
|
}
|
|
72
129
|
else {
|
|
73
|
-
this.
|
|
130
|
+
this._balanceRL(A); // Perform RL rotation
|
|
74
131
|
}
|
|
75
132
|
}
|
|
76
133
|
}
|
|
77
134
|
}
|
|
78
135
|
}
|
|
79
|
-
|
|
136
|
+
/**
|
|
137
|
+
* The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
138
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
139
|
+
*/
|
|
140
|
+
_balanceLL(A) {
|
|
80
141
|
const parentOfA = A.parent;
|
|
81
142
|
const B = A.left; // A is left-heavy and B is left-heavy
|
|
82
143
|
A.parent = B;
|
|
@@ -87,7 +148,7 @@ class AVLTree extends bst_1.BST {
|
|
|
87
148
|
B.parent = parentOfA;
|
|
88
149
|
if (A === this.root) {
|
|
89
150
|
if (B)
|
|
90
|
-
this.
|
|
151
|
+
this._setRoot(B);
|
|
91
152
|
}
|
|
92
153
|
else {
|
|
93
154
|
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
@@ -102,11 +163,15 @@ class AVLTree extends bst_1.BST {
|
|
|
102
163
|
A.left = B.right; // Make T2 the left subtree of A
|
|
103
164
|
B.right = A; // Make A the left child of B
|
|
104
165
|
}
|
|
105
|
-
this.
|
|
166
|
+
this._updateHeight(A);
|
|
106
167
|
if (B)
|
|
107
|
-
this.
|
|
168
|
+
this._updateHeight(B);
|
|
108
169
|
}
|
|
109
|
-
|
|
170
|
+
/**
|
|
171
|
+
* The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
172
|
+
* @param A - A is an AVLTreeNode object.
|
|
173
|
+
*/
|
|
174
|
+
_balanceLR(A) {
|
|
110
175
|
const parentOfA = A.parent;
|
|
111
176
|
const B = A.left; // A is left-heavy
|
|
112
177
|
let C = null;
|
|
@@ -128,7 +193,7 @@ class AVLTree extends bst_1.BST {
|
|
|
128
193
|
}
|
|
129
194
|
if (A === this.root) {
|
|
130
195
|
if (C)
|
|
131
|
-
this.
|
|
196
|
+
this._setRoot(C);
|
|
132
197
|
}
|
|
133
198
|
else {
|
|
134
199
|
if (parentOfA) {
|
|
@@ -147,11 +212,15 @@ class AVLTree extends bst_1.BST {
|
|
|
147
212
|
C.left = B;
|
|
148
213
|
C.right = A;
|
|
149
214
|
}
|
|
150
|
-
this.
|
|
151
|
-
B && this.
|
|
152
|
-
C && this.
|
|
215
|
+
this._updateHeight(A); // Adjust heights
|
|
216
|
+
B && this._updateHeight(B);
|
|
217
|
+
C && this._updateHeight(C);
|
|
153
218
|
}
|
|
154
|
-
|
|
219
|
+
/**
|
|
220
|
+
* The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
221
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
222
|
+
*/
|
|
223
|
+
_balanceRR(A) {
|
|
155
224
|
const parentOfA = A.parent;
|
|
156
225
|
const B = A.right; // A is right-heavy and B is right-heavy
|
|
157
226
|
A.parent = B;
|
|
@@ -163,7 +232,7 @@ class AVLTree extends bst_1.BST {
|
|
|
163
232
|
}
|
|
164
233
|
if (A === this.root) {
|
|
165
234
|
if (B)
|
|
166
|
-
this.
|
|
235
|
+
this._setRoot(B);
|
|
167
236
|
}
|
|
168
237
|
else {
|
|
169
238
|
if (parentOfA) {
|
|
@@ -179,10 +248,14 @@ class AVLTree extends bst_1.BST {
|
|
|
179
248
|
A.right = B.left; // Make T2 the right subtree of A
|
|
180
249
|
B.left = A;
|
|
181
250
|
}
|
|
182
|
-
this.
|
|
183
|
-
B && this.
|
|
251
|
+
this._updateHeight(A);
|
|
252
|
+
B && this._updateHeight(B);
|
|
184
253
|
}
|
|
185
|
-
|
|
254
|
+
/**
|
|
255
|
+
* The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
256
|
+
* @param A - A is an AVLTreeNode object.
|
|
257
|
+
*/
|
|
258
|
+
_balanceRL(A) {
|
|
186
259
|
const parentOfA = A.parent;
|
|
187
260
|
const B = A.right; // A is right-heavy
|
|
188
261
|
let C = null;
|
|
@@ -203,7 +276,7 @@ class AVLTree extends bst_1.BST {
|
|
|
203
276
|
}
|
|
204
277
|
if (A === this.root) {
|
|
205
278
|
if (C)
|
|
206
|
-
this.
|
|
279
|
+
this._setRoot(C);
|
|
207
280
|
}
|
|
208
281
|
else {
|
|
209
282
|
if (parentOfA) {
|
|
@@ -223,9 +296,9 @@ class AVLTree extends bst_1.BST {
|
|
|
223
296
|
C.left = A;
|
|
224
297
|
if (C)
|
|
225
298
|
C.right = B;
|
|
226
|
-
this.
|
|
227
|
-
B && this.
|
|
228
|
-
C && this.
|
|
299
|
+
this._updateHeight(A); // Adjust heights
|
|
300
|
+
B && this._updateHeight(B);
|
|
301
|
+
C && this._updateHeight(C);
|
|
229
302
|
}
|
|
230
303
|
}
|
|
231
304
|
exports.AVLTree = AVLTree;
|
|
@@ -1,8 +1,46 @@
|
|
|
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
|
+
*/
|
|
1
8
|
export declare class BinaryIndexedTree {
|
|
2
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
11
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
12
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
13
|
+
* The size of the sum tree array is `n + 1` because
|
|
14
|
+
*/
|
|
3
15
|
constructor(n: number);
|
|
16
|
+
private _sumTree;
|
|
17
|
+
get sumTree(): number[];
|
|
18
|
+
static lowBit(x: number): number;
|
|
19
|
+
/**
|
|
20
|
+
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
21
|
+
* its ancestors.
|
|
22
|
+
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
23
|
+
* updated.
|
|
24
|
+
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
25
|
+
* at index "i" in the "_sumTree" array.
|
|
26
|
+
*/
|
|
4
27
|
update(i: number, delta: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
30
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
31
|
+
* array for which we want to calculate the prefix sum.
|
|
32
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
33
|
+
* `i`.
|
|
34
|
+
*/
|
|
5
35
|
getPrefixSum(i: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
38
|
+
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
39
|
+
* sum.
|
|
40
|
+
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
41
|
+
* the sum.
|
|
42
|
+
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
43
|
+
*/
|
|
6
44
|
getRangeSum(start: number, end: number): number;
|
|
7
|
-
|
|
45
|
+
protected _setSumTree(value: number[]): void;
|
|
8
46
|
}
|
|
@@ -1,16 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BinaryIndexedTree = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
class BinaryIndexedTree {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
14
|
+
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
15
|
+
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
16
|
+
* The size of the sum tree array is `n + 1` because
|
|
17
|
+
*/
|
|
5
18
|
constructor(n) {
|
|
6
19
|
this._sumTree = new Array(n + 1).fill(0);
|
|
7
20
|
}
|
|
21
|
+
get sumTree() {
|
|
22
|
+
return this._sumTree;
|
|
23
|
+
}
|
|
24
|
+
static lowBit(x) {
|
|
25
|
+
return x & (-x);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
29
|
+
* its ancestors.
|
|
30
|
+
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
31
|
+
* updated.
|
|
32
|
+
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
33
|
+
* at index "i" in the "_sumTree" array.
|
|
34
|
+
*/
|
|
8
35
|
update(i, delta) {
|
|
9
36
|
while (i < this._sumTree.length) {
|
|
10
37
|
this._sumTree[i] += delta;
|
|
11
38
|
i += BinaryIndexedTree.lowBit(i);
|
|
12
39
|
}
|
|
13
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
43
|
+
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
44
|
+
* array for which we want to calculate the prefix sum.
|
|
45
|
+
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
46
|
+
* `i`.
|
|
47
|
+
*/
|
|
14
48
|
getPrefixSum(i) {
|
|
15
49
|
let sum = 0;
|
|
16
50
|
while (i > 0) {
|
|
@@ -19,13 +53,21 @@ class BinaryIndexedTree {
|
|
|
19
53
|
}
|
|
20
54
|
return sum;
|
|
21
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
58
|
+
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
59
|
+
* sum.
|
|
60
|
+
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
61
|
+
* the sum.
|
|
62
|
+
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
63
|
+
*/
|
|
22
64
|
getRangeSum(start, end) {
|
|
23
65
|
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
24
66
|
throw 'Index out of bounds';
|
|
25
67
|
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
26
68
|
}
|
|
27
|
-
|
|
28
|
-
|
|
69
|
+
_setSumTree(value) {
|
|
70
|
+
this._sumTree = value;
|
|
29
71
|
}
|
|
30
72
|
}
|
|
31
73
|
exports.BinaryIndexedTree = BinaryIndexedTree;
|
|
@@ -1,140 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
id: BinaryTreeNodeId;
|
|
13
|
-
val: T;
|
|
14
|
-
count?: number;
|
|
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 { BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions } from '../../types';
|
|
9
|
+
import { AbstractBinaryTree, AbstractBinaryTreeNode } from './abstract-binary-tree';
|
|
10
|
+
import { IBinaryTree, IBinaryTreeNode } from '../../interfaces';
|
|
11
|
+
export declare class BinaryTreeNode<T = any, NEIGHBOR extends BinaryTreeNode<T, NEIGHBOR> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, NEIGHBOR> implements IBinaryTreeNode<T, NEIGHBOR> {
|
|
12
|
+
constructor(id: BinaryTreeNodeId, val?: T);
|
|
15
13
|
}
|
|
16
|
-
export declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
get left(): BinaryTreeNode<T> | null | undefined;
|
|
34
|
-
set left(v: BinaryTreeNode<T> | null | undefined);
|
|
35
|
-
protected _right?: BinaryTreeNode<T> | null;
|
|
36
|
-
get right(): BinaryTreeNode<T> | null | undefined;
|
|
37
|
-
set right(v: BinaryTreeNode<T> | null | undefined);
|
|
38
|
-
protected _parent: BinaryTreeNode<T> | null | undefined;
|
|
39
|
-
get parent(): BinaryTreeNode<T> | null | undefined;
|
|
40
|
-
set parent(v: BinaryTreeNode<T> | null | undefined);
|
|
41
|
-
protected _familyPosition: FamilyPosition;
|
|
42
|
-
get familyPosition(): FamilyPosition;
|
|
43
|
-
set familyPosition(v: FamilyPosition);
|
|
44
|
-
protected _count: number;
|
|
45
|
-
get count(): number;
|
|
46
|
-
set count(v: number);
|
|
47
|
-
protected _height: number;
|
|
48
|
-
get height(): number;
|
|
49
|
-
set height(v: number);
|
|
50
|
-
constructor(id: BinaryTreeNodeId, val: T, count?: number);
|
|
51
|
-
swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
52
|
-
clone(): BinaryTreeNode<T>;
|
|
53
|
-
}
|
|
54
|
-
export declare class BinaryTree<T> {
|
|
55
|
-
protected _root: BinaryTreeNode<T> | null;
|
|
56
|
-
get root(): BinaryTreeNode<T> | null;
|
|
57
|
-
protected set root(v: BinaryTreeNode<T> | null);
|
|
58
|
-
protected _size: number;
|
|
59
|
-
get size(): number;
|
|
60
|
-
protected set size(v: number);
|
|
61
|
-
protected _count: number;
|
|
62
|
-
get count(): number;
|
|
63
|
-
protected set count(v: number);
|
|
64
|
-
private readonly _autoIncrementId;
|
|
65
|
-
private _maxId;
|
|
66
|
-
private readonly _isDuplicatedVal;
|
|
67
|
-
protected _loopType: LoopType;
|
|
68
|
-
protected _visitedId: BinaryTreeNodeId[];
|
|
69
|
-
protected _visitedVal: Array<T>;
|
|
70
|
-
protected _visitedNode: BinaryTreeNode<T>[];
|
|
71
|
-
protected _visitedCount: number[];
|
|
72
|
-
protected _visitedLeftSum: number[];
|
|
73
|
-
protected _resetResults(): void;
|
|
74
|
-
constructor(options?: {
|
|
75
|
-
loopType?: LoopType;
|
|
76
|
-
autoIncrementId?: boolean;
|
|
77
|
-
isDuplicatedVal?: boolean;
|
|
78
|
-
});
|
|
79
|
-
createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode<T> | null;
|
|
80
|
-
clear(): void;
|
|
81
|
-
isEmpty(): boolean;
|
|
82
|
-
insertTo({ newNode, parent }: {
|
|
83
|
-
newNode: BinaryTreeNode<T> | null;
|
|
84
|
-
parent: BinaryTreeNode<T>;
|
|
85
|
-
}): BinaryTreeNode<T> | null | undefined;
|
|
86
|
-
put(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined;
|
|
87
|
-
insertMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[];
|
|
88
|
-
fill(data: T[] | BinaryTreeNode<T>[]): boolean;
|
|
89
|
-
remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<T>[];
|
|
90
|
-
getDepth(node: BinaryTreeNode<T>): number;
|
|
91
|
-
getHeight(beginRoot?: BinaryTreeNode<T> | null): number;
|
|
92
|
-
getMinHeight(beginRoot?: BinaryTreeNode<T> | null): number;
|
|
93
|
-
isBalanced(beginRoot?: BinaryTreeNode<T> | null): boolean;
|
|
94
|
-
getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): (BinaryTreeNode<T> | null | undefined)[];
|
|
95
|
-
has(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
96
|
-
get(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): BinaryTreeNode<T> | null;
|
|
97
|
-
getPathToRoot(node: BinaryTreeNode<T>): BinaryTreeNode<T>[];
|
|
98
|
-
protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): boolean | undefined;
|
|
99
|
-
protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName?: NodeOrPropertyName): void;
|
|
100
|
-
protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T>;
|
|
101
|
-
getLeftMost(): BinaryTreeNode<T> | null;
|
|
102
|
-
getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
103
|
-
getRightMost(): BinaryTreeNode<T> | null;
|
|
104
|
-
getRightMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
105
|
-
isBST(node?: BinaryTreeNode<T> | null): boolean;
|
|
106
|
-
getSubTreeSizeAndCount(subTreeRoot: BinaryTreeNode<T> | null | undefined): [number, number];
|
|
107
|
-
subTreeSum(subTreeRoot: BinaryTreeNode<T>, propertyName?: BinaryTreeNodePropertyName): number;
|
|
108
|
-
subTreeAdd(subTreeRoot: BinaryTreeNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
109
|
-
BFS(): BinaryTreeNodeId[];
|
|
110
|
-
BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
|
|
111
|
-
BFS(nodeOrPropertyName: 'val'): T[];
|
|
112
|
-
BFS(nodeOrPropertyName: 'node'): BinaryTreeNode<T>[];
|
|
113
|
-
BFS(nodeOrPropertyName: 'count'): number[];
|
|
114
|
-
DFS(): BinaryTreeNodeId[];
|
|
115
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
116
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
|
|
117
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
|
|
118
|
-
DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
119
|
-
DFSIterative(): BinaryTreeNodeId[];
|
|
120
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
121
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
|
|
122
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
|
|
123
|
-
DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
124
|
-
levelIterative(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[];
|
|
125
|
-
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
126
|
-
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[];
|
|
127
|
-
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
|
|
128
|
-
levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[];
|
|
129
|
-
listLevels(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[][];
|
|
130
|
-
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
|
|
131
|
-
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[][];
|
|
132
|
-
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[][];
|
|
133
|
-
listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[][];
|
|
134
|
-
getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
|
|
135
|
-
morris(): BinaryTreeNodeId[];
|
|
136
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
|
|
137
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
|
|
138
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
|
|
139
|
-
morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
|
|
14
|
+
export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
|
|
15
|
+
/**
|
|
16
|
+
* This is a constructor function for a binary tree class that takes an optional options parameter.
|
|
17
|
+
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
18
|
+
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
19
|
+
* different configuration options.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: BinaryTreeOptions);
|
|
22
|
+
/**
|
|
23
|
+
* The function creates a new binary tree node with an optional value.
|
|
24
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
25
|
+
* `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
|
|
26
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
27
|
+
* stored in the node.
|
|
28
|
+
* @returns a new instance of a BinaryTreeNode with the specified id and value.
|
|
29
|
+
*/
|
|
30
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
140
31
|
}
|