Home Manual Reference Source

Function

Static Public Summary
public

Node(color: number, key: any)

An internal node.

public

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
public

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root
public

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root
  • n's sibling is black
public

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root
  • n's sibling is black
  • if n is a left child, the right child of n's sibling is red
  • if n is a right child, the left child of n's sibling is red
public

Delete a node n that has no non-leaf child.

public

Delete a node n with one non-leaf left child and one leaf right child.

public

Computes the grandparent (parent of parent) of the input node.

public

* inordertraversal(node: Node): IterableIterator

Traverses the tree rooted at node in order.

public

insert(compare: Function, A: Node, B: Node): Node

Walks the tree rooted at A down the only path that satisfies the following property: if at a node C we make a left (resp.

public

Preconditions:

  • n is red.
public

Preconditions:

  • n is red.
public

Preconditions:

  • n is red.
public

Preconditions:

  • n is red.
public

Preconditions:

  • n is red.
public

* leftOpenRangeTraversal(compare: Function, root: Node, right: any): IterableIterator

Yields all the keys in the tree rooted at root that lie in the interval (-oo, right[, in order.

public

Computes the predecessor of the input node, in the subtree rooted at the input node, when this predecessor is guaranteed to exist.

public

prune(root: Node)

Prune subtree rooted at input node.

public

* rangetraversal(compare: Function, root: Node, left: any, right: any): IterableIterator

Yields all the keys in the tree rooted at root that lie in the interval [left, right[, in order.

public

Replaces node A by node B.

public

* rightOpenRangeTraversal(compare: Function, root: Node, left: any): IterableIterator

Yields all the keys in the tree rooted at root that lie in the interval [left, +oo), in order.

public

Rotate tree left.

public

Rotate tree right.

public

search(compare: Function, root: Node, key: any): Node

Search for the first node whose key equals key.

public

sibling(node: Node): Node

Computes the sibling of the input node.

public

swap_color(A: Node, B: Node)

Swap colors of two arbitrary nodes.

public

Swap pointers and colors of a node and its left child B with one constraint:

  • B's right child is a leaf

NOTE: This constraint is implied because B is A's in-subtree predecessor.

public

Swap pointers and colors of two NON-ADJACENT nodes with three constraints:

  • B is not the root
  • B is its parent right child
  • B's right child is a leaf

NOTE: These three constraints are implied because B is A's in-subtree predecessor without being A's left child.

    p       q             q           p
    |        \             \          |
   -A        +B            +A        -B
   / \       / \           / \       / \
  u   v     x   -     ->  x   -     u   v
public

uncle(node: Node): Node

Computes the uncle of the input node when the grandparent is guaranteed to exist.

Static Private Summary
private

_debug(colors: Object): Function

Builds a debug function from color handlers.

Static Public

public Node(color: number, key: any) source

An internal node. This node can be red or black.

Params:

NameTypeAttributeDescription
color number

The color of the node.

key any

The key of the node.

public delete_case0(n: Node): Node source

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public delete_case1(n: Node): Node source

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public delete_case2(n: Node): Node source

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root
  • n's sibling is black

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public delete_case5(n: Node): Node source

Preconditions:

  • n is black
  • all root-leaf paths going through n have a black height of b - 1
  • all other root-leaf paths have a black height of b
  • n is not the root
  • n's sibling is black
  • if n is a left child, the right child of n's sibling is red
  • if n is a right child, the left child of n's sibling is red

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public delete_no_child(n: Node): Node source

Delete a node n that has no non-leaf child.

Precondition:

  • n has no non-leaf child.
  • n is not the root

Params:

NameTypeAttributeDescription
n Node

The node to delete.

Return:

Node

The root of the modified subtree.

public delete_one_child(n: Node) source

Delete a node n with one non-leaf left child and one leaf right child.

  p
  |
  n (BLACK)
 / \

RED - / \

  • -

Precondition:

  • n has exactly one non-leaf child.
  • n is not the root
  • n's only non-leaf child is n's left child.
  • hence, n's right child is a leaf
  • hence, n's left child is RED
  • hence, n is BLACK

Params:

NameTypeAttributeDescription
n Node

The node to delete.

public grandparent(node: Node): Node source

Computes the grandparent (parent of parent) of the input node.

Params:

NameTypeAttributeDescription
node Node

The input node.

Return:

Node

public * inordertraversal(node: Node): IterableIterator source

Traverses the tree rooted at node in order.

Params:

NameTypeAttributeDescription
node Node

The root of the tree.

Return:

IterableIterator

public insert(compare: Function, A: Node, B: Node): Node source

Walks the tree rooted at A down the only path that satisfies the following property: if at a node C we make a left (resp. right), then B < C (resp. B >= C). Once we hit the end of the path, we can add node B at this position. By the property of the path, the tree rooted at A is still a binary search tree. For our red-black tree, all that is left to do is fix the red-black tree properties in case they have been violated by this insertion. This is fixed by insert_case0.

Params:

NameTypeAttributeDescription
compare Function

The comparison function to use.

A Node

The root of the tree.

B Node

The node to insert.

Return:

Node

B - The node that has been inserted.

public insert_case0(n: Node): Node source

Preconditions:

  • n is red.
  • n's children are BLACK

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public insert_case1(n: Node): Node source

Preconditions:

  • n is red.
  • n's children are BLACK
  • n is not the root of the tree.

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public insert_case2(n: Node): Node source

Preconditions:

  • n is red.
  • n's children are BLACK
  • n is not the root of the tree.
  • n's parent is red.

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public insert_case3(n: Node): Node source

Preconditions:

  • n is red.
  • n's children are BLACK
  • n is not the root of the tree.
  • n's parent is red.
  • n's uncle is black.

Here we fix the input subtree to pass the preconditions of insert_case4.

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public insert_case4(n: Node): Node source

Preconditions:

  • n is red.
  • n's children are BLACK
  • n is not the root of the tree.
  • n's parent is red.
  • n's uncle is black.
  • the path from n to its grandparent makes a left-left or right-right.

Params:

NameTypeAttributeDescription
n Node

The input node.

Return:

Node

The root of the modified subtree.

public * leftOpenRangeTraversal(compare: Function, root: Node, right: any): IterableIterator source

Yields all the keys in the tree rooted at root that lie in the interval (-oo, right[, in order.

Params:

NameTypeAttributeDescription
compare Function

The comparison function.

root Node

The root of the tree.

right any

The non-inclusive upper bound of the interval.

Return:

IterableIterator

public predecessor(node: Node): Node source

Computes the predecessor of the input node, in the subtree rooted at the input node, when this predecessor is guaranteed to exist.

Params:

NameTypeAttributeDescription
node Node

The input node.

Return:

Node

public prune(root: Node) source

Prune subtree rooted at input node.

Params:

NameTypeAttributeDescription
root Node

The root of the subtree to prune.

public * rangetraversal(compare: Function, root: Node, left: any, right: any): IterableIterator source

Yields all the keys in the tree rooted at root that lie in the interval [left, right[, in order.

Params:

NameTypeAttributeDescription
compare Function

The comparison function.

root Node

The root of the tree.

left any

The inclusive lower bound of the interval.

right any

The non-inclusive upper bound of the interval.

Return:

IterableIterator

public replace_node(A: Node, B: Node) source

Replaces node A by node B.

Params:

NameTypeAttributeDescription
A Node

The node to replace.

B Node

The replacement node.

public * rightOpenRangeTraversal(compare: Function, root: Node, left: any): IterableIterator source

Yields all the keys in the tree rooted at root that lie in the interval [left, +oo), in order.

Params:

NameTypeAttributeDescription
compare Function

The comparison function.

root Node

The root of the tree.

left any

The inclusive lower bound of the interval.

Return:

IterableIterator

public rotate_left(A: Node) source

Rotate tree left. (see https://en.wikipedia.org/wiki/Tree_rotation)

    p                p
    |                |
    A                B
   / \              / \
  x   B     ->     A   y
     / \          / \
    b   y        x   b

Params:

NameTypeAttributeDescription
A Node

The root of the tree.

public rotate_right(B: Node) source

Rotate tree right. (see https://en.wikipedia.org/wiki/Tree_rotation)

    p                p
    |                |
    B                A
   / \              / \
  A   y     ->     x   B
 / \                  / \
x   b                b   j

Params:

NameTypeAttributeDescription
B Node

The root of the tree.

Search for the first node whose key equals key.

Params:

NameTypeAttributeDescription
compare Function

The comparison function.

root Node

The root of the tree to scan.

key any

The key to search for.

Return:

Node

public sibling(node: Node): Node source

Computes the sibling of the input node.

Params:

NameTypeAttributeDescription
node Node

The input node.

Return:

Node

public swap_color(A: Node, B: Node) source

Swap colors of two arbitrary nodes.

   -A        +B      ->      +A        -B

Params:

NameTypeAttributeDescription
A Node

The first node.

B Node

The second node.

public swap_left(A: Node): Node source

Swap pointers and colors of a node and its left child B with one constraint:

  • B's right child is a leaf

NOTE: This constraint is implied because B is A's in-subtree predecessor.

    p                   p
    |                   |
   -A                  -B
   / \                 / \
 +B   c       ->     +A   c
 / \                 / \
a   -               a   -

Params:

NameTypeAttributeDescription
A Node

The node.

Return:

Node

The node B.

public swap_non_adjacent(A: Node, B: Node) source

Swap pointers and colors of two NON-ADJACENT nodes with three constraints:

  • B is not the root
  • B is its parent right child
  • B's right child is a leaf

NOTE: These three constraints are implied because B is A's in-subtree predecessor without being A's left child.

    p       q             q           p
    |        \             \          |
   -A        +B            +A        -B
   / \       / \           / \       / \
  u   v     x   -     ->  x   -     u   v

Params:

NameTypeAttributeDescription
A Node

The first node.

B Node

The second node.

public uncle(node: Node): Node source

Computes the uncle of the input node when the grandparent is guaranteed to exist.

Params:

NameTypeAttributeDescription
node Node

The input node.

Return:

Node

Static Private

private _debug(colors: Object): Function source

Builds a debug function from color handlers.

Params:

NameTypeAttributeDescription
colors Object

The colors to use.

Return:

Function

The debug function.