doubly-linked-list-typed
Version:
Doubly Linked List
281 lines (280 loc) • 16 kB
TypeScript
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types';
import { BST, BSTNode } from './bst';
import { IBinaryTree } from '../../interfaces';
export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
parent?: RedBlackTreeNode<K, V>;
/**
* The constructor initializes a node with a key, value, and color for a Red-Black Tree.
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
* Red-Black Tree data structure.
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type
* `V`. It represents the value associated with the key in the data structure being constructed.
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
* explicitly.
*/
constructor(key: K, value?: V, color?: RBTNColor);
_left?: RedBlackTreeNode<K, V> | null | undefined;
get left(): RedBlackTreeNode<K, V> | null | undefined;
set left(v: RedBlackTreeNode<K, V> | null | undefined);
_right?: RedBlackTreeNode<K, V> | null | undefined;
get right(): RedBlackTreeNode<K, V> | null | undefined;
set right(v: RedBlackTreeNode<K, V> | null | undefined);
}
/**
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
* @example
* // using Red-Black Tree as a price-based index for stock data
* // Define the structure of individual stock records
* interface StockRecord {
* price: number; // Stock price (key for indexing)
* symbol: string; // Stock ticker symbol
* volume: number; // Trade volume
* }
*
* // Simulate stock market data as it might come from an external feed
* const marketStockData: StockRecord[] = [
* { price: 142.5, symbol: 'AAPL', volume: 1000000 },
* { price: 335.2, symbol: 'MSFT', volume: 800000 },
* { price: 3285.04, symbol: 'AMZN', volume: 500000 },
* { price: 267.98, symbol: 'META', volume: 750000 },
* { price: 234.57, symbol: 'GOOGL', volume: 900000 }
* ];
*
* // Extend the stock record type to include metadata for database usage
* type StockTableRecord = StockRecord & { lastUpdated: Date };
*
* // Create a Red-Black Tree to index stock records by price
* // Simulates a database index with stock price as the key for quick lookups
* const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
* toEntryFn: stockRecord => [
* stockRecord.price, // Use stock price as the key
* {
* ...stockRecord,
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
* }
* ]
* });
*
* // Query the stock with the highest price
* const highestPricedStock = priceIndex.getRightMost();
* console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
*
* // Query stocks within a specific price range (200 to 400)
* const stocksInRange = priceIndex.rangeSearch(
* [200, 400], // Price range
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
* );
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
*/
export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
/**
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
* raw data.
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It
* is used to initialize the Red-Black Tree with keys, nodes, entries, or
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
* V, R>`. It is an optional parameter that allows you to specify additional options for the
* RedBlackTree class. These options could include configuration settings, behavior customization, or
* any other parameters that are specific to
*/
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
protected _root: RedBlackTreeNode<K, V> | undefined;
get root(): RedBlackTreeNode<K, V> | undefined;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function creates a new Red-Black Tree node with the specified key, value, and color.
* @param {K} key - The key parameter represents the key value of the node being created. It is of
* type K, which is a generic type that can be replaced with any specific type when using the
* function.
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
* associated with the key in the node. It is not required and can be omitted if you only need to
* create a node with a key.
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
* set to "BLACK" if not specified.
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
* returned.
*/
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function creates a new Red-Black Tree with the specified options.
* @param [options] - The `options` parameter is an optional object that contains additional
* configuration options for creating the Red-Black Tree. It has the following properties:
* @returns a new instance of a RedBlackTree object.
*/
createTree(options?: RedBlackTreeOptions<K, V, R>): RedBlackTree<K, V, R, MK, MV, MR>;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function checks if the input is an instance of the RedBlackTreeNode class.
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
* an instance of the `RedBlackTreeNode` class.
*/
isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The "clear" function sets the root node of a data structure to a sentinel value and resets the
* size counter to zero.
*/
clear(): void;
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*
* The function adds a new node to a binary search tree and returns true if the node was successfully
* added.
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
* the key in the data structure. It represents the value that you want to add or update in the data
* structure.
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
* the method returns true. If the node already exists and its value is updated, the method also
* returns true. If the node cannot be added or updated, the method returns false.
*/
add(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*
* The function overrides the delete method in a binary tree data structure to remove a node based on
* a given predicate and maintain the binary search tree properties.
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
* parameter in the `override delete` method is used to specify the condition or key based on which a
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
* function that determines which node(s) should be deleted.
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
* objects. Each object in the array contains information about the deleted node and whether
* balancing is needed.
*/
delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
* applying a callback to each entry in the original tree.
* @param callback - A function that will be called for each entry in the tree, with parameters
* representing the key, value, index, and the tree itself. It should return an entry for the new
* tree.
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
* Tree that will be created during the mapping process. These options could include things like
* custom comparators
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
* the value of `this` when executing the `callback` function. It allows you to set the context
* (value of `this`) for the callback function. This can be useful when you want to access properties
* or
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
* provided callback function.
*/
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR>;
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
* structure.
* @returns The `cloned` object is being returned.
*/
clone(): RedBlackTree<K, V, R, MK, MV, MR>;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function sets the root of a tree-like structure and updates the parent property of the new
* root.
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
*/
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function replaces an old node with a new node while preserving the color of the old node.
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
* the data structure.
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
* data structure.
* @returns The method is returning the result of calling the `_replaceNode` method from the
* superclass, with the `oldNode` and `newNode` parameters.
*/
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
/**
* Time Complexity: O(log n)
* Space Complexity: O(log n)
*
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
* maintain the red-black tree properties.
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
* binary search tree.
* @returns a string value indicating the result of the insertion operation. It can return either
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
* was created and inserted into the tree.
*/
protected _insert(node: RedBlackTreeNode<K, V>): CRUD;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree.
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
* either be a `RedBlackTreeNode<K, V>` object or `undefined`.
*/
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void;
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
* structure. It can either be a valid node or `undefined`.
*/
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void;
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
* the colors and performing rotations.
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
* be either a valid node object or `undefined`.
* @returns The function does not return any value. It has a return type of `void`, which means it
* does not return anything.
*/
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
* node in a binary tree or `undefined` if there is no node.
* @returns void, which means it does not return any value.
*/
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void;
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
* node in a binary tree or `undefined` if there is no node.
* @returns void, which means it does not return any value.
*/
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void;
}