docs: add methods for Binary Tree data structures

This commit is contained in:
Revone 2023-12-07 09:21:56 +08:00
parent e6ca052093
commit 7ddad8eb65
5 changed files with 97 additions and 0 deletions

View file

@ -96,6 +96,18 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
*/
/**
* 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.
*
* The function overrides the add method of a binary tree node and balances the tree after inserting
* a new node.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry.
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
* being added to the binary tree.
* @returns The method is returning either the inserted node or undefined.
*/
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
if (keyOrNodeOrEntry === null) return undefined;
const inserted = super.add(keyOrNodeOrEntry, value);

View file

@ -183,6 +183,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
return exemplar instanceof BinaryTreeNode;
}
/**
* The function `exemplarToNode` converts an exemplar object into a node object.
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
* is provided, it will be `undefined`.
* @returns a value of type N (node), or null, or undefined.
*/
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
if (exemplar === undefined) return;
@ -224,6 +232,16 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
*/
/**
* Time Complexity O(log n) - O(n)
* Space Complexity O(1)
*
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
* existing node with the same key.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
* @param {V} [value] - The value to be inserted into the binary tree.
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
*/
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
let inserted: N | null | undefined;

View file

@ -155,6 +155,14 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
}
/**
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
* otherwise it returns undefined.
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the exemplar node.
* @returns a node of type N or undefined.
*/
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
let node: N | undefined;
if (exemplar === null || exemplar === undefined) {
@ -181,6 +189,18 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
* Space Complexity: O(1) - Constant space is used.
*/
/**
* 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.
*
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
* or inserting a new node if the key is unique.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
* being added to the binary tree.
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
* node was not added.
*/
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return;

View file

@ -115,6 +115,14 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
return exemplar instanceof RedBlackTreeNode;
}
/**
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
* is provided, it will be used when creating the new node. If no value is provided, the new node
* @returns a node of type N or undefined.
*/
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
let node: N | undefined;
@ -142,6 +150,18 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
* Space Complexity: O(1)
*
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
* color changes to maintain the red-black tree properties.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry.
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
* being added to the binary search tree.
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
*/
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
if (newNode === undefined) return;

View file

@ -86,6 +86,17 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
}
/**
* The function `exemplarToNode` converts an exemplar object into a node object.
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
* can be one of the following:
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
* it defaults to `undefined`.
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
* times the value should be added to the node. If not provided, it defaults to 1.
* @returns a node of type `N` or `undefined`.
*/
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
let node: N | undefined;
if (exemplar === undefined || exemplar === null) {
@ -112,6 +123,22 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
*/
/**
* 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.
*
* The function overrides the add method of a binary tree node and adds a new node to the tree.
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
* entry. It represents the key, node, or entry that you want to add to the binary tree.
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
* method.
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
* be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
* added once. However, you can specify a different value for `count` if you want to add
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
* was not successful.
*/
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
if (newNode === undefined) return;