binary-type-tree
Version:
Binary Trees written in TypeScript
154 lines (153 loc) • 5.07 kB
TypeScript
import { ASNDBS, SNDBSA, INodeConstructor, Node } from "../basic-node";
export interface IAVLNode {
height: number;
right: AVLNode | null;
left: AVLNode | null;
parent: AVLNode | null;
returnThisAVL(): this;
createSimilar(options: INodeConstructor<AVLNode>): AVLNode;
createLeftChild(options: INodeConstructor<AVLNode>): AVLNode;
createRightChild(options: INodeConstructor<AVLNode>): AVLNode;
checkisAVLT(): void;
getAVLNodeFromKey(key: ASNDBS): AVLNode | null;
_insert(key: ASNDBS, value: SNDBSA): void;
_delete(key: ASNDBS, value: SNDBSA): AVLNode;
_updateKey(key: ASNDBS, newKey: ASNDBS): AVLNode;
}
/**
* Used to create an AVL tree with a binary search tree as the root of each Node
*/
export declare class AVLNode extends Node<AVLNode> implements IAVLNode {
options: INodeConstructor<AVLNode>;
/**
* Used to hold the depth of the tree at this point.
* @type {number}
*/
height: number;
/**
* Holds a child of this type Node as a Node in this type tree.
*/
right: AVLNode | null;
/**
* Holds a child of this type Node as a Node in this type tree.
*/
left: AVLNode | null;
/**
* Holds the parent of this type Node as a Node in this type tree.
*/
parent: AVLNode | null;
/**
* Uses abstract class as base to build off of for this class.
* @param options
*/
constructor(options: INodeConstructor<AVLNode>);
/**
* To return this class if this class were to be extended.
* @returns {AVLNode}
*/
returnThisAVL(): this;
/**
* Create a new AVL Node similar to this one except without the key and value
* @param options
* @returns {AVLNode}
*/
createSimilar(options: INodeConstructor<AVLNode>): AVLNode;
/**
* Create a left child AVL Node with a reference to this parent AVL Node
* @param options
* @returns {AVLNode}
*/
createLeftChild(options: INodeConstructor<AVLNode>): AVLNode;
/**
* Create a right child AVL Node with a reference to this parent AVL Node
* @param options
* @returns {AVLNode}
*/
createRightChild(options: INodeConstructor<AVLNode>): AVLNode;
/**
* Calls upon super to check that all basic Node validation is met, along
* with also checking the balance and height correctness of the AVL Node.
*/
checkisAVLT(): void;
/**
* Retrieve a specific Node.
* @param key
* @returns {any}
*/
getAVLNodeFromKey(key: ASNDBS): AVLNode | null;
/**
* Insert a key, value pair in the tree while maintaining the AVL tree height constraint
* Return a pointer to the root node, which may have changed
* @param key
* @param value
* @returns {AVLNode}
* @private
*/
_insert(key: ASNDBS, value: SNDBSA): AVLNode;
/**
* Delete a key or just a value and return the new root of the tree
* @param key
* @param value - Need to send the value as an array for comparison.
* @returns {any}
* @private
*/
_delete(key: ASNDBS, value: SNDBSA): AVLNode;
/**
* Update the key of an index
* Use when you change a fieldNames value, you will need to
* update all they keys that are indexed.
* @param key
* @param newKey
* @returns {AVLNode}
* @private
*/
_updateKey(key: ASNDBS, newKey: ASNDBS): AVLNode;
/**
* Re-balance the tree along the given path. The path is given reversed
* Returns the new root of the tree
* Of course, the first element of the path must be the root of the tree
* @param path
* @returns {AVLNode}
*/
protected rebalanceAlongPath(path: AVLNode[]): AVLNode;
/**
* Check the recorded height for this AVL Node and all children.
* Throws an error if one height does not match.
*/
protected checkHeightCorrect(): void;
/**
* Returns the balance factor.
* @returns {number}
*/
protected balanceFactor(): number;
/**
* Check that the balance factors are between -1 and 1. Otherwise throw an error.
*/
protected checkBalanceFactors(): void;
/**
* Perform a right rotation of the tree if possible
* and return the root of the resulting tree
* The resulting tree's nodes' heights are also updated
* @returns {AVLNode}
*/
protected rightRotation(): AVLNode;
/**
* Perform a left rotation of the tree if possible
* and return the root of the resulting tree
* The resulting tree's nodes' heights are also updated
* @returns {AVLNode}
*/
protected leftRotation(): AVLNode;
/**
* Modify the tree if its right subtree is too small compared to the left.
* Return the new root if any.
* @returns {AVLNode}
*/
protected rightTooSmall(): AVLNode;
/**
* Modify the tree if its left subtree is too small compared to the right.
* Return the new root if any.
* @returns {AVLNode}
*/
protected leftTooSmall(): AVLNode;
}