diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 84932c9..7aa7315 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -96,6 +96,18 @@ export class AVLTree = 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, value?: V): N | undefined { if (keyOrNodeOrEntry === null) return undefined; const inserted = super.add(keyOrNodeOrEntry, value); diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index a97a651..54b9a3f 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -183,6 +183,14 @@ export class BinaryTree = 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`. + * @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, value?: V): N | null | undefined { if (exemplar === undefined) return; @@ -224,6 +232,16 @@ export class BinaryTree = 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, value?: V): N | null | undefined { let inserted: N | null | undefined; diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index c1c9fb1..9009509 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -155,6 +155,14 @@ export class BST = BSTNode`, 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, value?: V): N | undefined { let node: N | undefined; if (exemplar === null || exemplar === undefined) { @@ -181,6 +189,18 @@ export class BST = BSTNode, value?: V): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 828ad59..cfb6a01 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -115,6 +115,14 @@ export class RedBlackTree 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`, 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, value?: V): N | undefined { let node: N | undefined; @@ -142,6 +150,18 @@ export class RedBlackTree * 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, value?: V): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value); if (newNode === undefined) return; diff --git a/src/data-structures/binary-tree/tree-multimap.ts b/src/data-structures/binary-tree/tree-multimap.ts index 62d18b1..eb879da 100644 --- a/src/data-structures/binary-tree/tree-multimap.ts +++ b/src/data-structures/binary-tree/tree-multimap.ts @@ -86,6 +86,17 @@ export class TreeMultimap } + /** + * The function `exemplarToNode` converts an exemplar object into a node object. + * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar`, 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, value?: V, count = 1): N | undefined { let node: N | undefined; if (exemplar === undefined || exemplar === null) { @@ -112,6 +123,22 @@ export class TreeMultimap * 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, value?: V, count = 1): N | undefined { const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count); if (newNode === undefined) return;