directed-graph-typed 1.47.5 → 1.47.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/data-structures/binary-tree/bst.js +168 -169
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/heap/heap.d.ts +19 -21
- package/dist/data-structures/heap/heap.js +52 -34
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +1 -0
- package/dist/data-structures/queue/deque.js +3 -0
- package/dist/data-structures/queue/queue.d.ts +1 -0
- package/dist/data-structures/queue/queue.js +3 -0
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -2
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +4 -2
- package/dist/types/common.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -6,22 +6,34 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNKey, BTNodeExemplar } from '../../types';
|
|
10
10
|
import { BTNCallback } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
14
14
|
constructor(key: BTNKey, value?: V);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
18
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
19
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
20
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
21
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
22
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
23
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
24
|
+
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
25
|
+
*/
|
|
16
26
|
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
17
|
-
options: AVLTreeOptions;
|
|
18
27
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @param
|
|
21
|
-
*
|
|
22
|
-
*
|
|
28
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
29
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
30
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
31
|
+
* initialization.
|
|
32
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
33
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
34
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
23
35
|
*/
|
|
24
|
-
constructor(options?: AVLTreeOptions);
|
|
36
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>);
|
|
25
37
|
/**
|
|
26
38
|
* The function creates a new AVL tree node with the specified key and value.
|
|
27
39
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -32,6 +44,13 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
32
44
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
33
45
|
*/
|
|
34
46
|
createNode(key: BTNKey, value?: V): N;
|
|
47
|
+
/**
|
|
48
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
49
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
50
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
51
|
+
* being created.
|
|
52
|
+
* @returns a new AVLTree object.
|
|
53
|
+
*/
|
|
35
54
|
createTree(options?: AVLTreeOptions): TREE;
|
|
36
55
|
/**
|
|
37
56
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
@@ -41,17 +60,15 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
41
60
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
42
61
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
43
62
|
*
|
|
44
|
-
* The function overrides the add method of a
|
|
45
|
-
*
|
|
46
|
-
* @param
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
* added to the binary search tree.
|
|
50
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
63
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
64
|
+
* a new node.
|
|
65
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
66
|
+
* entry.
|
|
67
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
51
68
|
*/
|
|
52
|
-
add(
|
|
69
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
53
70
|
/**
|
|
54
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The
|
|
71
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
55
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
56
73
|
*/
|
|
57
74
|
/**
|
|
@@ -71,7 +88,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
71
88
|
*/
|
|
72
89
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BiTreeDeleteResult<N>[];
|
|
73
90
|
/**
|
|
74
|
-
* The `
|
|
91
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
75
92
|
* tree.
|
|
76
93
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
77
94
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -80,7 +97,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
80
97
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
81
98
|
* if either `srcNode` or `destNode` is undefined.
|
|
82
99
|
*/
|
|
83
|
-
protected
|
|
100
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
84
101
|
/**
|
|
85
102
|
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
|
|
86
103
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -170,4 +187,5 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
170
187
|
* @param {N} A - A is a node in a binary tree.
|
|
171
188
|
*/
|
|
172
189
|
protected _balanceRL(A: N): void;
|
|
190
|
+
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
173
191
|
}
|
|
@@ -9,7 +9,6 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
|
-
const types_1 = require("../../types");
|
|
13
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
14
13
|
constructor(key, value) {
|
|
15
14
|
super(key, value);
|
|
@@ -17,21 +16,30 @@ class AVLTreeNode extends bst_1.BSTNode {
|
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
18
|
exports.AVLTreeNode = AVLTreeNode;
|
|
19
|
+
/**
|
|
20
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
21
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
22
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
23
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
24
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
25
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
26
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
27
|
+
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
28
|
+
*/
|
|
20
29
|
class AVLTree extends bst_1.BST {
|
|
21
30
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @param
|
|
24
|
-
*
|
|
25
|
-
*
|
|
31
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
32
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
33
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
34
|
+
* initialization.
|
|
35
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
36
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
37
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
26
38
|
*/
|
|
27
|
-
constructor(options) {
|
|
28
|
-
super(options);
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
34
|
-
}
|
|
39
|
+
constructor(elements, options) {
|
|
40
|
+
super([], options);
|
|
41
|
+
if (elements)
|
|
42
|
+
super.addMany(elements);
|
|
35
43
|
}
|
|
36
44
|
/**
|
|
37
45
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -45,8 +53,15 @@ class AVLTree extends bst_1.BST {
|
|
|
45
53
|
createNode(key, value) {
|
|
46
54
|
return new AVLTreeNode(key, value);
|
|
47
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
58
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
59
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
60
|
+
* being created.
|
|
61
|
+
* @returns a new AVLTree object.
|
|
62
|
+
*/
|
|
48
63
|
createTree(options) {
|
|
49
|
-
return new AVLTree(Object.assign(
|
|
64
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
50
65
|
}
|
|
51
66
|
/**
|
|
52
67
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
@@ -56,24 +71,22 @@ class AVLTree extends bst_1.BST {
|
|
|
56
71
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
57
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
58
73
|
*
|
|
59
|
-
* The function overrides the add method of a
|
|
60
|
-
*
|
|
61
|
-
* @param
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* added to the binary search tree.
|
|
65
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
74
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
75
|
+
* a new node.
|
|
76
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
77
|
+
* entry.
|
|
78
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
66
79
|
*/
|
|
67
|
-
add(
|
|
68
|
-
if (
|
|
80
|
+
add(keyOrNodeOrEntry) {
|
|
81
|
+
if (keyOrNodeOrEntry === null)
|
|
69
82
|
return undefined;
|
|
70
|
-
const inserted = super.add(
|
|
83
|
+
const inserted = super.add(keyOrNodeOrEntry);
|
|
71
84
|
if (inserted)
|
|
72
85
|
this._balancePath(inserted);
|
|
73
86
|
return inserted;
|
|
74
87
|
}
|
|
75
88
|
/**
|
|
76
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The
|
|
89
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
77
90
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
78
91
|
*/
|
|
79
92
|
/**
|
|
@@ -103,7 +116,7 @@ class AVLTree extends bst_1.BST {
|
|
|
103
116
|
return deletedResults;
|
|
104
117
|
}
|
|
105
118
|
/**
|
|
106
|
-
* The `
|
|
119
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
107
120
|
* tree.
|
|
108
121
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
109
122
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -112,9 +125,9 @@ class AVLTree extends bst_1.BST {
|
|
|
112
125
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
113
126
|
* if either `srcNode` or `destNode` is undefined.
|
|
114
127
|
*/
|
|
115
|
-
|
|
116
|
-
srcNode = this.
|
|
117
|
-
destNode = this.
|
|
128
|
+
_swapProperties(srcNode, destNode) {
|
|
129
|
+
srcNode = this.ensureNode(srcNode);
|
|
130
|
+
destNode = this.ensureNode(destNode);
|
|
118
131
|
if (srcNode && destNode) {
|
|
119
132
|
const { key, value, height } = destNode;
|
|
120
133
|
const tempNode = this.createNode(key, value);
|
|
@@ -425,5 +438,9 @@ class AVLTree extends bst_1.BST {
|
|
|
425
438
|
B && this._updateHeight(B);
|
|
426
439
|
C && this._updateHeight(C);
|
|
427
440
|
}
|
|
441
|
+
_replaceNode(oldNode, newNode) {
|
|
442
|
+
newNode.height = oldNode.height;
|
|
443
|
+
return super._replaceNode(oldNode, newNode);
|
|
444
|
+
}
|
|
428
445
|
}
|
|
429
446
|
exports.AVLTree = AVLTree;
|