From 5aeb51b5eb2c4e780fb880c760a701c70fe54e86 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Mon, 7 Oct 2019 14:35:48 +0530 Subject: [PATCH 01/11] fix: More meaningful name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 48c66b20..6cb00d75 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack) - [Baseball Game](src/_DataStructures_/Stack/baseball-game) - - [Minimum Stack](src/_DataStructures_/Stack/min-stack) + - [Find minimum in the Stack](src/_DataStructures_/Stack/min-stack) - [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) From 31c5648ba285e77e611178cc17504f7fdd273fbc Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 12:51:27 +0530 Subject: [PATCH 02/11] refactor: folder structure --- src/_DataStructures_/{ => Trees}/SuffixTree/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/_DataStructures_/{ => Trees}/SuffixTree/index.js (100%) diff --git a/src/_DataStructures_/SuffixTree/index.js b/src/_DataStructures_/Trees/SuffixTree/index.js similarity index 100% rename from src/_DataStructures_/SuffixTree/index.js rename to src/_DataStructures_/Trees/SuffixTree/index.js From f32e9aaf5a036e06ea086e3e86052ef656a0f167 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 13:27:28 +0530 Subject: [PATCH 03/11] update: BST insertion added --- src/_DataStructures_/Trees/BST/Node.js | 7 +++++ src/_DataStructures_/Trees/BST/index.js | 42 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/_DataStructures_/Trees/BST/Node.js create mode 100644 src/_DataStructures_/Trees/BST/index.js diff --git a/src/_DataStructures_/Trees/BST/Node.js b/src/_DataStructures_/Trees/BST/Node.js new file mode 100644 index 00000000..2b515b2a --- /dev/null +++ b/src/_DataStructures_/Trees/BST/Node.js @@ -0,0 +1,7 @@ +module.exports = class Node { + constructor(value) { + this.value = value; + this.leftChild = null; // will be a node + this.rightChild = null; // will be a node + } +}; diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js new file mode 100644 index 00000000..2c11b29d --- /dev/null +++ b/src/_DataStructures_/Trees/BST/index.js @@ -0,0 +1,42 @@ +const Node = require('./Node'); + +class BinarySearchTree { + constructor(value) { + this.root = new Node(value); + } + + insert(root, value) { + if (root === null) { + const newNode = new Node(value); + // eslint-disable-next-line no-param-reassign + root = newNode; + return root; + } + + if (value < root.value) { + // go left + // eslint-disable-next-line no-param-reassign + root.leftChild = this.insert(root.leftChild, value); + return root; + } + if (value > root.value) { + // go right + // eslint-disable-next-line no-param-reassign + root.rightChild = this.insert(root.rightChild, value); + return root; + } + return root; + } +} + +// const bst = new BinarySearchTree(10); +// console.log(bst.root); +// bst.insert(bst.root, 12); +// bst.insert(bst.root, 9); +// bst.insert(bst.root, 19); +// bst.insert(bst.root, 11); +// bst.insert(bst.root, 6); + +// console.log(bst.root); + +module.exports = BinarySearchTree; From 7b65d6a9092437fd1b4629c606788542e02009c6 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 13:49:02 +0530 Subject: [PATCH 04/11] update: preorder traversal --- src/_DataStructures_/Trees/BST/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index 2c11b29d..23e94f41 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -27,6 +27,15 @@ class BinarySearchTree { } return root; } + + preorder(root) { + if (root === null) return; + // eslint-disable-next-line no-console + console.log(`${root.value} `); + + this.preorder(root.leftChild); + this.preorder(root.rightChild); + } } // const bst = new BinarySearchTree(10); @@ -39,4 +48,6 @@ class BinarySearchTree { // console.log(bst.root); +// bst.preorder(bst.root); + module.exports = BinarySearchTree; From 22787ddacac9a0ec357de4db86a59b1dbea3eb27 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 13:52:41 +0530 Subject: [PATCH 05/11] update: return pre-order traversal as an array --- src/_DataStructures_/Trees/BST/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index 23e94f41..8313943a 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -29,12 +29,17 @@ class BinarySearchTree { } preorder(root) { - if (root === null) return; + /** returning an array so as to make testing easy */ + let arr = []; + if (root === null) return []; // eslint-disable-next-line no-console - console.log(`${root.value} `); + arr.push(root.value); - this.preorder(root.leftChild); - this.preorder(root.rightChild); + const left = this.preorder(root.leftChild); + arr = [...arr, ...left]; + const right = this.preorder(root.rightChild); + arr = [...arr, ...right]; + return arr; } } @@ -48,6 +53,7 @@ class BinarySearchTree { // console.log(bst.root); -// bst.preorder(bst.root); +// const a = bst.preorder(bst.root); +// console.log('arr = ', a); module.exports = BinarySearchTree; From 312e43a5d85d09cdd514e81d472480a103197388 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 13:53:53 +0530 Subject: [PATCH 06/11] cleanup --- src/_DataStructures_/Trees/BST/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index 8313943a..0e1dfed8 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -32,11 +32,11 @@ class BinarySearchTree { /** returning an array so as to make testing easy */ let arr = []; if (root === null) return []; - // eslint-disable-next-line no-console arr.push(root.value); const left = this.preorder(root.leftChild); arr = [...arr, ...left]; + const right = this.preorder(root.rightChild); arr = [...arr, ...right]; return arr; From 34593cbef565f7559d7d917732003217184cc96e Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 14:07:24 +0530 Subject: [PATCH 07/11] cleanup --- src/_DataStructures_/Trees/BST/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index 0e1dfed8..50b36889 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -14,13 +14,11 @@ class BinarySearchTree { } if (value < root.value) { - // go left // eslint-disable-next-line no-param-reassign root.leftChild = this.insert(root.leftChild, value); return root; } if (value > root.value) { - // go right // eslint-disable-next-line no-param-reassign root.rightChild = this.insert(root.rightChild, value); return root; From 78cb213d15e3101d7dd5b08e161844d0d8d835a6 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 14:20:55 +0530 Subject: [PATCH 08/11] update: implementation of Inorder Traversal --- src/_DataStructures_/Trees/BST/index.js | 32 +++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index 50b36889..af720c88 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -39,19 +39,37 @@ class BinarySearchTree { arr = [...arr, ...right]; return arr; } + + inorder(root) { + if (root === null) return []; + let arr = []; + const left = this.inorder(root.leftChild); + arr = [...left, ...arr]; + + // print root + arr = [...arr, root.value]; + + const right = this.inorder(root.rightChild); + arr = [...arr, ...right]; + return arr; + } } -// const bst = new BinarySearchTree(10); +// const bst = new BinarySearchTree(6); // console.log(bst.root); -// bst.insert(bst.root, 12); +// bst.insert(bst.root, 4); // bst.insert(bst.root, 9); -// bst.insert(bst.root, 19); -// bst.insert(bst.root, 11); -// bst.insert(bst.root, 6); +// bst.insert(bst.root, 2); +// bst.insert(bst.root, 5); +// bst.insert(bst.root, 8); +// bst.insert(bst.root, 12); // console.log(bst.root); -// const a = bst.preorder(bst.root); -// console.log('arr = ', a); +// const preorder = bst.preorder(bst.root); +// console.log('Preorder Traversal - ', preorder); + +// const inorder = bst.inorder(bst.root); +// console.log('Inorder Traversal - ', inorder); module.exports = BinarySearchTree; From 5fd958e63e14b35d4e76c477f3b71bfabad006e1 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 14:26:31 +0530 Subject: [PATCH 09/11] update: added Postorder traversal --- src/_DataStructures_/Trees/BST/index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index af720c88..e48b9164 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -41,6 +41,7 @@ class BinarySearchTree { } inorder(root) { + /** left - root - right */ if (root === null) return []; let arr = []; const left = this.inorder(root.leftChild); @@ -53,6 +54,21 @@ class BinarySearchTree { arr = [...arr, ...right]; return arr; } + + postorder(root) { + /** left - right - root */ + + if (root === null) return []; + let arr = []; + + const left = this.postorder(root.leftChild); + arr = [...left, ...arr]; + + const right = this.postorder(root.rightChild); + arr = [...arr, ...right]; + + return [...arr, root.value]; + } } // const bst = new BinarySearchTree(6); @@ -72,4 +88,7 @@ class BinarySearchTree { // const inorder = bst.inorder(bst.root); // console.log('Inorder Traversal - ', inorder); +// const postorder = bst.postorder(bst.root); +// console.log('Postorder Traversal - ', postorder); + module.exports = BinarySearchTree; From 8fae46e9e674b201b7a67e143eae6cc9923a670f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 14:28:21 +0530 Subject: [PATCH 10/11] update: entry for trees in README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9af03ae1..665bc09c 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,16 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation) - [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits) - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - - [Queue](src/_DataStructures_/Queue) + - [Weave](src/_DataStructures_/Queue/weave) - [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) -- [Suffix Tree](src/_DataStructures_/SuffixTree) + +- [Trees](src/_DataStructures_/Trees) + - [Binary Search Tree](src/_DataStructures_/Trees/BST) + - [Suffix Tree](src/_DataStructures_/SuffixTree) ### Logical Problems @@ -67,7 +70,6 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [LRU Cache](src/_Algorithms_/lru-cache) - ### Path Finder - [A\*](src/PathFinder/AStart) From 34750133ae1ad8cb8b82c10e511fc6258eba222e Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 8 Oct 2019 15:03:36 +0530 Subject: [PATCH 11/11] update: search in BST --- src/_DataStructures_/Trees/BST/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/_DataStructures_/Trees/BST/index.js b/src/_DataStructures_/Trees/BST/index.js index e48b9164..0a1ad377 100644 --- a/src/_DataStructures_/Trees/BST/index.js +++ b/src/_DataStructures_/Trees/BST/index.js @@ -69,6 +69,19 @@ class BinarySearchTree { return [...arr, root.value]; } + + search(root, value) { + if (root === null) return false; + if (value === root.value) return true; + + if (value < root.value) { + return this.search(root.leftChild, value); + } + if (value > root.value) { + return this.search(root.rightChild, value); + } + return false; + } } // const bst = new BinarySearchTree(6); @@ -91,4 +104,7 @@ class BinarySearchTree { // const postorder = bst.postorder(bst.root); // console.log('Postorder Traversal - ', postorder); +// const search = 18; +// console.log(`Search for ${search}`, bst.search(bst.root, search)); + module.exports = BinarySearchTree;