Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
They allow fast lookup, addition and removal of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key.
Install with npm
npm i javascript-algo-ds
Import in your javascript file as:
const BinarySearchTree = require('javascript-algo-ds/src/data-structure/binarySearchTree/binarySearchTree')const bst = new BinarySearchTree([value])- value - number
- Default value - null
- value of root
- Optional
- root - the root node
- height - the height of the tree
- bst.insert(value)
- bst.contains(value)
- bst.remove(value)
- bst.traverseInOrder([callback])
- bst.traversePreOrder([callback])
- bst.traversePostOrder([callback])
- bst.toString()
- value - number
- value to be added
Type - BinarySearchTreeNode
Returns reference to inserted node
Checks if value is contained in the bst or not.
- value - number
- The number to be checked.
Type - boolean
- value - number
- value to be removed
Type - boolean
True if removed successfully, false otherwise.
- callback - TraversalCallback
- Default value - undefined
- Arguments -
- node - BinarySearchTreeNode --- the current node
- Optional
Type - number[]
Array of traversed nodes in in-order.
- callback - TraversalCallback
- Default value - undefined
- Arguments -
- node - BinarySearchTreeNode --- the current node
- Optional
Type - number[]
Array of traversed nodes in pre-order.
- callback - TraversalCallback
- Default value - undefined
- Arguments -
- node - BinarySearchTreeNode --- the current node
- Optional
Type - number[]
Array of traversed nodes in post-order.
Converts the tree in sorted array in ascending order and returns it
Type - String
@static
Creates BST from an array
- array - number[]
- Array containing elements to be added.
Type - BinarySearchTree