sussy-util
Version:
Util package made by me
115 lines (114 loc) • 4.57 kB
TypeScript
import Optional from './Optional';
declare class TreeNode<T> {
value: T;
left?: TreeNode<T>;
right?: TreeNode<T>;
constructor(value: T);
}
/**
* Represents a binary search tree.
* @template T - The type of elements stored in the tree.
*/
export default class Tree<T> {
private readonly compare;
root?: TreeNode<T>;
/**
* Creates a new Tree instance.
* @param {T} value - The value of the root node.
* @param {(a: T, b: T) => number} compareFunction - The function used to compare elements.
*/
constructor(value: T, compareFunction: (a: T, b: T) => number);
/**
* Creates a new tree node with the given value.
* @param {T} value - The value of the node.
* @returns {TreeNode<T>} - The created tree node.
*/
private createNode;
/**
* Inserts a new value into the tree.
* @param {T} value - The value to insert.
*/
insert: (value: T) => void;
/**
* Recursively inserts a value into the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {T} value - The value to insert.
* @returns {TreeNode<T>} - The updated root of the subtree.
*/
private insertNode;
/**
* Searches for a value in the tree.
* @param {T} value - The value to search for.
* @returns {Optional<TreeNode<T>>} - An Optional containing the found node or undefined.
*/
search: (value: T) => Optional<TreeNode<T>>;
/**
* Recursively searches for a value in the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {T} value - The value to search for.
* @returns {TreeNode<T> | undefined} - The found node or undefined.
*/
private searchNode;
/**
* Performs an in-order traversal of the tree, applying the callback to each node's value.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
inOrderTraversal: (callback: (value: T) => void) => void;
/**
* Recursively performs an in-order traversal of the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
private inOrderTraversalNode;
/**
* Performs a pre-order traversal of the tree, applying the callback to each node's value.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
preOrderTraversal: (callback: (value: T) => void) => void;
/**
* Recursively performs a pre-order traversal of the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
private preOrderTraversalNode;
/**
* Performs a post-order traversal of the tree, applying the callback to each node's value.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
postOrderTraversal: (callback: (value: T) => void) => void;
/**
* Recursively performs a post-order traversal of the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {(value: T) => void} callback - The callback function to apply to each node's value.
*/
private postOrderTraversalNode;
/**
* Finds the minimum value in the tree.
* @returns {Optional<TreeNode<T>>} - An Optional containing the node with the minimum value or undefined.
*/
findMin: () => Optional<TreeNode<T>>;
/**
* Finds the maximum value in the tree.
* @returns {Optional<TreeNode<T>>} - An Optional containing the node with the maximum value or undefined.
*/
findMax: () => Optional<TreeNode<T>>;
/**
* Removes a value from the tree if it exists.
* @param {T} value - The value to remove.
*/
remove: (value: T) => void;
/**
* Recursively removes a node from the tree.
* @param {TreeNode<T> | undefined} root - The root node of the subtree.
* @param {T} value - The value to remove.
* @returns {TreeNode<T> | undefined} - The updated subtree root.
*/
private removeNode;
/**
* Gets the node with the minimum value in the subtree.
* @param {TreeNode<T>} root - The root node of the subtree.
* @returns {TreeNode<T> | undefined} - The node with the minimum value.
*/
private getMinNode;
}
export {};