deque-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.
Files changed (71) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +36 -18
  2. package/dist/data-structures/binary-tree/avl-tree.js +46 -29
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
  4. package/dist/data-structures/binary-tree/binary-tree.js +182 -184
  5. package/dist/data-structures/binary-tree/bst.d.ts +73 -63
  6. package/dist/data-structures/binary-tree/bst.js +168 -169
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
  8. package/dist/data-structures/binary-tree/rb-tree.js +77 -31
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
  10. package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  13. package/dist/data-structures/hash/hash-map.js +5 -8
  14. package/dist/data-structures/heap/heap.d.ts +19 -21
  15. package/dist/data-structures/heap/heap.js +52 -34
  16. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  17. package/dist/data-structures/heap/max-heap.js +2 -2
  18. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/min-heap.js +2 -2
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  23. package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
  24. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  26. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  30. package/dist/data-structures/queue/deque.d.ts +1 -0
  31. package/dist/data-structures/queue/deque.js +3 -0
  32. package/dist/data-structures/queue/queue.d.ts +1 -0
  33. package/dist/data-structures/queue/queue.js +3 -0
  34. package/dist/data-structures/stack/stack.d.ts +2 -1
  35. package/dist/data-structures/stack/stack.js +10 -2
  36. package/dist/data-structures/trie/trie.d.ts +3 -0
  37. package/dist/data-structures/trie/trie.js +19 -4
  38. package/dist/interfaces/binary-tree.d.ts +4 -2
  39. package/dist/types/common.d.ts +7 -0
  40. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  41. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  42. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  43. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  44. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  45. package/package.json +2 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +61 -31
  47. package/src/data-structures/binary-tree/binary-tree.ts +283 -254
  48. package/src/data-structures/binary-tree/bst.ts +193 -170
  49. package/src/data-structures/binary-tree/rb-tree.ts +87 -32
  50. package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
  51. package/src/data-structures/graph/abstract-graph.ts +1 -1
  52. package/src/data-structures/hash/hash-map.ts +8 -8
  53. package/src/data-structures/heap/heap.ts +57 -39
  54. package/src/data-structures/heap/max-heap.ts +5 -5
  55. package/src/data-structures/heap/min-heap.ts +5 -5
  56. package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
  57. package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
  58. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  59. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  60. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  61. package/src/data-structures/queue/deque.ts +4 -0
  62. package/src/data-structures/queue/queue.ts +4 -0
  63. package/src/data-structures/stack/stack.ts +12 -3
  64. package/src/data-structures/trie/trie.ts +23 -4
  65. package/src/interfaces/binary-tree.ts +14 -2
  66. package/src/types/common.ts +15 -1
  67. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  68. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  69. package/src/types/data-structures/hash/hash-map.ts +1 -2
  70. package/src/types/data-structures/heap/heap.ts +3 -1
  71. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -14,7 +14,12 @@ class Stack {
14
14
  * is provided and is an array, it is assigned to the `_elements
15
15
  */
16
16
  constructor(elements) {
17
- this._elements = Array.isArray(elements) ? elements : [];
17
+ this._elements = [];
18
+ if (elements) {
19
+ for (const el of elements) {
20
+ this.push(el);
21
+ }
22
+ }
18
23
  }
19
24
  get elements() {
20
25
  return this._elements;
@@ -137,7 +142,7 @@ class Stack {
137
142
  * @returns An iterator object.
138
143
  */
139
144
  *[Symbol.iterator]() {
140
- for (let i = this.elements.length - 1; i >= 0; i--) {
145
+ for (let i = 0; i < this.elements.length; i++) {
141
146
  yield this.elements[i];
142
147
  }
143
148
  }
@@ -181,5 +186,8 @@ class Stack {
181
186
  }
182
187
  return accumulator;
183
188
  }
189
+ print() {
190
+ console.log([...this]);
191
+ }
184
192
  }
185
193
  exports.Stack = Stack;
@@ -20,6 +20,8 @@ export declare class TrieNode {
20
20
  */
21
21
  export declare class Trie {
22
22
  constructor(words?: string[], caseSensitive?: boolean);
23
+ protected _size: number;
24
+ get size(): number;
23
25
  protected _caseSensitive: boolean;
24
26
  get caseSensitive(): boolean;
25
27
  protected _root: TrieNode;
@@ -145,6 +147,7 @@ export declare class Trie {
145
147
  filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
146
148
  map(callback: (word: string, index: number, trie: this) => string): Trie;
147
149
  reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
150
+ print(): void;
148
151
  /**
149
152
  * Time Complexity: O(M), where M is the length of the input string.
150
153
  * Space Complexity: O(1) - Constant space.
@@ -27,12 +27,16 @@ class Trie {
27
27
  constructor(words, caseSensitive = true) {
28
28
  this._root = new TrieNode('');
29
29
  this._caseSensitive = caseSensitive;
30
+ this._size = 0;
30
31
  if (words) {
31
- for (const i of words) {
32
- this.add(i);
32
+ for (const word of words) {
33
+ this.add(word);
33
34
  }
34
35
  }
35
36
  }
37
+ get size() {
38
+ return this._size;
39
+ }
36
40
  get caseSensitive() {
37
41
  return this._caseSensitive;
38
42
  }
@@ -54,6 +58,7 @@ class Trie {
54
58
  add(word) {
55
59
  word = this._caseProcess(word);
56
60
  let cur = this.root;
61
+ let isNewWord = false;
57
62
  for (const c of word) {
58
63
  let nodeC = cur.children.get(c);
59
64
  if (!nodeC) {
@@ -62,8 +67,12 @@ class Trie {
62
67
  }
63
68
  cur = nodeC;
64
69
  }
65
- cur.isEnd = true;
66
- return true;
70
+ if (!cur.isEnd) {
71
+ isNewWord = true;
72
+ cur.isEnd = true;
73
+ this._size++;
74
+ }
75
+ return isNewWord;
67
76
  }
68
77
  /**
69
78
  * Time Complexity: O(M), where M is the length of the input word.
@@ -130,6 +139,9 @@ class Trie {
130
139
  return false;
131
140
  };
132
141
  dfs(this.root, 0);
142
+ if (isDeleted) {
143
+ this._size--;
144
+ }
133
145
  return isDeleted;
134
146
  }
135
147
  /**
@@ -352,6 +364,9 @@ class Trie {
352
364
  }
353
365
  return accumulator;
354
366
  }
367
+ print() {
368
+ console.log([...this]);
369
+ }
355
370
  /**
356
371
  * Time Complexity: O(M), where M is the length of the input string.
357
372
  * Space Complexity: O(1) - Constant space.
@@ -1,7 +1,9 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeNested, BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
2
+ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
3
3
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
4
4
  createNode(key: BTNKey, value?: N['value']): N;
5
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
5
+ createTree(options?: Partial<BinaryTreeOptions>): TREE;
6
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
7
+ addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
6
8
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
7
9
  }
@@ -1,3 +1,4 @@
1
+ import { BTNKey } from "./data-structures";
1
2
  export type Comparator<T> = (a: T, b: T) => number;
2
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
3
4
  export type BTNCallback<N, D = any> = (node: N) => D;
@@ -18,3 +19,9 @@ export type BinaryTreePrintOptions = {
18
19
  isShowNull?: boolean;
19
20
  isShowRedBlackNIL?: boolean;
20
21
  };
22
+ export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
23
+ export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
24
+ export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
25
+ export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
26
+ export type BTNodePureKeyOrNode<N> = BTNKey | N;
27
+ export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
@@ -26,6 +26,6 @@ export type BiTreeDeleteResult<N> = {
26
26
  export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
27
27
  export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
28
28
  export type BinaryTreeOptions = {
29
- iterationType?: IterationType;
29
+ iterationType: IterationType;
30
30
  };
31
31
  export type NodeDisplayLayout = [string[], number, number, number];
@@ -1,8 +1,8 @@
1
1
  import { BST, BSTNode } from '../../../data-structures';
2
2
  import type { BinaryTreeOptions, BTNKey } from './binary-tree';
3
- export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
3
+ import { Comparator } from "../../common";
4
4
  export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
6
  export type BSTOptions = BinaryTreeOptions & {
7
- comparator?: BSTComparator;
7
+ comparator: Comparator<BTNKey>;
8
8
  };
@@ -4,8 +4,7 @@ export type HashMapLinkedNode<K, V> = {
4
4
  next: HashMapLinkedNode<K, V>;
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
- export type HashMapOptions<K, V> = {
8
- elements: Iterable<[K, V]>;
7
+ export type HashMapOptions<K> = {
9
8
  hashFn: (key: K) => string;
10
9
  objHashFn: (key: K) => object;
11
10
  };
@@ -1 +1,4 @@
1
- export {};
1
+ import { Comparator } from "../../common";
2
+ export type HeapOptions<T> = {
3
+ comparator: Comparator<T>;
4
+ };
@@ -1 +1,2 @@
1
- export {};
1
+ import { HeapOptions } from "../heap";
2
+ export type PriorityQueueOptions<T> = HeapOptions<T> & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deque-typed",
3
- "version": "1.47.5",
3
+ "version": "1.47.7",
4
4
  "description": "Deque. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -119,6 +119,6 @@
119
119
  "typescript": "^4.9.5"
120
120
  },
121
121
  "dependencies": {
122
- "data-structure-typed": "^1.47.4"
122
+ "data-structure-typed": "^1.47.7"
123
123
  }
124
124
  }
@@ -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, IterationType } 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,25 +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
- override options: AVLTreeOptions;
27
-
28
44
  /**
29
- * This is a constructor function for an AVL tree data structure in TypeScript.
30
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
31
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
32
- * 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.
33
52
  */
34
- constructor(options?: AVLTreeOptions) {
35
- super(options);
36
- if (options) {
37
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
38
- } else {
39
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
40
- }
53
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>) {
54
+ super([], options);
55
+ if (elements) super.addMany(elements);
41
56
  }
42
57
 
43
58
  /**
@@ -53,8 +68,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
53
68
  return new AVLTreeNode<V, N>(key, value) as N;
54
69
  }
55
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
+ */
56
78
  override createTree(options?: AVLTreeOptions): TREE {
57
- return new AVLTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
79
+ return new AVLTree<V, N, TREE>([], {
80
+ iterationType: this.iterationType,
81
+ comparator: this.comparator, ...options
82
+ }) as TREE;
58
83
  }
59
84
 
60
85
  /**
@@ -66,23 +91,21 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
66
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.
67
92
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
68
93
  *
69
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
70
- * balances the structure if necessary.
71
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
72
- * `BTNKey`, `N`, `null`, or `undefined`.
73
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
74
- * added to the binary search tree.
75
- * @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`.
76
99
  */
77
- override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
78
- if (keyOrNode === null) return undefined;
79
- 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);
80
103
  if (inserted) this._balancePath(inserted);
81
104
  return inserted;
82
105
  }
83
106
 
84
107
  /**
85
- * 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.
108
+ * 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.
86
109
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
87
110
  */
88
111
 
@@ -115,8 +138,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
115
138
  return deletedResults;
116
139
  }
117
140
 
141
+
118
142
  /**
119
- * 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
120
144
  * tree.
121
145
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
122
146
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -125,9 +149,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
125
149
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
126
150
  * if either `srcNode` or `destNode` is undefined.
127
151
  */
128
- protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
129
- srcNode = this.ensureNotKey(srcNode);
130
- 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);
131
155
 
132
156
  if (srcNode && destNode) {
133
157
  const { key, value, height } = destNode;
@@ -440,4 +464,10 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
440
464
  B && this._updateHeight(B);
441
465
  C && this._updateHeight(C);
442
466
  }
467
+
468
+ protected _replaceNode(oldNode: N, newNode: N): N {
469
+ newNode.height = oldNode.height;
470
+
471
+ return super._replaceNode(oldNode, newNode)
472
+ }
443
473
  }