refactor: Extract the isNode and exemplarToNode methods to achieve best practices.

This commit is contained in:
Revone 2023-11-27 13:38:26 +08:00
parent 20550fb718
commit e1e672b3b5
8 changed files with 228 additions and 126 deletions

View file

@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
## [v1.47.8](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.47.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
### Changes

View file

@ -12,17 +12,7 @@
Data Structures of Javascript & TypeScript.
Do you envy C++ with [STL]() (std::), Python with [collections](), and Java with [java.util]() ? Well, no need to envy
anymore! JavaScript and TypeScript now have [data-structure-typed]().
Now you can use this in Node.js and browser environments
CommonJS:**`require export.modules =`**
ESModule:   **`import export`**
Typescript:   **`import export`**
UMD:           **`var Deque = dataStructureTyped.Deque`**
anymore! JavaScript and TypeScript now have [data-structure-typed]().**`Benchmark`** compared with C++ STL. **`API standards`** aligned with ES6 and Java. **`Usability`** is comparable to Python
[//]: # (![Branches](https://img.shields.io/badge/branches-55.47%25-red.svg?style=flat))
@ -35,6 +25,17 @@ UMD:           **`var Deq
## Installation and Usage
Now you can use it in Node.js and browser environments
CommonJS:**`require export.modules =`**
ESModule:   **`import export`**
Typescript:   **`import export`**
UMD:           **`var Deque = dataStructureTyped.Deque`**
### npm
```bash
@ -474,127 +475,127 @@ avl2.print();
<td>Binary Tree</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>View</span></a></td>
</tr>
<tr>
<td>Binary Search Tree (BST)</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BST.html"><span>BST</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BST.html"><span>View</span></a></td>
</tr>
<tr>
<td>AVL Tree</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTree.html"><span>AVLTree</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AVLTree.html"><span>View</span></a></td>
</tr>
<tr>
<td>Red Black Tree</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/RedBlackTree.html"><span>RedBlackTree</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/RedBlackTree.html"><span>View</span></a></td>
</tr>
<tr>
<td>Tree Multiset</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultimap.html"><span>TreeMultimap</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/TreeMultimap.html"><span>View</span></a></td>
</tr>
<tr>
<td>Segment Tree</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTree.html"><span>SegmentTree</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SegmentTree.html"><span>View</span></a></td>
</tr>
<tr>
<td>Binary Indexed Tree</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryIndexedTree.html"><span>BinaryIndexedTree</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryIndexedTree.html"><span>View</span></a></td>
</tr>
<tr>
<td>Heap</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Heap.html"><span>Heap</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Heap.html"><span>View</span></a></td>
</tr>
<tr>
<td>Priority Queue</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/PriorityQueue.html"><span>PriorityQueue</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/PriorityQueue.html"><span>View</span></a></td>
</tr>
<tr>
<td>Max Priority Queue</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MaxPriorityQueue.html"><span>MaxPriorityQueue</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MaxPriorityQueue.html"><span>View</span></a></td>
</tr>
<tr>
<td>Min Priority Queue</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MinPriorityQueue.html"><span>MinPriorityQueue</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/MinPriorityQueue.html"><span>View</span></a></td>
</tr>
<tr>
<td>Trie</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Trie.html"><span>Trie</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Trie.html"><span>View</span></a></td>
</tr>
<tr>
<td>Graph</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractGraph.html"><span>AbstractGraph</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/AbstractGraph.html"><span>View</span></a></td>
</tr>
<tr>
<td>Directed Graph</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedGraph.html"><span>DirectedGraph</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DirectedGraph.html"><span>View</span></a></td>
</tr>
<tr>
<td>Undirected Graph</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedGraph.html"><span>UndirectedGraph</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/UndirectedGraph.html"><span>View</span></a></td>
</tr>
<tr>
<td>Queue</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Queue.html"><span>Queue</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Queue.html"><span>View</span></a></td>
</tr>
<tr>
<td>Deque</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Deque.html"><span>Deque</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Deque.html"><span>View</span></a></td>
</tr>
<tr>
<td>Linked List</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>SinglyLinkedList</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>View</span></a></td>
</tr>
<tr>
<td>Singly Linked List</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>SinglyLinkedList</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/SinglyLinkedList.html"><span>View</span></a></td>
</tr>
<tr>
<td>Doubly Linked List</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedList.html"><span>DoublyLinkedList</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/DoublyLinkedList.html"><span>View</span></a></td>
</tr>
<tr>
<td>Stack</td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Stack.html"><span>Stack</span></a></td>
<td><a href="https://data-structure-typed-docs.vercel.app/classes/Stack.html"><span>View</span></a></td>
</tr>
</tbody>
</table>

View file

@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.47.8",
"version": "1.47.9",
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",

View file

@ -82,6 +82,15 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
}) as TREE;
}
/**
* 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.
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.

View file

@ -163,6 +163,47 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return new BinaryTree<V, N, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
}
/**
* 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 {
return exemplar instanceof BinaryTreeNode;
}
/**
* 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 {
if (exemplar === undefined) return;
let node: N | null | undefined;
if (exemplar === null) {
node = null;
} else if (this.isEntry(exemplar)) {
const [key, value] = exemplar;
if (key === undefined) {
return;
} else if (key === null) {
node = null;
} else {
node = this.createNode(key, value);
}
} else if (this.isNode(exemplar)) {
node = exemplar;
} else if (this.isNodeKey(exemplar)) {
node = this.createNode(exemplar);
} else {
return;
}
return node;
}
/**
* The function checks if a given value is an entry in a binary tree node.
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
@ -188,7 +229,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
*/
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | null | undefined {
let inserted: N | null | undefined, needInsert: N | null | undefined;
let inserted: N | null | undefined;
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
if (newNode === undefined) return;
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
const queue = new Queue<N>([root]);
@ -205,30 +248,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
}
};
if (keyOrNodeOrEntry === null) {
needInsert = null;
} else if (this.isNodeKey(keyOrNodeOrEntry)) {
needInsert = this.createNode(keyOrNodeOrEntry);
} else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
needInsert = keyOrNodeOrEntry;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined) {
return;
} else if (key === null) {
needInsert = null;
} else {
needInsert = this.createNode(key, value);
}
} else {
return;
}
if (this.root) {
inserted = _bfs(this.root, needInsert);
inserted = _bfs(this.root, newNode);
} else {
this._setRoot(needInsert);
if (needInsert) {
this._setRoot(newNode);
if (newNode) {
this._size = 1;
} else {
this._size = 0;

View file

@ -143,6 +143,42 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
}) as TREE;
}
/**
* 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).
* Space Complexity: O(1) - Constant space is used.
@ -159,25 +195,8 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
*/
override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
return undefined;
}
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;
}
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
if (newNode === undefined) return;
if (this.root === undefined) {
this._setRoot(newNode);

View file

@ -106,12 +106,51 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
}) as TREE;
}
/**
* 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 `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 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, RBTNColor.RED);
}
} else if (this.isNodeKey(exemplar)) {
node = this.createNode(exemplar, undefined, RBTNColor.RED);
} else {
return;
}
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:
@ -119,26 +158,11 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
* `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) {
return;
} else if (this.isEntry(keyOrNodeOrEntry)) {
const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value, RBTNColor.RED);
}
} else {
return;
}
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
if (newNode === undefined) return;
node.left = this.Sentinel;
node.right = this.Sentinel;
newNode.left = this.Sentinel;
newNode.right = this.Sentinel;
let y: N | undefined = undefined;
let x: N | undefined = this.root;
@ -146,13 +170,13 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
while (x !== this.Sentinel) {
y = x;
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)
}
return;
}
@ -160,27 +184,27 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
}
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++;
return;
}
if (node.parent.parent === undefined) {
if (newNode.parent.parent === undefined) {
this._size++;
return;
}
this._fixInsert(node);
this._fixInsert(newNode);
this._size++;
}

View file

@ -80,6 +80,45 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
}) as TREE;
}
/**
* 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.
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@ -98,23 +137,9 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
* @returns either a node (`N`) or `undefined`.
*/
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;
const inserted = super.add(newNode);
if (inserted) {