undirected-graph-typed
Advanced tools
Comparing version 1.47.9 to 1.48.0
@@ -56,2 +56,8 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
*/ | ||
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N; | ||
/** | ||
* 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. | ||
@@ -58,0 +64,0 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. |
@@ -67,2 +67,10 @@ "use strict"; | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof AVLTreeNode; | ||
} | ||
/** | ||
* 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. | ||
@@ -69,0 +77,0 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. |
@@ -76,2 +76,16 @@ /** | ||
/** | ||
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the class N. | ||
*/ | ||
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node | ||
* object. | ||
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the | ||
* function. It can be any type. | ||
* @returns a value of type `N` (which represents a node), or `null`, or `undefined`. | ||
*/ | ||
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | null | undefined; | ||
/** | ||
* The function checks if a given value is an entry in a binary tree node. | ||
@@ -78,0 +92,0 @@ * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has |
@@ -76,2 +76,15 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar | ||
* is valid, otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a variable `node` which is of type `N` or `undefined`. | ||
*/ | ||
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
@@ -78,0 +91,0 @@ * Space Complexity: O(1) - Constant space is used. |
@@ -104,2 +104,41 @@ "use strict"; | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof BSTNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar | ||
* is valid, otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a variable `node` which is of type `N` or `undefined`. | ||
*/ | ||
exemplarToNode(exemplar) { | ||
let node; | ||
if (exemplar === null || exemplar === undefined) { | ||
return; | ||
} | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
return; | ||
} | ||
else { | ||
node = this.createNode(key, value); | ||
} | ||
} | ||
else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar); | ||
} | ||
else { | ||
return; | ||
} | ||
return node; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
@@ -119,24 +158,5 @@ * Space Complexity: O(1) - Constant space is used. | ||
add(keyOrNodeOrEntry) { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return undefined; | ||
} | ||
let newNode; | ||
if (keyOrNodeOrEntry instanceof BSTNode) { | ||
newNode = keyOrNodeOrEntry; | ||
} | ||
else if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
newNode = this.createNode(keyOrNodeOrEntry); | ||
} | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
return; | ||
} | ||
else { | ||
newNode = this.createNode(key, value); | ||
} | ||
} | ||
else { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry); | ||
if (newNode === undefined) | ||
return; | ||
} | ||
if (this.root === undefined) { | ||
@@ -143,0 +163,0 @@ this._setRoot(newNode); |
@@ -62,2 +62,18 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree | ||
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value | ||
* that is not a valid exemplar. | ||
* @returns a variable `node` which is of type `N | undefined`. | ||
*/ | ||
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
@@ -64,0 +80,0 @@ * Space Complexity: O(1) |
@@ -80,24 +80,28 @@ "use strict"; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* The function adds a node to a Red-Black Tree data structure. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: | ||
* @returns The method `add` returns either an instance of `N` (the node that was added) or | ||
* `undefined`. | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree | ||
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value | ||
* that is not a valid exemplar. | ||
* @returns a variable `node` which is of type `N | undefined`. | ||
*/ | ||
add(keyOrNodeOrEntry) { | ||
exemplarToNode(exemplar) { | ||
let node; | ||
if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, undefined, types_1.RBTNColor.RED); | ||
if (exemplar === null || exemplar === undefined) { | ||
return; | ||
} | ||
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) { | ||
node = keyOrNodeOrEntry; | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} | ||
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return; | ||
} | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
@@ -110,7 +114,26 @@ return; | ||
} | ||
else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar, undefined, types_1.RBTNColor.RED); | ||
} | ||
else { | ||
return; | ||
} | ||
node.left = this.Sentinel; | ||
node.right = this.Sentinel; | ||
return node; | ||
} | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* The function adds a node to a Red-Black Tree data structure. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: | ||
* @returns The method `add` returns either an instance of `N` (the node that was added) or | ||
* `undefined`. | ||
*/ | ||
add(keyOrNodeOrEntry) { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry); | ||
if (newNode === undefined) | ||
return; | ||
newNode.left = this.Sentinel; | ||
newNode.right = this.Sentinel; | ||
let y = undefined; | ||
@@ -121,11 +144,11 @@ let x = this.root; | ||
if (x) { | ||
if (node.key < x.key) { | ||
if (newNode.key < x.key) { | ||
x = x.left; | ||
} | ||
else if (node.key > x.key) { | ||
else if (newNode.key > x.key) { | ||
x = x === null || x === void 0 ? void 0 : x.right; | ||
} | ||
else { | ||
if (node !== x) { | ||
this._replaceNode(x, node); | ||
if (newNode !== x) { | ||
this._replaceNode(x, newNode); | ||
} | ||
@@ -136,22 +159,22 @@ return; | ||
} | ||
node.parent = y; | ||
newNode.parent = y; | ||
if (y === undefined) { | ||
this._setRoot(node); | ||
this._setRoot(newNode); | ||
} | ||
else if (node.key < y.key) { | ||
y.left = node; | ||
else if (newNode.key < y.key) { | ||
y.left = newNode; | ||
} | ||
else { | ||
y.right = node; | ||
y.right = newNode; | ||
} | ||
if (node.parent === undefined) { | ||
node.color = types_1.RBTNColor.BLACK; | ||
if (newNode.parent === undefined) { | ||
newNode.color = types_1.RBTNColor.BLACK; | ||
this._size++; | ||
return; | ||
} | ||
if (node.parent.parent === undefined) { | ||
if (newNode.parent.parent === undefined) { | ||
this._size++; | ||
return; | ||
} | ||
this._fixInsert(node); | ||
this._fixInsert(newNode); | ||
this._size++; | ||
@@ -158,0 +181,0 @@ } |
@@ -45,2 +45,18 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N; | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents | ||
* the value type and `N` represents the node type. | ||
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of | ||
* times the node should be created. If not provided, it defaults to 1. | ||
* @returns a value of type `N` (the generic type parameter) or `undefined`. | ||
*/ | ||
exemplarToNode(exemplar: BTNodeExemplar<V, N>, count?: number): N | undefined; | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
@@ -47,0 +63,0 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. |
@@ -55,2 +55,44 @@ "use strict"; | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
isNode(exemplar) { | ||
return exemplar instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents | ||
* the value type and `N` represents the node type. | ||
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of | ||
* times the node should be created. If not provided, it defaults to 1. | ||
* @returns a value of type `N` (the generic type parameter) or `undefined`. | ||
*/ | ||
exemplarToNode(exemplar, count = 1) { | ||
let node; | ||
if (exemplar === undefined || exemplar === null) { | ||
return; | ||
} | ||
else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} | ||
else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
return; | ||
} | ||
else { | ||
node = this.createNode(key, value, count); | ||
} | ||
} | ||
else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar, undefined, count); | ||
} | ||
else { | ||
return; | ||
} | ||
return node; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
@@ -72,24 +114,5 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
add(keyOrNodeOrEntry, count = 1) { | ||
let newNode; | ||
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count); | ||
if (newNode === undefined) | ||
return; | ||
} | ||
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) { | ||
newNode = keyOrNodeOrEntry; | ||
} | ||
else if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
newNode = this.createNode(keyOrNodeOrEntry, undefined, count); | ||
} | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
return; | ||
} | ||
else { | ||
newNode = this.createNode(key, value, count); | ||
} | ||
} | ||
else { | ||
return; | ||
} | ||
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0; | ||
@@ -96,0 +119,0 @@ const inserted = super.add(newNode); |
{ | ||
"name": "undirected-graph-typed", | ||
"version": "1.47.9", | ||
"version": "1.48.0", | ||
"description": "Undirected Graph. Javascript & Typescript Data Structure.", | ||
@@ -148,4 +148,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.47.9" | ||
"data-structure-typed": "^1.48.0" | ||
} | ||
} |
@@ -86,2 +86,11 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of AVLTreeNode. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class. | ||
*/ | ||
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N { | ||
return exemplar instanceof AVLTreeNode; | ||
} | ||
/** | ||
* 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. | ||
@@ -88,0 +97,0 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. |
@@ -147,2 +147,38 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of BSTNode. | ||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class. | ||
*/ | ||
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N { | ||
return exemplar instanceof BSTNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar | ||
* is valid, otherwise it returns undefined. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a variable `node` which is of type `N` or `undefined`. | ||
*/ | ||
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === null || exemplar === undefined) { | ||
return; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
return; | ||
} else { | ||
node = this.createNode(key, value); | ||
} | ||
} else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar); | ||
} else { | ||
return; | ||
} | ||
return node; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n). | ||
@@ -163,22 +199,5 @@ * Space Complexity: O(1) - Constant space is used. | ||
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined { | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
return undefined; | ||
} | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry); | ||
if (newNode === undefined) return; | ||
let newNode: N | undefined; | ||
if (keyOrNodeOrEntry instanceof BSTNode) { | ||
newNode = keyOrNodeOrEntry; | ||
} else if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
newNode = this.createNode(keyOrNodeOrEntry); | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
return; | ||
} else { | ||
newNode = this.createNode(key, value); | ||
} | ||
} else { | ||
return; | ||
} | ||
if (this.root === undefined) { | ||
@@ -185,0 +204,0 @@ this._setRoot(newNode); |
@@ -110,23 +110,28 @@ /** | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode | ||
* class. | ||
*/ | ||
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N { | ||
return exemplar instanceof RedBlackTreeNode; | ||
} | ||
/** | ||
* The function adds a node to a Red-Black Tree data structure. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: | ||
* @returns The method `add` returns either an instance of `N` (the node that was added) or | ||
* `undefined`. | ||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid, | ||
* otherwise it returns undefined. | ||
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree | ||
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value | ||
* that is not a valid exemplar. | ||
* @returns a variable `node` which is of type `N | undefined`. | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined { | ||
let node: N; | ||
if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
node = this.createNode(keyOrNodeOrEntry, undefined, RBTNColor.RED); | ||
} else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) { | ||
node = keyOrNodeOrEntry; | ||
} else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) { | ||
override exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === null || exemplar === undefined) { | ||
return; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
@@ -137,9 +142,28 @@ return; | ||
} | ||
} else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar, undefined, RBTNColor.RED); | ||
} else { | ||
return; | ||
} | ||
return node; | ||
} | ||
node.left = this.Sentinel; | ||
node.right = this.Sentinel; | ||
/** | ||
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree) | ||
* Space Complexity: O(1) | ||
*/ | ||
/** | ||
* The function adds a node to a Red-Black Tree data structure. | ||
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following: | ||
* @returns The method `add` returns either an instance of `N` (the node that was added) or | ||
* `undefined`. | ||
*/ | ||
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined { | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry); | ||
if (newNode === undefined) return; | ||
newNode.left = this.Sentinel; | ||
newNode.right = this.Sentinel; | ||
let y: N | undefined = undefined; | ||
@@ -151,9 +175,9 @@ let x: N | undefined = this.root; | ||
if (x) { | ||
if (node.key < x.key) { | ||
if (newNode.key < x.key) { | ||
x = x.left; | ||
} else if (node.key > x.key) { | ||
} else if (newNode.key > x.key) { | ||
x = x?.right; | ||
} else { | ||
if (node !== x) { | ||
this._replaceNode(x, node) | ||
if (newNode !== x) { | ||
this._replaceNode(x, newNode) | ||
} | ||
@@ -166,13 +190,13 @@ return; | ||
node.parent = y; | ||
newNode.parent = y; | ||
if (y === undefined) { | ||
this._setRoot(node); | ||
} else if (node.key < y.key) { | ||
y.left = node; | ||
this._setRoot(newNode); | ||
} else if (newNode.key < y.key) { | ||
y.left = newNode; | ||
} else { | ||
y.right = node; | ||
y.right = newNode; | ||
} | ||
if (node.parent === undefined) { | ||
node.color = RBTNColor.BLACK; | ||
if (newNode.parent === undefined) { | ||
newNode.color = RBTNColor.BLACK; | ||
this._size++; | ||
@@ -182,3 +206,3 @@ return; | ||
if (node.parent.parent === undefined) { | ||
if (newNode.parent.parent === undefined) { | ||
this._size++; | ||
@@ -188,3 +212,3 @@ return; | ||
this._fixInsert(node); | ||
this._fixInsert(newNode); | ||
this._size++; | ||
@@ -191,0 +215,0 @@ } |
@@ -84,2 +84,41 @@ /** | ||
/** | ||
* The function checks if an exemplar is an instance of the TreeMultimapNode class. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`. | ||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode | ||
* class. | ||
*/ | ||
override isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N { | ||
return exemplar instanceof TreeMultimapNode; | ||
} | ||
/** | ||
* The function `exemplarToNode` converts an exemplar object into a node object. | ||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`, where `V` represents | ||
* the value type and `N` represents the node type. | ||
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of | ||
* times the node should be created. If not provided, it defaults to 1. | ||
* @returns a value of type `N` (the generic type parameter) or `undefined`. | ||
*/ | ||
override exemplarToNode(exemplar: BTNodeExemplar<V, N>, count = 1): N | undefined { | ||
let node: N | undefined; | ||
if (exemplar === undefined || exemplar === null) { | ||
return; | ||
} else if (this.isNode(exemplar)) { | ||
node = exemplar; | ||
} else if (this.isEntry(exemplar)) { | ||
const [key, value] = exemplar; | ||
if (key === undefined || key === null) { | ||
return; | ||
} else { | ||
node = this.createNode(key, value, count); | ||
} | ||
} else if (this.isNodeKey(exemplar)) { | ||
node = this.createNode(exemplar, undefined, count); | ||
} else { | ||
return; | ||
} | ||
return node; | ||
} | ||
/** | ||
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. | ||
@@ -102,19 +141,5 @@ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size. | ||
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count = 1): N | undefined { | ||
let newNode: N | undefined; | ||
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) { | ||
return; | ||
} else if (keyOrNodeOrEntry instanceof TreeMultimapNode) { | ||
newNode = keyOrNodeOrEntry; | ||
} else if (this.isNodeKey(keyOrNodeOrEntry)) { | ||
newNode = this.createNode(keyOrNodeOrEntry, undefined, count); | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
const [key, value] = keyOrNodeOrEntry; | ||
if (key === undefined || key === null) { | ||
return; | ||
} else { | ||
newNode = this.createNode(key, value, count); | ||
} | ||
} else { | ||
return; | ||
} | ||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count); | ||
if (newNode === undefined) return; | ||
const orgNodeCount = newNode?.count || 0; | ||
@@ -121,0 +146,0 @@ const inserted = super.add(newNode); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1832256
34633
Updateddata-structure-typed@^1.48.0