Options
All
  • Public
  • Public/Protected
  • All
Menu

Module Internal - Functions

The internal recursive functions used by the AVLTree class, and additional helper functions. These perform outside of the tree class context, and take nodes to operate on. This makes them reusable for many more applications.

While the AVLTree interface only exposes its contained values the functions here return nodes with these values within, so that internally we can easily traverse subtrees and otherwise operate on them.

Index

Tree recursion Functions

deleteKey

  • deleteKey<K, V>(key: K, node: AVLNode<K, V> | null): [AVLNode<K, V> | null, boolean]
  • Deletes the node by given key from the subtree of given node. Returns an array with:

    • [0] - the node that takes the place of given node after deletion and re-balancing, which might also be the given node itself.

    • [1] - A boolean indicating whether a node was actually deleted. This will be false if the key was not found in the tree.

    Type parameters

    Parameters

    • key: K
    • node: AVLNode<K, V> | null

    Returns [AVLNode<K, V> | null, boolean]

foldLeft

  • foldLeft<K, V, T>(node: AVLNode<K, V> | null, fn: (acc: T, node: AVLNode<K, V>) => T, seed: T): T
  • Folds (reduces) the given node's subtree left-to-right using in-order traversal.

    Type parameters

    Parameters

    • node: AVLNode<K, V> | null
    • fn: (acc: T, node: AVLNode<K, V>) => T
        • Parameters

          Returns T

    • seed: T

    Returns T

foldRight

  • foldRight<K, V, T>(node: AVLNode<K, V> | null, fn: (acc: T, node: AVLNode<K, V>) => T, seed: T): T
  • Folds (reduces) the given node's subtree right-to-left using reversed in-order traversal.

    Type parameters

    Parameters

    • node: AVLNode<K, V> | null
    • fn: (acc: T, node: AVLNode<K, V>) => T
        • Parameters

          Returns T

    • seed: T

    Returns T

insert

  • insert<K, V>(key: K, val: V, node: AVLNode<K, V> | null): [AVLNode<K, V>, boolean]
  • Inserts the given key an corresponding value into the sub-tree of the given node. Returns an array of length 2 with at:

    • [0] - the node that takes the place of given node after insertion and re-balancing, which might also be the given node itself.

    • [1] - A boolean indicating whether the new key and value were actually inserted. This will be false if the key was already in the tree prior to insertion.

    Type parameters

    Parameters

    • key: K
    • val: V
    • node: AVLNode<K, V> | null

    Returns [AVLNode<K, V>, boolean]

maxNode

  • Returns the node within the subtree of given node that ranks highest. Returns the given node itself if it has no children, or null if the given node is itself null.

    Type parameters

    Parameters

    Returns AVLNode<K, V> | null

minNode

  • Returns the node within the subtree of given node that ranks lowest. Returns the given node itself if it has no children, or null if the given node is itself null.

    Type parameters

    Parameters

    Returns AVLNode<K, V> | null

search

  • search<K, V>(node: AVLNode<K, V> | null, searchKey: K): AVLNode<K, V> | null
  • Search a node by a given searchKey, matching against keys of nodes. Returns the matching node if found, or null otherwise.

    Type parameters

    Parameters

    • node: AVLNode<K, V> | null
    • searchKey: K

    Returns AVLNode<K, V> | null

traverseInOrder

  • traverseInOrder<K, V>(node: AVLNode<K, V> | null, fn: (node: AVLNode<K, V>) => void): void
  • Performs in-order traversal of the given node's subtree, applying the given fn to each contained value.

    Type parameters

    Parameters

    Returns void

traversePostOrder

  • traversePostOrder<K, V>(node: AVLNode<K, V> | null, fn: (node: AVLNode<K, V>) => void): void
  • Performs post-order traversal of the given node's subtree, applying the given fn to each contained value.

    Type parameters

    Parameters

    Returns void

traversePreOrder

  • traversePreOrder<K, V>(node: AVLNode<K, V> | null, fn: (node: AVLNode<K, V>) => void): void
  • Performs pre-order traversal of the given node's subtree, applying the given fn to each contained value.

    Type parameters

    Parameters

    Returns void

Helper Functions

nodeBalance

  • nodeBalance(node: AVLNode | null): number
  • Returns the given node's balance: the relationship between the heights of its left and right subtrees. Returns a negative number if the node's left child is the higher height child, or a positive number if the right child is the higher height child. Returns zero if either the node is null or both its children have equal height.

    Parameters

    Returns number

nodeHeight

  • nodeHeight(node: AVLNode | null): number
  • Returns the height of given node or zero if the node is null.

    Parameters

    Returns number

rotateLeft

  • Performs a left rotation on the given node. Returns the node that takes the place of the given node after rotation.

    ┌──────────────────────────────────────────────────────────────────┐
    │ argument -> z                                      y <- return   │
    │            /  \                                  /   \           │
    │           T1   y         rotateLeft(z)          z      x         │
    │               /  \       - - - - - - - ->      / \    / \        │
    │              T2   x                           T1  T2 T3  T4      │
    │                  / \                                             │
    │                T3  T4                                            │
    └──────────────────────────────────────────────────────────────────┘
    

    Type parameters

    Parameters

    Returns AVLNode<K, V>

rotateRight

  • Performs a right rotation on the given node. Returns the node that takes the place of the given node after rotation.

    ┌──────────────────────────────────────────────────────────────────┐
    │ argument -> z                                      y <- return   │
    │            / \                                   /   \           │
    │           y   T4      rotateRight(z)            x      z         │
    │          / \          - - - - - - - - ->      /  \    /  \       │
    │         x   T3                               T1  T2  T3  T4      │
    │        / \                                                       │
    │      T1   T2                                                     │
    └──────────────────────────────────────────────────────────────────┘
    

    Type parameters

    Parameters

    Returns AVLNode<K, V>

scalarCompare

  • scalarCompare<K>(ka: K, kb: K): Ordering
  • Takes two values of type K extends Orderable and returns their ordering.

    Type parameters

    Parameters

    • ka: K
    • kb: K

    Returns Ordering

updateNodeHeight

  • updateNodeHeight(node: AVLNode): void
  • Updates the given node's height property based on the current heights of its left and right subtrees.

    Parameters

    Returns void

updateNodeSize

  • updateNodeSize(node: AVLNode): void
  • Updates the given node's size property based on the current sizes of its left and right subtrees.

    Parameters

    Returns void

writeNodeContents

  • writeNodeContents<K, V>(__namedParameters: { from: AVLNode<K, V>; to: AVLNode<K, V> }): void
  • Writes the key and value (by reference) from the given from node to the given to node.

    Type parameters

    Parameters

    Returns void