UNPKG

heap-typed

Version:

Heap. Javascript & Typescript Data Structure.

206 lines (205 loc) 11.1 kB
import type { BinaryTreeDeleteResult, BTNRep, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; import { BST, BSTNode } from './bst'; import { IBinaryTree } from '../../interfaces'; export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> { /** * The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a * color. * @param {K} key - The key parameter is of type K and represents the key of the node in the * Red-Black Tree. * @param {V} [value] - The `value` parameter is an optional parameter that represents the value * associated with the key in the Red-Black Tree Node. It is not required and can be omitted when * creating a new instance of the Red-Black Tree Node. * @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black * Tree Node. It is an optional parameter with a default value of `'BLACK'`. */ constructor(key: K, value?: V, color?: RBTNColor); protected _color: RBTNColor; /** * The function returns the color value of a variable. * @returns The color value stored in the private variable `_color`. */ get color(): RBTNColor; /** * The function sets the color property to the specified value. * @param {RBTNColor} value - The value parameter is of type RBTNColor. */ set color(value: RBTNColor); } export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> { /** * This is the constructor function for a Red-Black Tree data structure in TypeScript. * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to * initialize the RBTree with the provided elements. * @param [options] - The `options` parameter is an optional object that can be passed to the * constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would * depend on the implementation */ constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RBTreeOptions<K, V, R>); protected _root: NODE | undefined; /** * The function returns the root node of a tree or undefined if there is no root. * @returns The root node of the tree structure, or undefined if there is no root node. */ get root(): NODE | undefined; /** * 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): NODE; /** * 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?: RBTreeOptions<K, V, R>): TREE; /** * Time Complexity: O(1) * Space Complexity: O(1) * * The function checks if the input is an instance of the RedBlackTreeNode class. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`. * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is * an instance of the `RedBlackTreeNode` class. */ isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE; /** * 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(1) * * The function adds a new node to a binary search tree and returns true if the node was successfully * added. * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`. * @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(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean; /** * Time Complexity: O(log n) * Space Complexity: O(1) * * 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 {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw` * 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<NODE>` * objects. Each object in the array contains information about the deleted node and whether * balancing is needed. */ delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[]; /** * 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 {NODE | undefined} v - v is a parameter of type NODE or undefined. */ protected _setRoot(v: NODE | 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 {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in * the data structure. * @param {NODE} newNode - The `newNode` parameter is of type `NODE`, 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: NODE, newNode: NODE): NODE; /** * Time Complexity: O(log n) * Space Complexity: O(1) * * The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to * maintain the red-black tree properties. * @param {NODE} 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: NODE): 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 {NODE} u - The parameter "u" represents a node in a binary tree. * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`, which means it can * either be a `NODE` object or `undefined`. */ protected _transplant(u: NODE, v: NODE | 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 {NODE | 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: NODE | 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 {NODE | 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: NODE | 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 {NODE | undefined} x - The parameter `x` is of type `NODE | 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: NODE | 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 {NODE | undefined} y - The parameter `y` is of type `NODE | 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: NODE | undefined): void; }