BinaryTree

data-structures/binary-search-tree. BinaryTree

new BinaryTree()

Binary tree.
Source:

Methods

find(value)

Finds a node by it's value.

Average time complexity: O(log N).
Source:
Parameters:
Name Type Description
value Number | String of the node which should be found.

findMax() → {Node}

Finds the node with maximum value in the whole tree.
Source:
Returns:
Type:
Node
The maximum node of the tree.

findMin() → {Node}

Finds the node with minimum value in the whole tree.
Source:
Returns:
Type:
Node
The minimum node of the tree.

getDiameter() → {Number}

Finds the diameter of the binary tree.
Source:
Returns:
Type:
Number
The longest path in the BST.

getHeight() → {Number}

Returns the height of the tree.
Source:
Returns:
Type:
Number
The height of the tree.

inorder(callback)

In-order traversal of the whole binary search tree.
Source:
Parameters:
Name Type Description
callback function Callback which will be called for each traversed node.

insert(value, current)

Inserts a node into the binary search tree.

Time complexity: O(log N) in the average case and O(N) in the worst case.
Source:
Parameters:
Name Type Description
value Number | String Node value.
current Node Current node.

isBalanced() → {Boolean}

Returns whether the BST is balanced.
Source:
Returns:
Type:
Boolean
Whether the tree is balanced or not.

lowestCommonAncestor(firstNode, secondNode) → {Node}

Finds the lowest common ancestor of two nodes.
Source:
Parameters:
Name Type Description
firstNode Node First node to be considered when checking for ancestor.
secondNode Node Second node to be considered when checking for ancestor.
Returns:
Type:
Node
The lowest common ancestor of the two nodes or null.

postorder(callback)

Post-order traversal of the whole tree.
Source:
Parameters:
Name Type Description
callback function Callback which will be called for each traversed node.

preorder(callback)

Pre-order preorder traversal of the whole tree.
Source:
Parameters:
Name Type Description
callback function Callback which will be called for each traversed node.

remove(node) → {Boolean}

Removes node from the tree.

Average runtime complexity: O(log N).
Source:
Parameters:
Name Type Description
node Node to be removed
Returns:
Type:
Boolean
True/false depending on whether the given node is removed.