Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Tree<K, V>

AVL Tree

Type parameters

Hierarchy

  • Tree

Index

Constructors

constructor

  • new Tree(_comparator?: function, _converter?: function): Tree
  • Creates an instance of AVL. Can set a converter from here.

    Parameters

    • Optional _comparator: function
        • (a: K, b: K): number
        • Parameters

          • a: K
          • b: K

          Returns number

    • Optional _converter: function
        • (value: V): K
        • Parameters

          • value: V

          Returns K

    Returns Tree

Properties

Private Optional _comparator

_comparator: function

Type declaration

    • (a: K, b: K): number
    • Parameters

      • a: K
      • b: K

      Returns number

Private Optional _converter

_converter: function

Type declaration

    • (value: V): K
    • Parameters

      • value: V

      Returns K

differenceFromLeft

differenceFromLeft: number = Infinity

differenceFromRight

differenceFromRight: number = Infinity

nearestFromLeft

nearestFromLeft: Node<K, V>

nearestFromRight

nearestFromRight: Node<K, V>

Private root

root: Node<K, V>

Accessors

comparator

  • get comparator(): function
  • set comparator(comparator: function): void
  • Returns function

      • (a: K, b: K): number
      • Parameters

        • a: K
        • b: K

        Returns number

  • Parameters

    • comparator: function
        • (a: K, b: K): number
        • Parameters

          • a: K
          • b: K

          Returns number

    Returns void

converter

  • get converter(): function
  • set converter(converter: function): void
  • Returns function

      • (value: V): K
      • Parameters

        • value: V

        Returns K

  • Parameters

    • converter: function
        • (value: V): K
        • Parameters

          • value: V

          Returns K

    Returns void

height

  • get height(): number
  • Returns the current height of the tree

    Returns number

length

  • get length(): number
  • Sums up how many nodes there are in the Tree

    Returns number

Methods

__@iterator

  • __@iterator(): IterableIterator<V>
  • Iterate through the values in ascending order

    Returns IterableIterator<V>

any

  • any(...keys: Array<K>): boolean
  • Return true if any of the key is contained

    Parameters

    • Rest ...keys: Array<K>

    Returns boolean

Private convert

  • Tries to convert the value. If its a convertable it will use it's inner converter. If not, it tries to use the supplied converter in the ops. Or optionally you can supply a converter method, but this wont be saved into the Tree If you want a permament converter use the opts or just set the converter field of the Tree TODO: bigint option if supported

    Parameters

    Returns K

enclosing

enclosingNodes

Private finalOperators

  • finalOperators(k: K): object
  • Parameters

    • k: K

    Returns object

    • comp: function
        • (a: K, b: K): number
        • Parameters

          • a: K
          • b: K

          Returns number

    • key: K

first

  • first(): Node<K, V>

firstFrom

  • firstFrom(k: K): V
  • Returns the first value it founds on key or after that

    Parameters

    • k: K

    Returns V

firstNodeFrom

  • firstNodeFrom(k: K): Node<K, V>
  • Returns the first node it founds on key or after that

    Parameters

    • k: K

    Returns Node<K, V>

forEach

  • forEach(callback: function): void
  • Calls a function on each element of the Tree, in order. There is an optional index

    Parameters

    • callback: function
        • (value: V, index?: number): void
        • Parameters

          • value: V
          • Optional index: number

          Returns void

    Returns void

forEachReversed

  • forEachReversed(callback: function): void
  • Calls a function on each element of the Tree, in order. There is an optional index

    Parameters

    • callback: function
        • (value: V, index?: number): void
        • Parameters

          • value: V
          • Optional index: number

          Returns void

    Returns void

get

  • get(key: K): V
  • Returns with the value on the supplied key. undefined if there is no value on that key

    Parameters

    • key: K

    Returns V

getNode

  • getNode(key: K): Node<K, V>
  • Returns with the node on the supplied key. undefined if there is no node on that key

    Parameters

    • key: K

    Returns Node<K, V>

has

  • has(...keys: Array<K>): boolean
  • Return true if every key is contained

    Parameters

    • Rest ...keys: Array<K>

    Returns boolean

invert

  • invert(): void
  • Please don't use this for the love of god

    Returns void

last

  • last(): Node<K, V>

lastBefore

  • lastBefore(k: K): V

lastNodeBefore

  • lastNodeBefore(k: K): Node<K, V>

max

  • max(): V

min

  • min(): V

moveNode

  • moveNode(from: K, to: K): boolean
  • This method will move the node object found at the key to the correct position in the tree. This does not reconstruct the object.

    If you try to move the node to an key thats already exists the method will do nothing

    Parameters

    • from: K
    • to: K

    Returns boolean

nodes

  • nodes(): IterableIterator<Node<K, V>>

print

  • print(): void

push

  • push(...input: Array<V>): boolean
  • The push method tries to convert the value into a number to use it as a Key if it has a convertTo method (suggested, but not necessarily by the Convertable interface) it will use that. If not, but you've set a converter

    Parameters

    • Rest ...input: Array<V>

    Returns boolean

put

  • put(...input: object[]): boolean
  • Sets multiple values to multiple keys

    Parameters

    • Rest ...input: object[]

    Returns boolean

remove

  • remove(key: K): V

reverse

  • reverse(): IterableIterator<V>
  • Iterate through the values in descending order

    Returns IterableIterator<V>

reverseNodes

  • reverseNodes(): IterableIterator<Node<K, V>>

set

  • set(key: K, value: V): boolean
  • sets a key to a value

    Parameters

    • key: K
    • value: V

    Returns boolean

stringify

  • stringify(): string
  • Because it's marked with @ToJSon we can sumply use JSON.stringify. I'm putting this method here for brevity

    Returns string

toArray

  • toArray(): Array<V>

Static parse

  • parse<K, V>(tree: string, keyType?: Constructor<K>, valueType?: Constructor<V>, ...extra: Array<Constructor<any>>): Tree<K, V>
  • ! WARNING: Limited capabilities! ! The converted tree must use Objects (Not Numbers, not Strings, Objects) ! on both the key and value to be working ! (Workaround: use wrapper objects) ! It also can't restore the explicit converter and comparator functions ! as JavaScript can't parse functions (reliably) ! (Workaround: use implicit converters and comparables)

    • The first limitation I believe can be improved with the Reflection-metadata API
    • which is included in this project but not used as I couldn't make TypedJSON work
    • with that. Feel free to contact me if you know a solution on supplying the constructor
    • reference dynamically while the tree is building so it can be used when stringifying

    Bit tricky to use. You have to supply the constructor of the key and the value. If it's a primitive you can just leave it undefined example:

    const tree = Tree.parse<number, Basic>(treeAsString, undefined, Basic);

    If those subtypes are also generic (and annotated by TypedJSON) you can add extra knownTypes after the initial two too.

    Type parameters

    Parameters

    • tree: string
    • Optional keyType: Constructor<K>
    • Optional valueType: Constructor<V>
    • Rest ...extra: Array<Constructor<any>>

    Returns Tree<K, V>

Generated using TypeDoc