Options
All
  • Public
  • Public/Protected
  • All
Menu

A Binary Search Tree implementation that accepts any kind of data, including arrays or plain objects. To do so, it must be provided a custom compareFunction that will be used to determine the position of the elements instead of the default comparison operators.

Type parameters

  • T

Hierarchy

  • BinarySearchTree

Implements

Index

Constructors

constructor

  • Constructor takes an optional parameter compareFunction to replace the default comparison function.

    Parameters

    • Optional compareFunction: CompareFunction<T>

      The function used to compare two values.

    Returns BinarySearchTree

Properties

Private Optional _root

compare

compare: CompareFunction<T> = compareMethods.compare.bind(this)

The function used to compare two elements. It is determinant for the tree structure. By default, it compares numbers so it must be replaced with a custom function when storing another data type.

param

Element A

param

Element B

returns

-1 if a < b, 1 if a > b, 0 if a === b

defaultTraverseMethod

defaultTraverseMethod: TraverseMethod = TraverseMethod.DFSPreOrder

The traverse method used when unspecified in the concerned methods. It can be changed via setDefaultTraverseMethod method. Possible values:

  • TraverseMethod.BFS / 0 (Breadth First Search)
  • TraverseMethod.DFSPreOrder / 1 (Depth First Search Pre Order)
  • TraverseMethod.DFSInOrder / 2 (Depth First Search In Order)
  • TraverseMethod.DFSPostOrder / 3 (Depth First Search Post Order)
defaultvalue

TraverseMethod.DFSPreOrder

equal

equal: equal = compareMethods.equal.bind(this)

inf

inf: inf = compareMethods.inf.bind(this)

infOrEqual

infOrEqual: infOrEqual = compareMethods.infOrEqual.bind(this)

sup

sup: sup = compareMethods.sup.bind(this)

supOrEqual

supOrEqual: supOrEqual = compareMethods.supOrEqual.bind(this)

Private traverseMethods

traverseMethods: traverseBFS[] = [this.traverseBFS.bind(this), // Ahh JavaScript...!this.traversePreOrder.bind(this),this.traverseInOrder.bind(this),this.traversePostOrder.bind(this),]

Methods

clear

  • clear(): void
  • Resets the tree, removing its elements.

    Returns void

filter

  • Creates a new tree with the filtered values of the current one, using the input filterFunction. Current tree is unaltered.

    Parameters

    • filterFunction: FilterFunction<T>

      The FilterFunction to use.

    • Default value traverseMethod: TraverseMethod = this.defaultTraverseMethod

      The TraverseMethod to use.

    Returns BinarySearchTree<T>

    The resulting tree.

insert

  • insert(value: T): boolean
  • Inserts a new value to the tree. Note: the structure does not accept duplicates.

    Parameters

    • value: T

      The value to insert

    Returns boolean

    true if succeeded, falseotherwise

map

  • Traverses the tree, applying a transformation to every value according to the given MapFunction. A new tree is returned, the current one is unaltered.

    Type parameters

    • U

    Parameters

    • mapFunction: MapFunction<T, U>

      The function describing the transformation to apply on each value.

    • Default value traverseMethod: TraverseMethod = this.defaultTraverseMethod

      The TraverseMethod to use.

    • Optional newCompareFunction: CompareFunction<U>

      The compare function of the new tree. Default is set to the compare function of the current tree. It is necessary if the map function changes the values data type.

    Returns BinarySearchTree<U>

    The resulting tree.

reduce

  • Computes and return a single value from the values of the tree, using the input ReduceFunction.

    Type parameters

    • U

    Parameters

    • reduceFunction: ReduceFunction<T, U>

      The ReduceFunction to use.

    • initialValue: U

      The initial value of the accumulator

    • Default value traverseMethod: TraverseMethod = this.defaultTraverseMethod

      The TraverseMethod to use.

    Returns U

    The accumulated value at the end of traversal.

root

  • Returns the root node.

    Returns BinarySearchTreeNode<T> | undefined

    The root node

setCompareFunction

  • Replaces the current compare function with the provided CompareFunction. A compare function has the signature: CompareFunction<T>(a: T, b: T) => -1 | 0 | 1.

    To keep its integrity, the tree is fully rebuilt.

    Parameters

    • compareFunction: CompareFunction<T>

      The function to use to compare two values.

    Returns BinarySearchTree<T>

    The tree instance.

setDefaultTraverseMethod

  • Sets a default traverse method that will be used when not specified in methods that require traversal. Possible values:

    • TraverseMethod.BFS / 0 (Breadth First Search)
    • TraverseMethod.DFSPreOrder / 1 (Depth First Search Pre Order)
    • TraverseMethod.DFSInOrder / 2 (Depth First Search In Order)
    • TraverseMethod.DFSPostOrder / 3 (Depth First Search Post Order)

    Parameters

    Returns void

toArray

  • Returns an array of the stored values. The order depends on the TraverseMethod used.

    Parameters

    • Default value traverseMethod: TraverseMethod = this.defaultTraverseMethod

      The TraverseMethod to use.

    Returns T[]

Private traverseBFS

  • Traverses the tree using Breadth First Search method, starting from the given root. The given callback is executed on each value.

    Parameters

    Returns void

Private traverseDFS

  • Traverses the tree recursively using Depth First Search method, and executes a callback on each value. This method gathers all DFS order methods into one, applying the callback at the right moment depending on order parameter.

    Parameters

    Returns void

Private traverseInOrder

  • Traverses the tree using DFS InOrder method and applies a callback on each value.

    Parameters

    Returns void

Private traversePostOrder

  • Traverses the tree using DFS PostOrder method and applies a callback on each value.

    Parameters

    Returns void

Private traversePreOrder

  • Traverses the tree using DFS PreOrder method and applies a callback on each value.

    Parameters

    Returns void

Generated using TypeDoc