min-heap-typed 1.41.6 → 1.41.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.
@@ -26,6 +26,8 @@ export declare class RedBlackTree {
26
26
  constructor();
27
27
  protected _root: RBTreeNode;
28
28
  get root(): RBTreeNode;
29
+ protected _size: number;
30
+ get size(): number;
29
31
  /**
30
32
  * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
31
33
  * violations of the red-black tree properties.
@@ -33,11 +35,11 @@ export declare class RedBlackTree {
33
35
  * the RBTree.
34
36
  * @returns The function does not explicitly return anything.
35
37
  */
36
- insert(key: number): void;
38
+ add(key: number): void;
37
39
  /**
38
40
  * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
39
41
  * tree.
40
- * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
42
+ * @param {number} key - The `node` parameter is of type `RBTreeNode` and represents the current
41
43
  * node being processed in the delete operation.
42
44
  * @returns The `delete` function does not return anything. It has a return type of `void`.
43
45
  */
@@ -80,6 +82,7 @@ export declare class RedBlackTree {
80
82
  * @returns the predecessor of the given RBTreeNode 'x'.
81
83
  */
82
84
  getPredecessor(x: RBTreeNode): RBTreeNode;
85
+ clear(): void;
83
86
  print(beginRoot?: RBTreeNode): void;
84
87
  /**
85
88
  * The function performs a left rotation on a red-black tree node.
@@ -30,11 +30,15 @@ exports.NIL = new RBTreeNode(0);
30
30
  */
31
31
  class RedBlackTree {
32
32
  constructor() {
33
+ this._size = 0;
33
34
  this._root = exports.NIL;
34
35
  }
35
36
  get root() {
36
37
  return this._root;
37
38
  }
39
+ get size() {
40
+ return this._size;
41
+ }
38
42
  /**
39
43
  * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
40
44
  * violations of the red-black tree properties.
@@ -42,7 +46,7 @@ class RedBlackTree {
42
46
  * the RBTree.
43
47
  * @returns The function does not explicitly return anything.
44
48
  */
45
- insert(key) {
49
+ add(key) {
46
50
  const node = new RBTreeNode(key, types_1.RBTNColor.RED);
47
51
  node.left = exports.NIL;
48
52
  node.right = exports.NIL;
@@ -69,17 +73,20 @@ class RedBlackTree {
69
73
  }
70
74
  if (node.parent === null) {
71
75
  node.color = types_1.RBTNColor.BLACK;
76
+ this._size++;
72
77
  return;
73
78
  }
74
79
  if (node.parent.parent === null) {
80
+ this._size++;
75
81
  return;
76
82
  }
77
83
  this._fixInsert(node);
84
+ this._size++;
78
85
  }
79
86
  /**
80
87
  * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
81
88
  * tree.
82
- * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
89
+ * @param {number} key - The `node` parameter is of type `RBTreeNode` and represents the current
83
90
  * node being processed in the delete operation.
84
91
  * @returns The `delete` function does not return anything. It has a return type of `void`.
85
92
  */
@@ -99,6 +106,7 @@ class RedBlackTree {
99
106
  }
100
107
  }
101
108
  if (z === exports.NIL) {
109
+ this._size--;
102
110
  return;
103
111
  }
104
112
  y = z;
@@ -131,6 +139,7 @@ class RedBlackTree {
131
139
  if (yOriginalColor === types_1.RBTNColor.BLACK) {
132
140
  this._fixDelete(x);
133
141
  }
142
+ this._size--;
134
143
  };
135
144
  helper(this.root);
136
145
  }
@@ -219,6 +228,10 @@ class RedBlackTree {
219
228
  }
220
229
  return y;
221
230
  }
231
+ clear() {
232
+ this._root = exports.NIL;
233
+ this._size = 0;
234
+ }
222
235
  print(beginRoot = this.root) {
223
236
  const display = (root) => {
224
237
  const [lines, , ,] = _displayAux(root);
@@ -172,21 +172,21 @@ class AbstractGraph {
172
172
  return [];
173
173
  }
174
174
  const dfs = (cur, dest, visiting, path) => {
175
- visiting.set(cur, true);
175
+ visiting.add(cur);
176
176
  if (cur === dest) {
177
177
  paths.push([vertex1, ...path]);
178
178
  }
179
179
  const neighbors = this.getNeighbors(cur);
180
180
  for (const neighbor of neighbors) {
181
- if (!visiting.get(neighbor)) {
181
+ if (!visiting.has(neighbor)) {
182
182
  path.push(neighbor);
183
183
  dfs(neighbor, dest, visiting, path);
184
- (0, utils_1.arrayRemove)(path, (vertex) => vertex === neighbor);
184
+ path.pop();
185
185
  }
186
186
  }
187
- visiting.set(cur, false);
187
+ visiting.delete(cur);
188
188
  };
189
- dfs(vertex1, vertex2, new Map(), []);
189
+ dfs(vertex1, vertex2, new Set(), []);
190
190
  return paths;
191
191
  }
192
192
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.41.6",
3
+ "version": "1.41.8",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.41.6"
135
+ "data-structure-typed": "^1.41.8"
136
136
  }
137
137
  }
@@ -142,7 +142,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
142
142
  }
