graph-typed 1.39.5 → 1.40.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.
Files changed (96) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
  2. package/dist/data-structures/binary-tree/avl-tree.js +13 -14
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
  6. package/dist/data-structures/binary-tree/binary-tree.js +25 -46
  7. package/dist/data-structures/binary-tree/bst.d.ts +7 -7
  8. package/dist/data-structures/binary-tree/bst.js +16 -16
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
  10. package/dist/data-structures/binary-tree/rb-tree.js +5 -11
  11. package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
  12. package/dist/data-structures/binary-tree/segment-tree.js +20 -68
  13. package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
  14. package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
  15. package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
  16. package/dist/data-structures/graph/abstract-graph.js +18 -57
  17. package/dist/data-structures/graph/directed-graph.d.ts +16 -22
  18. package/dist/data-structures/graph/directed-graph.js +17 -35
  19. package/dist/data-structures/graph/map-graph.d.ts +13 -19
  20. package/dist/data-structures/graph/map-graph.js +15 -33
  21. package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
  22. package/dist/data-structures/graph/undirected-graph.js +15 -28
  23. package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
  24. package/dist/data-structures/hash/coordinate-map.js +0 -3
  25. package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
  26. package/dist/data-structures/hash/coordinate-set.js +0 -3
  27. package/dist/data-structures/hash/hash-map.d.ts +8 -14
  28. package/dist/data-structures/hash/hash-map.js +4 -22
  29. package/dist/data-structures/hash/hash-table.d.ts +10 -13
  30. package/dist/data-structures/hash/hash-table.js +8 -17
  31. package/dist/data-structures/heap/heap.d.ts +12 -6
  32. package/dist/data-structures/heap/heap.js +40 -22
  33. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
  34. package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
  35. package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
  36. package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
  37. package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
  38. package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
  39. package/dist/data-structures/matrix/matrix.d.ts +1 -1
  40. package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
  41. package/dist/data-structures/matrix/navigator.d.ts +4 -4
  42. package/dist/data-structures/queue/deque.d.ts +8 -12
  43. package/dist/data-structures/queue/deque.js +31 -43
  44. package/dist/data-structures/queue/queue.d.ts +20 -5
  45. package/dist/data-structures/queue/queue.js +35 -18
  46. package/dist/data-structures/stack/stack.d.ts +2 -1
  47. package/dist/data-structures/stack/stack.js +10 -7
  48. package/dist/data-structures/tree/tree.d.ts +3 -9
  49. package/dist/data-structures/tree/tree.js +3 -21
  50. package/dist/data-structures/trie/trie.d.ts +6 -12
  51. package/dist/data-structures/trie/trie.js +6 -24
  52. package/dist/interfaces/binary-tree.d.ts +3 -3
  53. package/dist/interfaces/graph.d.ts +2 -2
  54. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  55. package/package.json +2 -2
  56. package/src/data-structures/binary-tree/avl-tree.ts +15 -17
  57. package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
  58. package/src/data-structures/binary-tree/binary-tree.ts +35 -60
  59. package/src/data-structures/binary-tree/bst.ts +21 -22
  60. package/src/data-structures/binary-tree/rb-tree.ts +19 -27
  61. package/src/data-structures/binary-tree/segment-tree.ts +25 -92
  62. package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
  63. package/src/data-structures/graph/abstract-graph.ts +42 -88
  64. package/src/data-structures/graph/directed-graph.ts +29 -55
  65. package/src/data-structures/graph/map-graph.ts +20 -45
  66. package/src/data-structures/graph/undirected-graph.ts +24 -41
  67. package/src/data-structures/hash/coordinate-map.ts +0 -4
  68. package/src/data-structures/hash/coordinate-set.ts +0 -4
  69. package/src/data-structures/hash/hash-map.ts +13 -37
  70. package/src/data-structures/hash/hash-table.ts +15 -27
  71. package/src/data-structures/hash/tree-map.ts +2 -1
  72. package/src/data-structures/hash/tree-set.ts +2 -1
  73. package/src/data-structures/heap/heap.ts +58 -30
  74. package/src/data-structures/heap/max-heap.ts +1 -1
  75. package/src/data-structures/heap/min-heap.ts +1 -1
  76. package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
  77. package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
  78. package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
  79. package/src/data-structures/matrix/matrix.ts +2 -2
  80. package/src/data-structures/matrix/matrix2d.ts +1 -1
  81. package/src/data-structures/matrix/navigator.ts +4 -4
  82. package/src/data-structures/matrix/vector2d.ts +2 -1
  83. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  84. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  85. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  86. package/src/data-structures/queue/deque.ts +38 -53
  87. package/src/data-structures/queue/queue.ts +38 -20
  88. package/src/data-structures/stack/stack.ts +13 -9
  89. package/src/data-structures/tree/tree.ts +7 -33
  90. package/src/data-structures/trie/trie.ts +14 -40
  91. package/src/interfaces/binary-tree.ts +3 -3
  92. package/src/interfaces/graph.ts +2 -2
  93. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  94. package/src/types/data-structures/matrix/navigator.ts +1 -1
  95. package/src/types/utils/utils.ts +1 -1
  96. package/src/types/utils/validate-type.ts +2 -2
