From 9d421e6ad1945c1140b5588047debe3fb52a74fa Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Mon, 30 May 2022 17:44:32 -0700 Subject: [PATCH 1/8] update --- README.md | 401 ++++++++---------------------------------------------- 1 file changed, 53 insertions(+), 348 deletions(-) diff --git a/README.md b/README.md index 6725b06..577a640 100644 --- a/README.md +++ b/README.md @@ -29,22 +29,22 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript * [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) - * [BinarySearchTreeNode](#binarysearchtreenodet-u) - * [AvlTreeNode](#avltreenodet-u) + * [insert](#insertkey) + * [has](#has) + * [find](#find) + * [min](#min) + * [max](#max) + * [lowerBound (floor)](#lowerboundk-floor) + * [upperBound (ceil)](#upperboundk-ceil) + * [root](#root) + * [count](#count) + * [traverseInOrder](#traverseinorder) + * [traversePreOrder](#traversepreorder) + * [traversePostOrder](#traversepostorder) + * [remove](#remove) + * [clear](#clear) + * [BinarySearchTreeNode](#binarysearchtreenode) + * [AvlTreeNode](#avltreenode) * [Build](#build) * [License](#license) @@ -99,27 +99,10 @@ const bst = new BinarySearchTree(); const bst = new AvlTree(); ``` -### .insert(key[, value]) +### insert 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,47 +113,17 @@ bst.insert(40, 'v6'); bst.insert(20, 'v7'); ``` -### .has(key) +### has checks if a node exists by its key. - - - - - - - - - - - -
paramsreturnruntime
- key: T (number | string) - booleanO(log(n))
- ```js bst.has(50); // true bst.has(100); // false ``` -### .find(key) +### find finds a node in the tree by its key. - - - - - - - - - - - -
paramsreturnruntime
- key: T (number | string) - BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
- ```js const n60 = bst.find(60); console.log(n60.getKey()); // 60 @@ -179,92 +132,36 @@ console.log(n60.getValue()); // v5 console.log(bst.find(100)); // null ``` -### .min() +### min finds the node with min key in the tree. - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
- ```js const min = bst.min(); console.log(min.getKey()); // 20 console.log(min.getValue()); // v7 ``` -### .max() +### max finds the node with max key in the tree. - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(log(n))
- ```js const max = bst.max(); console.log(max.getKey()); // 90 console.log(max.getValue()); // v4 ``` -### .lowerBound(k[, includeEqual]) (.floor) +### lowerBound (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))
- ```js console.log(bst.lowerBound(60).getKey()); // 60 console.log(bst.lowerBound(60, false).getKey()); // 50 console.log(bst.lowerBound(10)); // null ``` -### .upperBound(k[, includeEqual]) (.ceil) +### upperBound (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))
- ```js console.log(bst.upperBound(75).getKey()); // 80 console.log(bst.upperBound(80).getKey()); // 80 @@ -272,58 +169,25 @@ console.log(bst.upperBound(80, false).getKey()); // 90 console.log(bst.upperBound(110)); // null ``` -### .root() +### root returns the root node of the tree. - - - - - - - - - -
returnruntime
BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>O(1)
- ```js const root = bst.root(); console.log(root.getKey()); // 50 console.log(root.getValue()); // v1 ``` -### .count() +### count returns the count of nodes in the tree. - - - - - - - - - -
returnruntime
numberO(1)
- ```js console.log(bst.count()); // 7 ``` -### .traverseInOrder(cb) +### traverseInOrder traverses the tree in order (left-node-right). - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
- ```js bst.traverseInOrder((node) => console.log(node.getKey())); @@ -338,20 +202,9 @@ bst.traverseInOrder((node) => console.log(node.getKey())); */ ``` -### .traversePreOrder(cb) +### traversePreOrder traverses the tree pre order (node-left-right). - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
- ```js bst.traversePreOrder((node) => console.log(node.getKey())); @@ -366,20 +219,9 @@ bst.traversePreOrder((node) => console.log(node.getKey())); */ ``` -### .traversePostOrder(cb) +### traversePostOrder traverses the tree post order (left-right-node). - - - - - - - - - -
paramsruntime
cb: (node: BinarySearchTreeNode<T, U> | AvlTreeNode<T, U>) => voidO(n)
- ```js bst.traversePostOrder((node) => console.log(node.getKey())); @@ -394,40 +236,18 @@ bst.traversePostOrder((node) => console.log(node.getKey())); */ ``` -### .remove(key) +### remove removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced during deletion. - - - - - - - - - - - -
paramsreturnruntime
key: TbooleanO(log(n))
- ```js bst.remove(20); // true bst.remove(100); // false console.log(bst.count()); // 6 ``` -### .clear() +### clear clears the tree. - - - - - - - -
runtime
O(1)
- ```js bst.clear(); console.log(bst.count()); // 0 @@ -436,179 +256,64 @@ 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() - - - - -
return
boolean
- -#### .setRight(right) - - - - -
params
right: BinarySearchTreeNode<T, U>
- -#### .getRight() +#### getKey - - - -
return
BinarySearchTreeNode<T, U>
+#### setValue -#### .hasRight() +#### getValue - - - -
return
boolean
+#### setLeft -#### .setParent(parent) +#### getLeft - - - -
params
parent: BinarySearchTreeNode<T, U>
+#### hasLeft -#### .getParent() +#### setRight - - - -
return
BinarySearchTreeNode<T, U>
+#### getRight -#### .hasParent() +#### hasRight - - - -
return
boolean
+#### setParent -#### .isLeaf() +#### getParent - - - -
return
boolean
+#### hasParent -#### .isRoot() +#### isLeaf - - - -
return
boolean
+#### isRoot ### 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 From 544fdde245d9ae380df2b7c32cf55275a5c4c6d4 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Mon, 30 May 2022 17:48:18 -0700 Subject: [PATCH 2/8] update --- README.md | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 577a640..0e45c7d 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,6 @@ 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) @@ -34,8 +19,8 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript * [find](#find) * [min](#min) * [max](#max) - * [lowerBound (floor)](#lowerboundk-floor) - * [upperBound (ceil)](#upperboundk-ceil) + * [lowerBound (floor)](#lowerbound-floor) + * [upperBound (ceil)](#upperbound-ceil) * [root](#root) * [count](#count) * [traverseInOrder](#traverseinorder) From 51d0eee563d7be7b5246c651823c188778d1d585 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:27:54 -0700 Subject: [PATCH 3/8] update --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e45c7d..f424051 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript * [import](#import) * [API](#api) * [constructor](#constructor) - * [insert](#insertkey) + * [insert](#insert) * [has](#has) * [find](#find) * [min](#min) @@ -28,8 +28,8 @@ Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript * [traversePostOrder](#traversepostorder) * [remove](#remove) * [clear](#clear) - * [BinarySearchTreeNode](#binarysearchtreenode) - * [AvlTreeNode](#avltreenode) + * [BinarySearchTreeNode](#binarysearchtreenodet-u) + * [AvlTreeNode](#avltreenodet-u) * [Build](#build) * [License](#license) From 216e9d170999878340b5fd75040132872de2e3e7 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:31:20 -0700 Subject: [PATCH 4/8] update --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index f424051..f3f1530 100644 --- a/README.md +++ b/README.md @@ -241,33 +241,50 @@ console.log(bst.root()); // null ### BinarySearchTreeNode<T, U> +#### setKey +sets the node's key. + #### getKey +gets the node's key. #### setValue +sets the node's value. #### getValue +gets the node's value. #### setLeft +sets the node's left child. #### getLeft +gets the node's left child. #### hasLeft +checks if node has a left child. #### setRight +sets the node's right child. #### getRight +gets the node's right child. #### hasRight +checks if node has a right child. #### setParent +sets the node's parent node. #### getParent +gets the node's parent node. #### hasParent +checks if node has a parent node. #### isLeaf +checks if node is a leaf in the tree. #### isRoot +check if node is the root node. ### AvlTreeNode<T, U> extends BinarySearchTreeNode<T, U> and adds the following methods: From 4bd3f10ab4ef7b1073b6049c4dab4d9635268e16 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:35:00 -0700 Subject: [PATCH 5/8] update --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index f3f1530..011b061 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ const bst = new AvlTree(); ``` ### insert +runtime complexity: 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. @@ -99,6 +100,8 @@ bst.insert(20, 'v7'); ``` ### has +runtime complexity: O(log(n)). + checks if a node exists by its key. ```js @@ -107,6 +110,8 @@ bst.has(100); // false ``` ### find +runtime complexity: O(log(n)). + finds a node in the tree by its key. ```js From 12c20a69002bfb7c59055df707ecfce664ebc0e7 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:37:40 -0700 Subject: [PATCH 6/8] update --- README.md | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 011b061..e5c6109 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ const bst = new AvlTree(); ``` ### insert -runtime complexity: O(log(n)). +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. @@ -100,7 +100,7 @@ bst.insert(20, 'v7'); ``` ### has -runtime complexity: O(log(n)). +O(log(n)) checks if a node exists by its key. @@ -110,7 +110,7 @@ bst.has(100); // false ``` ### find -runtime complexity: O(log(n)). +O(log(n)) finds a node in the tree by its key. @@ -123,6 +123,8 @@ console.log(bst.find(100)); // null ``` ### min +O(log(n)) + finds the node with min key in the tree. ```js @@ -132,6 +134,8 @@ console.log(min.getValue()); // v7 ``` ### max +O(log(n)) + finds the node with max key in the tree. ```js @@ -141,6 +145,8 @@ console.log(max.getValue()); // v4 ``` ### lowerBound (floor) +O(log(n)) + 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. ```js @@ -150,6 +156,8 @@ console.log(bst.lowerBound(10)); // null ``` ### upperBound (ceil) +O(log(n)) + 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. ```js @@ -160,6 +168,8 @@ console.log(bst.upperBound(110)); // null ``` ### root +O(1) + returns the root node of the tree. ```js @@ -169,6 +179,8 @@ console.log(root.getValue()); // v1 ``` ### count +O(1) + returns the count of nodes in the tree. ```js @@ -176,6 +188,8 @@ console.log(bst.count()); // 7 ``` ### traverseInOrder +O(n) + traverses the tree in order (left-node-right). ```js @@ -193,6 +207,8 @@ bst.traverseInOrder((node) => console.log(node.getKey())); ``` ### traversePreOrder +O(n) + traverses the tree pre order (node-left-right). ```js @@ -210,6 +226,8 @@ bst.traversePreOrder((node) => console.log(node.getKey())); ``` ### traversePostOrder +O(n) + traverses the tree post order (left-right-node). ```js @@ -227,6 +245,8 @@ bst.traversePostOrder((node) => console.log(node.getKey())); ``` ### remove +O(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 @@ -236,6 +256,8 @@ console.log(bst.count()); // 6 ``` ### clear +O(1) + clears the tree. ```js From 9f51900671f217b7e0232ad61a615b3bc1e8ea94 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:39:42 -0700 Subject: [PATCH 7/8] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e5c6109..aedc4ba 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ console.log(max.getValue()); // v4 ### lowerBound (floor) O(log(n)) -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. +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 an alias to the same function. ```js console.log(bst.lowerBound(60).getKey()); // 60 @@ -158,7 +158,7 @@ console.log(bst.lowerBound(10)); // null ### upperBound (ceil) O(log(n)) -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. +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 an alias to the same function. ```js console.log(bst.upperBound(75).getKey()); // 80 From 32e8bedd1c47c410c60d89fe697eb4f835106ad8 Mon Sep 17 00:00:00 2001 From: Eyas Ranjous Date: Wed, 13 Jul 2022 19:40:41 -0700 Subject: [PATCH 8/8] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aedc4ba..d9e6147 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ console.log(max.getValue()); // v4 ### lowerBound (floor) O(log(n)) -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 an alias to the same function. +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 @@ -158,7 +158,7 @@ console.log(bst.lowerBound(10)); // null ### upperBound (ceil) O(log(n)) -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 an alias to the same function. +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