143
143
 
144
144
  protected defaultOneParamCallback = (node: N) => node.key;
145
-
145
+
146
146
  /**
147
147
  * Creates a new instance of BinaryTreeNode with the given key and value.
148
148
  * @param {BTNKey} key - The key for the new node.
@@ -36,7 +36,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
36
36
  }
37
37
  }
38
38
  }
39
-
39
+
40
40
  /**
41
41
  * The function creates a new binary search tree node with the given key and value.
42
42
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -44,6 +44,12 @@ export class RedBlackTree {
44
44
  return this._root;
45
45
  }
46
46
 
47
+ protected _size: number = 0;
48
+
49
+ get size(): number {
50
+ return this._size;
51
+ }
52
+
47
53
  /**
48
54
  * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
49
55
  * violations of the red-black tree properties.
@@ -51,7 +57,7 @@ export class RedBlackTree {
51
57
  * the RBTree.
52
58
  * @returns The function does not explicitly return anything.
53
59
  */
54
- insert(key: number): void {
60
+ add(key: number): void {
55
61
  const node: RBTreeNode = new RBTreeNode(key, RBTNColor.RED);
56
62
  node.left = NIL;
57
63
  node.right = NIL;
@@ -79,20 +85,23 @@ export class RedBlackTree {
79
85
 
80
86
  if (node.parent === null) {
81
87
  node.color = RBTNColor.BLACK;
88
+ this._size++;
82
89
  return;
83
90
  }
84
91
 
85
92
  if (node.parent.parent === null) {
93
+ this._size++;
86
94
  return;
87
95
  }
88
96
 
89
97
  this._fixInsert(node);
98
+ this._size++;
90
99
  }
91
100
 
92
101
  /**
93
102
  * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
94
103
  * tree.
95
- * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
104
+ * @param {number} key - The `node` parameter is of type `RBTreeNode` and represents the current
96
105
  * node being processed in the delete operation.
97
106
  * @returns The `delete` function does not return anything. It has a return type of `void`.
98
107
  */
@@ -113,6 +122,7 @@ export class RedBlackTree {
113
122
  }
114
123
 
115
124
  if (z === NIL) {
125
+ this._size--;
116
126
  return;
117
127
  }
118
128
 
@@ -144,6 +154,7 @@ export class RedBlackTree {
144
154
  if (yOriginalColor === RBTNColor.BLACK) {
145
155
  this._fixDelete(x);
146
156
  }
157
+ this._size--;
147
158
  };
148
159
  helper(this.root);
149
160
  }
@@ -241,6 +252,11 @@ export class RedBlackTree {
241
252
  return y;
242
253
  }
243
254
 
255
+ clear() {
256
+ this._root = NIL;
257
+ this._size = 0;
258
+ }
259
+
244
260
  print(beginRoot: RBTreeNode = this.root) {
245
261
  const display = (root: RBTreeNode | null): void => {
246
262
  const [lines, , ,] = _displayAux(root);
@@ -233,8 +233,8 @@ export abstract class AbstractGraph<
233
233
  return [];
234
234
  }
235
235
 
236
- const dfs = (cur: VO, dest: VO, visiting: Map<VO, boolean>, path: VO[]) => {
237
- visiting.set(cur, true);
236
+ const dfs = (cur: VO, dest: VO, visiting: Set<VO>, path: VO[]) => {
237
+ visiting.add(cur);
238
238
 
239
239
  if (cur === dest) {
240
240
  paths.push([vertex1, ...path]);
@@ -242,17 +242,17 @@ export abstract class AbstractGraph<
242
242
 
243
243
  const neighbors = this.getNeighbors(cur);
244
244
  for (const neighbor of neighbors) {
245
- if (!visiting.get(neighbor)) {
245
+ if (!visiting.has(neighbor)) {
246
246
  path.push(neighbor);
247
247
  dfs(neighbor, dest, visiting, path);
248
- arrayRemove(path, (vertex: VO) => vertex === neighbor);
248
+ path.pop();
249
249
  }
250
250
  }
251
251
 
252
- visiting.set(cur, false);
252
+ visiting.delete(cur);
253
253
  };
254
254
 
255
- dfs(vertex1, vertex2, new Map<VO, boolean>(), []);
255
+ dfs(vertex1, vertex2, new Set<VO>(), []);
256
256
  return paths;
257
257
  }
258
258