BinarySearchTree

Starts a Binary Search Tree algorithm

Online example https://8jn8z1.csb.app/

Constructor

new BinarySearchTree(valueopt)

Parameters:
NameTypeAttributesDescription
valueArray.<number> | number<optional>

The value to start the tree

Properties
NameTypeDescription
rootBSTNode | null

The root node of the tree

countnumber

The number of nodes in the tree

Example
const bst = new BinarySearchTree();
bst.add(1);
bst.add(0);
bst.delete(0);

Methods

add(value)

Add a new value to the Tree

Parameters:
NameTypeDescription
valuenumber

a number to add

Example
const bst = new BinarySearchTree();
bst.add(1);
bst.add(100);

delete(value, returnNodeopt) → {BSTNode|BinarySearchTree}

Delete the node with the given value

Parameters:
NameTypeAttributesDefaultDescription
valuenumber

the value to delete

returnNodeboolean<optional>
false

if the node should be returned

Returns:

if the value is in the Tree

Type: 
BSTNode | BinarySearchTree

larger(value) → {number|undefined|null}

Finds the largest value in the tree.

  • If a value is passed it will look for the largest value from that branch;
  • If no value is passed, it will start from the root;
Parameters:
NameTypeDescription
valuenumber

the value to start searching

Returns:

largest value

Type: 
number | undefined | null
  • Returns false if the number does not exist or if there is an error
  • If the number exists, return its node/true
Parameters:
NameTypeAttributesDefaultDescription
valuenumber

the value to find

returnNodeboolean<optional>
true

if the node should be returned

Returns:

if the value is in the Tree

Type: 
BSTNode | null

smaller(valueopt) → {number|undefined|null}

Finds the smallest value in the tree.

  • If a value is passed it will look for the smallest value from that branch;
  • If no value is passed, it will start from the root;
Parameters:
NameTypeAttributesDescription
valuenumber | undefined<optional>

the value to start searching

Returns:

smaller value

Type: 
number | undefined | null

toString() → {string}

Returns a string representation of the tree

Returns:

the string representation

Type: 
string