deque-typed 1.47.6 → 1.47.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
  10. package/dist/data-structures/binary-tree/segment-tree.js +7 -7
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  12. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  13. package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
  14. package/dist/data-structures/graph/abstract-graph.js +30 -30
  15. package/dist/data-structures/graph/directed-graph.d.ts +24 -24
  16. package/dist/data-structures/graph/directed-graph.js +28 -28
  17. package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
  18. package/dist/data-structures/graph/undirected-graph.js +18 -18
  19. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  20. package/dist/data-structures/hash/hash-map.js +5 -8
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
  25. package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
  26. package/dist/data-structures/queue/queue.d.ts +13 -13
  27. package/dist/data-structures/queue/queue.js +13 -13
  28. package/dist/data-structures/stack/stack.d.ts +6 -6
  29. package/dist/data-structures/stack/stack.js +7 -7
  30. package/dist/data-structures/trie/trie.d.ts +3 -0
  31. package/dist/data-structures/trie/trie.js +19 -4
  32. package/dist/interfaces/binary-tree.d.ts +3 -3
  33. package/dist/types/common.d.ts +6 -1
  34. package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
  35. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  36. package/package.json +2 -2
  37. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  38. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  39. package/src/data-structures/binary-tree/bst.ts +157 -154
  40. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  41. package/src/data-structures/binary-tree/segment-tree.ts +10 -10
  42. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  43. package/src/data-structures/graph/abstract-graph.ts +46 -46
  44. package/src/data-structures/graph/directed-graph.ts +40 -40
  45. package/src/data-structures/graph/undirected-graph.ts +26 -26
  46. package/src/data-structures/hash/hash-map.ts +8 -8
  47. package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
  48. package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
  49. package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
  50. package/src/data-structures/queue/queue.ts +13 -13
  51. package/src/data-structures/stack/stack.ts +9 -9
  52. package/src/data-structures/trie/trie.ts +23 -4
  53. package/src/interfaces/binary-tree.ts +3 -3
  54. package/src/types/common.ts +11 -1
  55. package/src/types/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -6,8 +6,16 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
- import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
10
- import { BTNCallback, IterableEntriesOrKeys } from '../../types';
9
+ import type {
10
+ AVLTreeNested,
11
+ AVLTreeNodeNested,
12
+ AVLTreeOptions,
13
+ BiTreeDeleteResult,
14
+ BSTNodeKeyOrNode,
15
+ BTNKey,
16
+ BTNodeExemplar
17
+ } from '../../types';
18
+ import { BTNCallback } from '../../types';
11
19
  import { IBinaryTree } from '../../interfaces';
12
20
 
13
21
  export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
@@ -19,19 +27,32 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
19
27
  }
20
28
  }
21
29
 
30
+ /**
31
+ * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
32
+ * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
33
+ * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
34
+ * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
35
+ * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
36
+ * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
37
+ * 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.
38
+ * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
39
+ */
22
40
  export 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>>>
23
41
  extends BST<V, N, TREE>
24
42
  implements IBinaryTree<V, N, TREE> {
25
43
 
26
44
  /**
27
- * This is a constructor function for an AVL tree data structure in TypeScript.
28
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
29
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
30
- * options.
45
+ * The constructor function initializes an AVLTree object with optional elements and options.
46
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
47
+ * objects. It represents a collection of elements that will be added to the AVL tree during
48
+ * initialization.
49
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
50
+ * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
51
+ * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
31
52
  */
32
- constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<AVLTreeOptions>) {
53
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>) {
33
54
  super([], options);
34
- if (elements) this.init(elements);
55
+ if (elements) super.addMany(elements);
35
56
  }
36
57
 
37
58
  /**
@@ -47,6 +68,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
47
68
  return new AVLTreeNode<V, N>(key, value) as N;
48
69
  }
49
70
 
71
+ /**
72
+ * The function creates a new AVL tree with the specified options and returns it.
73
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
74
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
75
+ * being created.
76
+ * @returns a new AVLTree object.
77
+ */
50
78
  override createTree(options?: AVLTreeOptions): TREE {
51
79
  return new AVLTree<V, N, TREE>([], {
52
80
  iterationType: this.iterationType,
@@ -54,21 +82,24 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
54
82
  }) as TREE;
55
83
  }
56
84
 
85
+ /**
86
+ * 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.
87
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
88
+ */
89
+
57
90
  /**
58
91
  * 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.
59
92
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
60
93
  *
61
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
62
- * balances the structure if necessary.
63
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
64
- * `BTNKey`, `N`, `null`, or `undefined`.
65
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
66
- * added to the binary search tree.
67
- * @returns The method is returning either a node (N) or undefined.
94
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
95
+ * a new node.
96
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
97
+ * entry.
98
+ * @returns The method is returning either the inserted node or `undefined`.
68
99
  */
69
- override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
70
- if (keyOrNode === null) return undefined;
71
- const inserted = super.add(keyOrNode, value);
100
+ override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
101
+ if (keyOrNodeOrEntry === null) return undefined;
102
+ const inserted = super.add(keyOrNodeOrEntry);
72
103
  if (inserted) this._balancePath(inserted);
73
104
  return inserted;
74
105
  }
@@ -107,26 +138,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
107
138
  return deletedResults;
108
139
  }
109
140
 
110
- /**
111
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
112
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
113
- */
114
-
115
- init(elements: IterableEntriesOrKeys<V>): void {
116
- if (elements) {
117
- for (const entryOrKey of elements) {
118
- if (Array.isArray(entryOrKey)) {
119
- const [key, value] = entryOrKey;
120
- this.add(key, value);
121
- } else {
122
- this.add(entryOrKey);
123
- }
124
- }
125
- }
126
- }
127
141
 
128
142
  /**
129
- * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
143
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
130
144
  * tree.
131
145
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
132
146
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -135,9 +149,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
135
149
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
136
150
  * if either `srcNode` or `destNode` is undefined.
137
151
  */
138
- protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
139
- srcNode = this.ensureNotKey(srcNode);
140
- destNode = this.ensureNotKey(destNode);
152
+ protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
153
+ srcNode = this.ensureNode(srcNode);
154
+ destNode = this.ensureNode(destNode);
141
155
 
142
156
  if (srcNode && destNode) {
143
157
  const { key, value, height } = destNode;
@@ -450,4 +464,10 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
450
464
  B && this._updateHeight(B);
451
465
  C && this._updateHeight(C);
452
466
  }
467
+
468
+ protected _replaceNode(oldNode: N, newNode: N): N {
469
+ newNode.height = oldNode.height;
470
+
471
+ return super._replaceNode(oldNode, newNode)
472
+ }
453
473
  }