diff --git a/README.md b/README.md index 6725b06..d9e6147 100644 --- a/README.md +++ b/README.md @@ -8,41 +8,26 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript - - - - - - - - - -
Binary Search Tree - Binary Search Tree -
AVL Tree
(Self Balancing Tree)
- AVL Tree -
- # Contents * [Install](#install) * [require](#require) * [import](#import) * [API](#api) * [constructor](#constructor) - * [.insert(key, value)](#insertkey-value) - * [.has(key)](#haskey) - * [.find(key)](#findkey) - * [.min()](#min) - * [.max()](#max) - * [.lowerBound(k[, includeEqual]) (floor)](#lowerboundk-includeEqual-floor) - * [.upperBound(k[, includeEqual]) (ceil)](#upperboundk-includeEqual-ceil) - * [.root()](#root) - * [.count()](#count) - * [.traverseInOrder(cb)](#traverseinordercb) - * [.traversePreOrder(cb)](#traversepreordercb) - * [.traversePostOrder(cb)](#traversepostordercb) - * [.remove(key)](#removekey) - * [.clear()](#clear) + * [insert](#insert) + * [has](#has) + * [find](#find) + * [min](#min) + * [max](#max) + * [lowerBound (floor)](#lowerbound-floor) + * [upperBound (ceil)](#upperbound-ceil) + * [root](#root) + * [count](#count) + * [traverseInOrder](#traverseinorder) + * [traversePreOrder](#traversepreorder) + * [traversePostOrder](#traversepostorder) + * [remove](#remove) + * [clear](#clear) * [BinarySearchTreeNode](#binarysearchtreenodet-u) * [AvlTreeNode](#avltreenodet-u) * [Build](#build) @@ -99,27 +84,11 @@ const bst = new BinarySearchTree(); const bst = new AvlTree(); ``` -### .insert(key[, value]) +### insert +O(log(n)) 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. - - - - - - - - - - - -
paramsreturnruntime
- key: T (number | string) -
- value: U -
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
- ```js bst.insert(50, 'v1'); bst.insert(80, 'v2'); @@ -130,46 +99,20 @@ bst.insert(40, 'v6'); bst.insert(20, 'v7'); ``` -### .has(key) -checks if a node exists by its key. +### has +O(log(n)) - - - - - - - - - - - -
paramsreturnruntime
- key: T (number | string) - booleanO(log(n))
+checks if a node exists by its key. ```js bst.has(50); // true bst.has(100); // false ``` -### .find(key) -finds a node in the tree by its key. +### find +O(log(n)) - - - - - - - - - - - -
paramsreturnruntime
- key: T (number | string) - BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
+finds a node in the tree by its key. ```js const n60 = bst.find(60); @@ -179,19 +122,10 @@ console.log(n60.getValue()); // v5 console.log(bst.find(100)); // null ``` -### .min() -finds the node with min key in the tree. +### min +O(log(n)) - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
+finds the node with min key in the tree. ```js const min = bst.min(); @@ -199,19 +133,10 @@ console.log(min.getKey()); // 20 console.log(min.getValue()); // v7 ``` -### .max() -finds the node with max key in the tree. +### max +O(log(n)) - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
+finds the node with max key in the tree. ```js const max = bst.max(); @@ -219,25 +144,10 @@ console.log(max.getKey()); // 90 console.log(max.getValue()); // v4 ``` -### .lowerBound(k[, includeEqual]) (.floor) -finds the node with the biggest key less or equal a given value k. You can eliminate equal keys by passing second param as false. `.floor` is a delegate to the same function. - - - - - - - - - - - - -
paramsreturnruntime
- k: T (number | string) -
- includeEqual: boolean -
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
+### lowerBound (floor) +O(log(n)) + +finds the node with the biggest key less or equal a given key k. You can eliminate equal keys by passing second param as false. `.floor` is an alias to the same function. ```js console.log(bst.lowerBound(60).getKey()); // 60 @@ -245,25 +155,10 @@ console.log(bst.lowerBound(60, false).getKey()); // 50 console.log(bst.lowerBound(10)); // null ``` -### .upperBound(k[, includeEqual]) (.ceil) -finds the node with the smallest key bigger or equal a given value k. You can eliminate equal keys by passing second param as false. `.ceil` is a delegate to the same function. - - - - - - - - - - - - -
paramsreturnruntime
- k: T (number | string) -
- includeEqual: boolean -
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
+### upperBound (ceil) +O(log(n)) + +finds the node with the smallest key bigger or equal a given key k. You can eliminate equal keys by passing second param as false. `.ceil` is an alias to the same function. ```js console.log(bst.upperBound(75).getKey()); // 80 @@ -272,19 +167,10 @@ console.log(bst.upperBound(80, false).getKey()); // 90 console.log(bst.upperBound(110)); // null ``` -### .root() -returns the root node of the tree. +### root +O(1) - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(1)
+returns the root node of the tree. ```js const root = bst.root(); @@ -292,37 +178,19 @@ console.log(root.getKey()); // 50 console.log(root.getValue()); // v1 ``` -### .count() -returns the count of nodes in the tree. +### count +O(1) - - - - - - - - - -
returnruntime
numberO(1)
+returns the count of nodes in the tree. ```js console.log(bst.count()); // 7 ``` -### .traverseInOrder(cb) -traverses the tree in order (left-node-right). +### traverseInOrder +O(n) - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
+traverses the tree in order (left-node-right). ```js bst.traverseInOrder((node) => console.log(node.getKey())); @@ -338,19 +206,10 @@ bst.traverseInOrder((node) => console.log(node.getKey())); */ ``` -### .traversePreOrder(cb) -traverses the tree pre order (node-left-right). +### traversePreOrder +O(n) - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
+traverses the tree pre order (node-left-right). ```js bst.traversePreOrder((node) => console.log(node.getKey())); @@ -366,19 +225,10 @@ bst.traversePreOrder((node) => console.log(node.getKey())); */ ``` -### .traversePostOrder(cb) -traverses the tree post order (left-right-node). +### traversePostOrder +O(n) - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
+traverses the tree post order (left-right-node). ```js bst.traversePostOrder((node) => console.log(node.getKey())); @@ -394,21 +244,10 @@ 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. +### remove +O(log(n)) - - - - - - - - - - - -
paramsreturnruntime
key: TbooleanO(log(n))
+removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced during deletion. ```js bst.remove(20); // true @@ -416,17 +255,10 @@ bst.remove(100); // false console.log(bst.count()); // 6 ``` -### .clear() -clears the tree. +### clear +O(1) - - - - - - - -
runtime
O(1)
+clears the tree. ```js bst.clear(); @@ -436,179 +268,81 @@ console.log(bst.root()); // null ### BinarySearchTreeNode<T, U> -#### .getKey() - - - - -
return
T (number | string)
- -#### .setValue(value) - - - - -
params
value: U
- -#### .getValue() - - - - -
return
U
- -#### .setLeft(left) - - - - -
params
left: BinarySearchTreeNode<T, U>
- -#### .getLeft() - - - - -
return
BinarySearchTreeNode<T, U>
- -#### .hasLeft() +#### setKey +sets the node's key. - - - -
return
boolean
+#### getKey +gets the node's key. -#### .setRight(right) +#### setValue +sets the node's value. - - - -
params
right: BinarySearchTreeNode<T, U>
+#### getValue +gets the node's value. -#### .getRight() +#### setLeft +sets the node's left child. - - - -
return
BinarySearchTreeNode<T, U>
+#### getLeft +gets the node's left child. -#### .hasRight() +#### hasLeft +checks if node has a left child. - - - -
return
boolean
+#### setRight +sets the node's right child. -#### .setParent(parent) +#### getRight +gets the node's right child. - - - -
params
parent: BinarySearchTreeNode<T, U>
+#### hasRight +checks if node has a right child. -#### .getParent() +#### setParent +sets the node's parent node. - - - -
return
BinarySearchTreeNode<T, U>
+#### getParent +gets the node's parent node. -#### .hasParent() +#### hasParent +checks if node has a parent node. - - - -
return
boolean
+#### isLeaf +checks if node is a leaf in the tree. -#### .isLeaf() - - - - -
return
boolean
- -#### .isRoot() - - - - -
return
boolean
+#### isRoot +check if node is the root node. ### AvlTreeNode<T, U> extends BinarySearchTreeNode<T, U> and adds the following methods: -#### .rotateLeft() +#### rotateLeft Rotates self left (counter-clockwise). - - - -
return
AvlTreeNode<T, U>
- -#### .rotateRight() +#### rotateRight Rotates self right (clockwise). - - - -
return
AvlTreeNode<T, U>
- -#### .rotateLeftRight() +#### rotateLeftRight Rotates left child to left then self to right. - - - -
return
AvlTreeNode<T, U>
- -#### .rotateRightLeft() +#### rotateRightLeft Rotates right child to right then self to left. - - - -
return
AvlTreeNode<T, U>
- -#### .getHeight() +#### getHeight Gets the height of the node in the tree. root height is 1. - - - -
return
number
- -#### .getLeftHeight() +#### getLeftHeight Gets the height of left child. 0 if no left child. - - - -
return
number
- -#### .getRightHeight() +#### getRightHeight Gets the height of right child. 0 if no right child. - - - -
return
number
- -#### .getBalance() +#### getBalance returns the node's balance as the diff between left and right heights. - - - -
return
number
- -#### .isBalanced() +#### isBalanced checks if the node is balanced. (height diff is not more/less than 1/-1) - - - -
return
boolean
- ## Build ``` grunt build