Algorithms and data structures for JS/TS
  • Introduction
  • GETTING STARTED
    • TS
    • Node.js
    • ES Modules
    • Browsers
  • PUBLIC API
    • How to read
    • Linked lists
      • SinglyLinkedList
      • DoublyLinkedList
      • CircularlyLinkedList
    • Stacks and queues
      • LinkedStack
      • LinkedQueue
      • LinkedDeque
      • CircularQueue
      • CircularArrayBuffer
      • UnsortedPriorityQueue
      • SortedPriorityQueue
      • AdaptableHeapPriorityQueue
    • Maps
      • SortedMap
      • MaximaSet
      • AVLTreeMap
      • SplayTreeMap
      • RedBlackTreeMap
    • Trees
      • GeneralTree
      • LinkedBinaryTree
      • PreorderTreeTraversal
      • InorderTreeTraversal
      • PostorderTreeTraversal
      • EulerTourTreeTraversal
    • Searches
      • Binary search
      • Quick select
    • Text processing
      • Longest common subsequence
      • Boyer-Moore
      • Knuth-Morris-Pratt
    • Position
    • Locator
    • Comparators
  • CONTRIBUTION NOTES
    • How to contribute
    • Project structure
  • Changelog
Powered by GitBook
On this page
  • length
  • addChild()
  • addRoot()
  • areEqual()
  • attach()
  • clear()
  • getChildren()
  • getNumChildren()
  • getDepth()
  • getHeight()
  • getParent()
  • getRoot()
  • isEmpty()
  • isLeaf()
  • isRoot()
  • remove()
  • replace()
  • traverse()

Was this helpful?

  1. PUBLIC API
  2. Trees

GeneralTree

A tree in which element can have either zero or many children. This structure is link-based.

length

/**
 * Number of elements in the tree.
 *
 * @readonly
 */
get length(): number

addChild()

/**
 * Adds child element to parent element at the specified position.
 *
 * @param position Position of the parent element.
 * @param element Element to add.
 * @returns Position of the added element.
 */
addChild(position: Position<T, Node<T>>, element: T): Position<T, Node<T>>

addRoot()

/**
 * Adds the specified element at the root of the tree. Throws an error if the tree is not empty.
 *
 * @param element Element to add.
 * @returns Position of the added element.
 */
addRoot(element: T): Position<T, Node<T>>

areEqual()

/**
 * Checks whether the specified positions are of the same element.
 *
 * @param a Position in the tree.
 * @param b Position in the tree.
 * @returns TRUE if the positions are the same, FALSE otherwise.
 */
areEqual(a: P, b: P): boolean

attach()

/**
 * Attaches element structures of the listed trees to the specified parent element.
 *
 * @param position Position of the parent element.
 * @param trees List of trees.
 */
attach(position: Position<T, Node<T>>, ...trees: this[])

clear()

/**
 * Clears the tree. If instant set TRUE it takes O(1) time but does not deprecate the existing positions.
 *
 * @param instant TRUE to deprecate all existing positions, FALSE to skip deprecation (client code cares of it).
 */
clear(instant = false)

getChildren()

/**
 * Gets iteration of children of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Iterable iterator of positions.
 */
getChildren(position: P): IterableIterator<P>

getNumChildren()

/**
 * Gets count of children of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Number of children. Zero if position is of a leaf element.
 */
getNumChildren(position: P): number

getDepth()

/**
 * Gets number of levels separating the specified position from the root.
 *
 * @param position Position in the tree. No position to get the depth of the tree.
 * @returns Number of levels.
 */
getDepth(position: P): number

getHeight()

/**
 * Gets the maximum number of levels separating the specified position from the tree's leaves.
 *
 * @param position Position in the tree. No position to get the height of the tree.
 * @returns Number of levels.
 */
getHeight(position?: P): number

getParent()

/**
 * Gets parent of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Position or undefined if the specified position is of the root.
 */
getParent(position: P): P | undefined

getRoot()

/**
 * Gets position of the root element of the tree.
 *
 * @returns Position of the root element.
 */
getRoot(): P | undefined

isEmpty()

/**
 * Checks whether the tree is empty or not.
 *
 * @returns TRUE if the tree is empty, FALSE otherwise.
 */
isEmpty(): boolean

isLeaf()

/**
 * Checks whether the specified position is of a leaf element in the tree.
 *
 * @param position Position in the tree.
 * @returns TRUE if the position's element is leaf, FALSE otherwise.
 */
isLeaf(position: P): boolean

isRoot()

/**
 * Checks whether the specified position is of the root element in the tree.
 *
 * @param position Position in the tree.
 * @returns TRUE if the position's element is root, FALSE otherwise.
 */
isRoot(position: P): boolean

remove()

/**
 * Removes element from the tree by position and returns it. Throws an error if the position is not valid
 * or has more than one child.
 *
 * @param position Position of the element.
 * @returns Removed element.
 */
remove(position: Position<T, Node<T>>): T

replace()

/**
 * Replaces element at the specified position.
 *
 * @param position Position of the element.
 * @param element Element to replace the existing with.
 * @returns Replaced element.
 */
replace(position: Position<T, Node<T>>, element: T): T

traverse()

/**
 * Accepts traversal to traverse through the elements of the tree.
 *
 * @param traversal Tree traversal algorithm.
 */
traverse<R, M extends ITraversalMetadata>(traversal: TreeTraversalAbstract<T, R, this, M>): R
PreviousTreesNextLinkedBinaryTree

Last updated 5 years ago

Was this helpful?