@@ -16,6 +16,9 @@ class Stack {
16
16
  constructor(elements) {
17
17
  this._elements = Array.isArray(elements) ? elements : [];
18
18
  }
19
+ get elements() {
20
+ return this._elements;
21
+ }
19
22
  /**
20
23
  * The function "fromArray" creates a new Stack object from an array of elements.
21
24
  * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
@@ -30,14 +33,14 @@ class Stack {
30
33
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
31
34
  */
32
35
  isEmpty() {
33
- return this._elements.length === 0;
36
+ return this.elements.length === 0;
34
37
  }
35
38
  /**
36
39
  * The size() function returns the number of elements in an array.
37
40
  * @returns The size of the elements array.
38
41
  */
39
42
  size() {
40
- return this._elements.length;
43
+ return this.elements.length;
41
44
  }
42
45
  /**
43
46
  * The `peek` function returns the last element of an array, or null if the array is empty.
@@ -46,7 +49,7 @@ class Stack {
46
49
  peek() {
47
50
  if (this.isEmpty())
48
51
  return null;
49
- return this._elements[this._elements.length - 1];
52
+ return this.elements[this.elements.length - 1];
50
53
  }
51
54
  /**
52
55
  * The push function adds an element to the stack and returns the updated stack.
@@ -54,7 +57,7 @@ class Stack {
54
57
  * @returns The `push` method is returning the updated `Stack<E>` object.
55
58
  */
56
59
  push(element) {
57
- this._elements.push(element);
60
+ this.elements.push(element);
58
61
  return this;
59
62
  }
60
63
  /**
@@ -65,14 +68,14 @@ class Stack {
65
68
  pop() {
66
69
  if (this.isEmpty())
67
70
  return null;
68
- return this._elements.pop() || null;
71
+ return this.elements.pop() || null;
69
72
  }
70
73
  /**
71
74
  * The toArray function returns a copy of the elements in an array.
72
75
  * @returns An array of type E.
73
76
  */
74
77
  toArray() {
75
- return this._elements.slice();
78
+ return this.elements.slice();
76
79
  }
77
80
  /**
78
81
  * The clear function clears the elements array.
@@ -85,7 +88,7 @@ class Stack {
85
88
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
86
89
  */
87
90
  clone() {
88
- return new Stack(this._elements.slice());
91
+ return new Stack(this.elements.slice());
89
92
  }
90
93
  }
91
94
  exports.Stack = Stack;
