@datastructures-js/binary-search-tree
Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript.
Binary Search Tree
AVL Tree (Self Balancing Tree)
Table of Contents
installnpm install --save @datastructures-js/binary-search-tree API requireconst {
BinarySearchTree,
BinarySearchTreeNode,
AvlTree,
AvlTreeNode
} = require ( '@datastructures-js/binary-search-tree' ) ; importimport {
BinarySearchTree,
BinarySearchTreeNode,
AvlTree,
AvlTreeNode
} from '@datastructures-js/binary-search-tree' ; newconst bst = new BinarySearchTree ( ) ;
const bst = new AvlTree ( ) ; .insert(key, value)inserts a node with key/value into the tree and returns the inserted node. Inserting an node with existing key, will update the existing node's value with the new one.
bst. insert ( 50 , 'v1' ) ;
bst. insert ( 80 , 'v2' ) ;
bst. insert ( 30 , 'v3' ) ;
bst. insert ( 90 , 'v4' ) ;
bst. insert ( 60 , 'v5' ) ;
bst. insert ( 40 , 'v6' ) ;
bst. insert ( 20 , 'v7' ) ; .has(key)checks if a node exists by its key.
params
return
runtime
key: number | string
boolean
O(log(n))
bst. has ( 50 ) ;
bst. has ( 100 ) ; .find(key)finds a node in the tree by its key.
const n60 = bst. find ( 60 ) ;
console. log ( n60. getKey ( ) ) ;
console. log ( n60. getValue ( ) ) ;
console. log ( bst. find ( 100 ) ) ; .min()finds the node with min key in the tree.
const min = bst. min ( ) ;
console. log ( min. getKey ( ) ) ;
console. log ( min. getValue ( ) ) ; .max()finds the node with max key in the tree.
const max = bst. max ( ) ;
console. log ( max. getKey ( ) ) ;
console. log ( max. getValue ( ) ) ; .lowerBound(k)finds the node with the biggest key less or equal a given value k.
console. log ( bst. lowerBound ( 60 ) . getKey ( ) ) ;
console. log ( bst. lowerBound ( 10 ) ) ; .upperBound(k)finds the node with the smallest key bigger than a given value k.
console. log ( bst. upperBound ( 75 ) . getKey ( ) ) ;
console. log ( bst. upperBound ( 110 ) ) ; .root()returns the root node of the tree.
const root = bst. root ( ) ;
console. log ( root. getKey ( ) ) ;
console. log ( root. getValue ( ) ) ; .count()returns the count of nodes in the tree.
return
runtime
number
O(1)
console. log ( bst. count ( ) ) ; .traverseInOrder(cb)traverses the tree in order (left-node-right).
params
runtime
cb: function
O(n)
bst. traverseInOrder ( ( node ) => console. log ( node. getKey ( ) ) ) ;
.traversePreOrder(cb)traverses the tree pre order (node-left-right).
params
runtime
cb: function
O(n)
bst. traversePreOrder ( ( node ) => console. log ( node. getKey ( ) ) ) ;
.traversePostOrder(cb)traverses the tree post order (left-right-node).
params
runtime
cb: function
O(n)
bst. traversePostOrder ( ( node ) => console. log ( node. getKey ( ) ) ) ;
.remove(key)removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced during deletion.
params
return
runtime
key: number | string
boolean
O(log(n))
bst. remove ( 20 ) ;
bst. remove ( 100 ) ;
console. log ( bst. count ( ) ) ; .clear()clears the tree.
bst. clear ( ) ;
console. log ( bst. count ( ) ) ;
console. log ( bst. root ( ) ) ; BinarySearchTreeNode .getKey()
.setValue(value)
.getValue()
.setLeft(left)
.getLeft()
.hasLeft()
.setRight(right)
.getRight()
.hasRight()
.setParent(parent)
.getParent()
.hasParent()
.isLeaf()
.isRoot()
AvlTreeNodeextends BinarySearchTreeNode and adds the following methods:
.rotateLeft()Rotates self left (counter-clockwise).
.rotateRight()Rotates self right (clockwise).
.rotateLeftRight()Rotates left child to left then self to right.
.rotateRightLeft()Rotates right child to right then self to left.
.getHeight()Gets the height of the node in the tree. root height is 1.
.getLeftHeight()Gets the height of left child. 0 if no left child.
.getRightHeight()Gets the height of right child. 0 if no right child.
.getBalance()returns the node's balance as the diff between left and right heights.
.isBalanced()checks if the node is balanced. (height diff is not more/less than 1/-1)
Buildgrunt build
LicenseThe MIT License. Full License is here