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
  • addLeft()
  • addRIght()
  • addRoot()
  • areEqual()
  • attachLeft()
  • attachRight()
  • clear()
  • getChildren()
  • getLeft()
  • getNumChildren()
  • getDepth()
  • getHeight()
  • getParent()
  • getRight()
  • getRoot()
  • getSibling()
  • hasLeft()
  • hasRight()
  • hasSibling()
  • isEmpty()
  • isLeaf()
  • isLeftChild()
  • isRightChild()
  • isRoot()
  • remove()
  • replace()
  • traverse()

Was this helpful?

  1. PUBLIC API
  2. Trees

LinkedBinaryTree

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

length

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

addLeft()

/**
 * Adds element as the left child of the specified position. Throws an error if left child already exists.
 *
 * @param position Position in the tree.
 * @param element Element to add.
 * @returns Position of the added element.
 */
addLeft(position: P, element: T): P

addRIght()

/**
 * Adds element as the right child of the specified position. Throws an error if right child already exists.
 *
 * @param position Position in the tree.
 * @param element Element to add.
 * @returns Position of the added element.
 */
addRight(position: P, element: T): P

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

attachLeft()

/**
 * Attaches elements structure of the specified tree as the left child of the position.
 * Throws an error if left child already exists.
 *
 * @param position Position in the tree.
 * @param tree Tree to attach.
 */
attachLeft(position: P, tree: this)

attachRight()

/**
 * Attaches elements structure of the specified tree as the right child of the position.
 * Throws an error if right child already exists.
 *
 * @param position Position in the tree.
 * @param tree Tree to attach.
 */
attachRight(position: P, tree: 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>

getLeft()

/**
 * Gets left child of the specified position.
 *
 * @param position Position in the tree.
 * @returns Position or undefined if the specified position has no left child.
 */
getLeft(position: P): P | undefined

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

getRight()

/**
 * Gets right child of the specified position.
 *
 * @param position Position in the tree.
 * @returns Position or undefined if the specified position has no right child.
 */
getRight(position: P): P | undefined

getRoot()

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

getSibling()

/**
 * Gets sibling of the specified position.
 *
 * @param position Position in the tree.
 * @returns Position or undefined if the specified position has no sibling.
 */
getSibling(position: P): P | undefined

hasLeft()

/**
 * Checks existence of a left child for the specified position.
 *
 * @param position Position in the tree.
 * @returns TRUE if position has left child, FALSE otherwise.
 */
hasLeft(position: P): boolean

hasRight()

/**
 * Checks existence of a right child for the specified position.
 *
 * @param position Position in the tree.
 * @returns TRUE if position has right child, FALSE otherwise.
 */
hasRight(position: P): boolean

hasSibling()

/**
 * Checks existence of a sibling for the specified position.
 *
 * @param position Position in the tree.
 * @returns TRUE if position has sibling, FALSE otherwise.
 */
hasSibling(position: P): boolean

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

isLeftChild()

/**
 * Checks whether the first specified position is the left child of the second.
 *
 * @param a Position in the tree.
 * @param b Position in the tree.
 * @returns TRUE if the positions are in parent->left child relation, FALSE otherwise.
 */
isLeftChild(a: P, b: P): boolean

isRightChild()

/**
 * Checks whether the first specified position is the right child of the second.
 *
 * @param a Position in the tree.
 * @param b Position in the tree.
 * @returns TRUE if the positions are in parent->right child relation, FALSE otherwise.
 */
isRightChild(a: P, b: 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
PreviousGeneralTreeNextPreorderTreeTraversal

Last updated 5 years ago

Was this helpful?