@@ -1,14 +1,8 @@
1
1
  export declare class TreeNode<V = any> {
2
+ key: string;
3
+ value?: V | undefined;
4
+ children?: TreeNode<V>[] | undefined;
2
5
  constructor(key: string, value?: V, children?: TreeNode<V>[]);
3
- private _key;
4
- get key(): string;
5
- set key(value: string);
6
- private _value?;
7
- get value(): V | undefined;
8
- set value(value: V | undefined);
9
- private _children?;
10
- get children(): TreeNode<V>[] | undefined;
11
- set children(value: TreeNode<V>[] | undefined);
12
6
  addChildren(children: TreeNode<V> | TreeNode<V>[]): void;
13
7
  getHeight(): number;
14
8
  }
@@ -3,27 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TreeNode = void 0;
4
4
  class TreeNode {
5
5
  constructor(key, value, children) {
6
- this._key = key;
7
- this._value = value || undefined;
8
- this._children = children || [];
9
- }
10
- get key() {
11
- return this._key;
12
- }
13
- set key(value) {
14
- this._key = value;
15
- }
16
- get value() {
17
- return this._value;
18
- }
19
- set value(value) {
20
- this._value = value;
21
- }
22
- get children() {
23
- return this._children;
24
- }
25
- set children(value) {
26
- this._children = value;
6
+ this.key = key;
7
+ this.value = value || undefined;
8
+ this.children = children || [];
27
9
  }
28
10
  addChildren(children) {
29
11
  if (!this.children) {
@@ -10,26 +10,20 @@
10
10
  * and a flag indicating whether it's the end of a word.
11
11
  */
12
12
  export declare class TrieNode {
13
+ key: string;
14
+ children: Map<string, TrieNode>;
15
+ isEnd: boolean;
13
16
  constructor(key: string);
14
- private _key;
15
- get key(): string;
16
- set key(v: string);
17
- protected _children: Map<string, TrieNode>;
18
- get children(): Map<string, TrieNode>;
19
- set children(v: Map<string, TrieNode>);
20
- protected _isEnd: boolean;
21
- get isEnd(): boolean;
22
- set isEnd(v: boolean);
23
17
  }
24
18
  /**
25
19
  * Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
26
20
  */
27
21
  export declare class Trie {
28
- private readonly _caseSensitive;
29
22
  constructor(words?: string[], caseSensitive?: boolean);
23
+ protected _caseSensitive: boolean;
24
+ get caseSensitive(): boolean;
30
25
  protected _root: TrieNode;
31
26
  get root(): TrieNode;
32
- set root(v: TrieNode);
33
27
  /**
34
28
  * Add a word to the Trie structure.
35
29
  * @param {string} word - The word to add.
@@ -81,5 +75,5 @@ export declare class Trie {
81
75
  * @returns {string[]} an array of strings.
82
76
  */
83
77
  getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
84
- private _caseProcess;
78
+ protected _caseProcess(str: string): string;
85
79
  }
@@ -14,27 +14,9 @@ exports.Trie = exports.TrieNode = void 0;
14
14
  */
15
15
  class TrieNode {
16
16
  constructor(key) {
17
- this._key = key;
18
- this._isEnd = false;
19
- this._children = new Map();
20
- }
21
- get key() {
22
- return this._key;
23
- }
24
- set key(v) {
25
- this._key = v;
26
- }
27
- get children() {
28
- return this._children;
29
- }
30
- set children(v) {
31
- this._children = v;
32
- }
33
- get isEnd() {
34
- return this._isEnd;
35
- }
36
- set isEnd(v) {
37
- this._isEnd = v;
17
+ this.key = key;
18
+ this.isEnd = false;
19
+ this.children = new Map();
38
20
  }
39
21
  }
40
22
  exports.TrieNode = TrieNode;
@@ -51,12 +33,12 @@ class Trie {
51
33
  }
52
34
  }
53
35
  }
36
+ get caseSensitive() {
37
+ return this._caseSensitive;
38
+ }
54
39
  get root() {
55
40
  return this._root;
56
41
  }
57
- set root(v) {
58
- this._root = v;
59
- }
60
42
  /**
61
43
  * Add a word to the Trie structure.
62
44
  * @param {string} word - The word to add.
@@ -1,7 +1,7 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback } from '../types';
2
+ import { BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey } from '../types';
3
3
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
4
- createNode(key: BTNKey, val?: N['val']): N;
5
- add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
4
+ createNode(key: BTNKey, value?: N['value']): N;
5
+ add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
6
6
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
7
7
  }
@@ -1,5 +1,5 @@
1
1
  import { VertexKey } from '../types';
2
2
  export interface IGraph<V, E, VO, EO> {
3
- createVertex(key: VertexKey, val?: V): VO;
4
- createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): EO;
3
+ createVertex(key: VertexKey, value?: V): VO;
4
+ createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
5
5
  }
@@ -1,5 +1,5 @@
1
1
  import { BSTNode } from '../../../data-structures';
2
- import type { BTNKey, BinaryTreeOptions } from './binary-tree';
2
+ import type { BinaryTreeOptions, BTNKey } from './binary-tree';
3
3
  export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
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 BSTOptions = BinaryTreeOptions & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graph-typed",
3
- "version": "1.39.5",
3
+ "version": "1.40.0",
4
4
  "description": "Graph. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -135,6 +135,6 @@
135
135
  "typescript": "^4.9.5"
136
136
  },
137
137
  "dependencies": {
138
- "data-structure-typed": "^1.39.5"
138
+ "data-structure-typed": "^1.40.0"
139
139
  }
140
140
  }
@@ -13,16 +13,15 @@ import {IBinaryTree} from '../../interfaces';
13
13
  export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
14
14
  height: number;
15
15
 
16
- constructor(key: BTNKey, val?: V) {
17
- super(key, val);
16
+ constructor(key: BTNKey, value?: V) {
17
+ super(key, value);
18
18
  this.height = 0;
19
19
  }
20
20
  }
21
21
 
22
22
  export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N>
25
- {
24
+ implements IBinaryTree<V, N> {
26
25
  /**
27
26
  * This is a constructor function for an AVL tree data structure in TypeScript.
28
27
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -37,13 +36,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
37
36
  * The function creates a new AVL tree node with the specified key and value.
38
37
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
39
38
  * the new node. It is used to determine the position of the node in the binary search tree.
40
- * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
41
- * type `V`, which means it can be any value that is assignable to the `val` property of the
39
+ * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
40
+ * type `V`, which means it can be any value that is assignable to the `value` property of the
42
41
  * node type `N`.
43
42
  * @returns a new AVLTreeNode object with the specified key and value.
44
43
  */
45
- override createNode(key: BTNKey, val?: V): N {
46
- return new AVLTreeNode<V, N>(key, val) as N;
44
+ override createNode(key: BTNKey, value?: V): N {
45
+ return new AVLTreeNode<V, N>(key, value) as N;
47
46
  }
48
47
 
49
48
  /**
@@ -51,13 +50,12 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
51
50
  * a new node.
52
51
  * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
53
52
  * `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
54
- * @param [val] - The `val` parameter is the value that you want to assign to the new node that you
53
+ * @param [value] - The `value` parameter is the value that you want to assign to the new node that you
55
54
  * are adding to the binary search tree.
56
55
  * @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
57
56
  */
58
- override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
59
- // TODO support node as a param
60
- const inserted = super.add(keyOrNode, val);
57
+ override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
58
+ const inserted = super.add(keyOrNode, value);
61
59
  if (inserted) this._balancePath(inserted);
62
60
  return inserted;
63
61
  }
@@ -97,18 +95,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
97
95
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
98
96
  */
99
97
  protected override _swap(srcNode: N, destNode: N): N {
100
- const {key, val, height} = destNode;
101
- const tempNode = this.createNode(key, val);
98
+ const {key, value, height} = destNode;
99
+ const tempNode = this.createNode(key, value);
102
100
 
103
101
  if (tempNode) {
104
102
  tempNode.height = height;
105
103
 
106
104
  destNode.key = srcNode.key;
107
- destNode.val = srcNode.val;
105
+ destNode.value = srcNode.value;
108
106
  destNode.height = srcNode.height;
109
107
 
110
108
  srcNode.key = tempNode.key;
111
- srcNode.val = tempNode.val;
109
+ srcNode.value = tempNode.value;
112
110
  srcNode.height = tempNode.height;
113
111
  }
114
112
 
@@ -162,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
162
160
  // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
163
161
  switch (
164
162
  this._balanceFactor(A) // second O(1)
165
- ) {
163
+ ) {
166
164
  case -2:
167
165
  if (A && A.left) {
168
166
  if (this._balanceFactor(A.left) <= 0) {
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
17
17
  * @param - - `frequency`: The default frequency value. It is optional and has a default
18
18
  * value of 0.
19
19
  */
20
- constructor({frequency = 0, max}: {frequency?: number; max: number}) {
20
+ constructor({frequency = 0, max}: { frequency?: number; max: number }) {
21
21
  this._freq = frequency;
22
22
  this._max = max;
23
23
  this._freqMap = {0: 0};
@@ -31,30 +31,18 @@ export class BinaryIndexedTree {
31
31
  return this._freqMap;
32
32
  }
33
33
 
34
- set freqMap(value: Record<number, number>) {
35
- this._freqMap = value;
36
- }
37
-
38
34
  protected _msb: number;
39
35
 
40
36
  get msb(): number {
41
37
  return this._msb;
42
38
  }
43
39
 
44
- set msb(value: number) {
45
- this._msb = value;
46
- }
47
-
48
40
  protected _negativeCount: number;
49
41
 
50
42
  get negativeCount(): number {
51
43
  return this._negativeCount;
52
44
  }
53
45
 
54
- set negativeCount(value: number) {
55
- this._negativeCount = value;
56
- }
57
-
58
46
  get freq(): number {
59
47
  return this._freq;
60
48
  }
@@ -232,9 +220,9 @@ export class BinaryIndexedTree {
232
220
  */
233
221
  protected _updateNegativeCount(freqCur: number, freqNew: number): void {
234
222
  if (freqCur < 0 && freqNew >= 0) {
235
- this.negativeCount--;
223
+ this._negativeCount--;
236
224
  } else if (freqCur >= 0 && freqNew < 0) {
237
- this.negativeCount++;
225
+ this._negativeCount++;
238
226
  }
239
227
  }
240
228
 
@@ -26,7 +26,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
26
26
  /**
27
27
  * The value stored in the node.
28
28
  */
29
- val: V | undefined;
29
+ value: V | undefined;
30
30
 
31
31
  /**
32
32
  * The parent node of the current node.
@@ -36,14 +36,14 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
36
36
  /**
37
37
  * Creates a new instance of BinaryTreeNode.
38
38
  * @param {BTNKey} key - The key associated with the node.
39
- * @param {V} val - The value stored in the node.
39
+ * @param {V} value - The value stored in the node.
40
40
  */
41
- constructor(key: BTNKey, val?: V) {
41
+ constructor(key: BTNKey, value?: V) {
42
42
  this.key = key;
43
- this.val = val;
43
+ this.value = value;
44
44
  }
45
45
 
46
- private _left: N | null | undefined;
46
+ protected _left: N | null | undefined;
47
47
 
48
48
  /**
49
49
  * Get the left child node.
@@ -63,7 +63,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
63
63
  this._left = v;
64
64
  }
65
65
 
66
- private _right: N | null | undefined;
66
+ protected _right: N | null | undefined;
67
67
 
68
68
  /**
69
69
  * Get the right child node.
@@ -108,8 +108,9 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
108
108
  * @template N - The type of the binary tree's nodes.
109
109
  */
110
110
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
111
- implements IBinaryTree<V, N>
112
- {
111
+ implements IBinaryTree<V, N> {
112
+ iterationType: IterationType = IterationType.ITERATIVE;
113
+
113
114
  /**
114
115
  * Creates a new instance of BinaryTree.
115
116
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -117,28 +118,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
117
118
  constructor(options?: BinaryTreeOptions) {
118
119
  if (options !== undefined) {
119
120
  const {iterationType = IterationType.ITERATIVE} = options;
120
- this._iterationType = iterationType;
121
+ this.iterationType = iterationType;
121
122
  }
122
123
  }
123
124
 
124
- private _iterationType: IterationType = IterationType.ITERATIVE;
125
-
126
- /**
127
- * Get the iteration type used in the binary tree.
128
- */
129
- get iterationType(): IterationType {
130
- return this._iterationType;
131
- }
132
-
133
- /**
134
- * Set the iteration type for the binary tree.
135
- * @param {IterationType} v - The new iteration type to set.
136
- */
137
- set iterationType(v: IterationType) {
138
- this._iterationType = v;
139
- }
140
-
141
- private _root: N | null = null;
125
+ protected _root: N | null = null;
142
126
 
143
127
  /**
144
128
  * Get the root node of the binary tree.
@@ -147,7 +131,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
147
131
  return this._root;
148
132
  }
149
133
 
150
- private _size = 0;
134
+ protected _size = 0;
151
135
 
152
136
  /**
153
137
  * Get the number of nodes in the binary tree.
@@ -159,18 +143,18 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
159
143
  /**
160
144
  * Creates a new instance of BinaryTreeNode with the given key and value.
161
145
  * @param {BTNKey} key - The key for the new node.
162
- * @param {V} val - The value for the new node.
146
+ * @param {V} value - The value for the new node.
163
147
  * @returns {N} - The newly created BinaryTreeNode.
164
148
  */
165
- createNode(key: BTNKey, val?: V): N {
166
- return new BinaryTreeNode<V, N>(key, val) as N;
149
+ createNode(key: BTNKey, value?: V): N {
150
+ return new BinaryTreeNode<V, N>(key, value) as N;
167
151
  }
168
152
 
169
153
  /**
170
154
  * Clear the binary tree, removing all nodes.
171
155
  */
172
156
  clear() {
173
- this._root = null;
157
+ this._setRoot(null);
174
158
  this._size = 0;
175
159
  }
176
160
 
@@ -185,10 +169,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
185
169
  /**
186
170
  * Add a node with the given key and value to the binary tree.
187
171
  * @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
188
- * @param {V} val - The value for the new node (optional).
172
+ * @param {V} value - The value for the new node (optional).
189
173
  * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
190
174
  */
191
- add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
175
+ add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
192
176
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
193
177
  const queue = new Queue<N | null>([root]);
194
178
  while (queue.size > 0) {
@@ -209,7 +193,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
209
193
  if (keyOrNode === null) {
210
194
  needInsert = null;
211
195
  } else if (typeof keyOrNode === 'number') {
212
- needInsert = this.createNode(keyOrNode, val);
196
+ needInsert = this.createNode(keyOrNode, value);
213
197
  } else if (keyOrNode instanceof BinaryTreeNode) {
214
198
  needInsert = keyOrNode;
215
199
  } else {
@@ -221,7 +205,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
221
205
 
222
206
  if (this.root) {
223
207
  if (existNode) {
224
- existNode.val = val;
208
+ existNode.value = value;
225
209
  inserted = existNode;
226
210
  } else {
227
211
  inserted = _bfs(this.root, needInsert);
@@ -229,9 +213,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
229
213
  } else {
230
214
  this._setRoot(needInsert);
231
215
  if (needInsert !== null) {
232
- this._setSize(1);
216
+ this._size = 1;
233
217
  } else {
234
- this._setSize(0);
218
+ this._size = 0;
235
219
  }
236
220
  inserted = this.root;
237
221
  }
@@ -252,15 +236,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
252
236
  // TODO not sure addMany not be run multi times
253
237
  return keysOrNodes.map((keyOrNode, i) => {
254
238
  if (keyOrNode instanceof BinaryTreeNode) {
255
- return this.add(keyOrNode.key, keyOrNode.val);
239
+ return this.add(keyOrNode.key, keyOrNode.value);
256
240
  }
257
241
 
258
242
  if (keyOrNode === null) {
259
243
  return this.add(null);
260
244
  }
261
245
 
262
- const val = values?.[i];
263
- return this.add(keyOrNode, val);
246
+ const value = values?.[i];
247
+ return this.add(keyOrNode, value);
264
248
  });
265
249
  }
266
250
 
@@ -339,7 +323,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
339
323
  }
340
324
  }
341
325
  }
342
- this._setSize(this.size - 1);
326
+ this._size = this.size - 1;
343
327
 
344
328
  bstDeletedResult.push({deleted: orgCurrent, needBalanced});
345
329
  return bstDeletedResult;
@@ -401,7 +385,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
401
385
  return -1;
402
386
  }
403
387
 
404
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
388
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
405
389
  let maxHeight = 0;
406
390
 
407
391
  while (stack.length > 0) {
@@ -904,7 +888,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
904
888
  _traverse(beginRoot);
905
889
  } else {
906
890
  // 0: visit, 1: print
907
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
891
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
908
892
 
909
893
  while (stack.length > 0) {
910
894
  const cur = stack.pop();
@@ -1174,7 +1158,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1174
1158
  * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1175
1159
  * binary tree nodes in a specific order.
1176
1160
  */
1177
- *[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1161
+ * [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1178
1162
  if (!node) {
1179
1163
  return;
1180
1164
  }
@@ -1212,15 +1196,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1212
1196
  * @returns {N} - The destination node after the swap.
1213
1197
  */
1214
1198
  protected _swap(srcNode: N, destNode: N): N {
1215
- const {key, val} = destNode;
1216
- const tempNode = this.createNode(key, val);
1199
+ const {key, value} = destNode;
1200
+ const tempNode = this.createNode(key, value);
1217
1201
 
1218
1202
  if (tempNode) {
1219
1203
  destNode.key = srcNode.key;
1220
- destNode.val = srcNode.val;
1204
+ destNode.value = srcNode.value;
1221
1205
 
1222
1206
  srcNode.key = tempNode.key;
1223
- srcNode.val = tempNode.val;
1207
+ srcNode.value = tempNode.value;
1224
1208
  }
1225
1209
 
1226
1210
  return destNode;
@@ -1244,13 +1228,13 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1244
1228
  if (parent.left === undefined) {
1245
1229
  parent.left = newNode;
1246
1230
  if (newNode) {
1247
- this._setSize(this.size + 1);
1231
+ this._size = this.size + 1;
1248
1232
  }
1249
1233
  return parent.left;
1250
1234
  } else if (parent.right === undefined) {
1251
1235
  parent.right = newNode;
1252
1236
  if (newNode) {
1253
- this._setSize(this.size + 1);
1237
+ this._size = this.size + 1;
1254
1238
  }
1255
1239
  return parent.right;
1256
1240
  } else {
@@ -1274,14 +1258,5 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1274
1258
  this._root = v;
1275
1259
  }
1276
1260
 
1277
- /**
1278
- * The function sets the value of the protected property "_size" to the given number.
1279
- * @param {number} v - The parameter "v" is a number that represents the size value that we want to
1280
- * set.
1281
- */
1282
- protected _setSize(v: number) {
1283
- this._size = v;
1284
- }
1285
-
1286
1261
  // --- end additional methods ---
1287
1262
  }