diff --git a/README.md b/README.md index f60a9d70..665bc09c 100644 --- a/README.md +++ b/README.md @@ -24,18 +24,21 @@ 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) - [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) 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..0a1ad377 --- /dev/null +++ b/src/_DataStructures_/Trees/BST/index.js @@ -0,0 +1,110 @@ +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) { + // eslint-disable-next-line no-param-reassign + root.leftChild = this.insert(root.leftChild, value); + return root; + } + if (value > root.value) { + // eslint-disable-next-line no-param-reassign + root.rightChild = this.insert(root.rightChild, value); + return root; + } + return root; + } + + preorder(root) { + /** returning an array so as to make testing easy */ + let arr = []; + if (root === null) return []; + arr.push(root.value); + + const left = this.preorder(root.leftChild); + arr = [...arr, ...left]; + + const right = this.preorder(root.rightChild); + arr = [...arr, ...right]; + return arr; + } + + inorder(root) { + /** left - root - right */ + 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; + } + + 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]; + } + + 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); +// console.log(bst.root); +// bst.insert(bst.root, 4); +// bst.insert(bst.root, 9); +// 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 preorder = bst.preorder(bst.root); +// console.log('Preorder Traversal - ', preorder); + +// const inorder = bst.inorder(bst.root); +// console.log('Inorder Traversal - ', inorder); + +// 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; 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