Skip to content

Added BST #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 8, 2019
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/_DataStructures_/Trees/BST/Node.js
Original file line number Diff line number Diff line change
@@ -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
}
};
110 changes: 110 additions & 0 deletions src/_DataStructures_/Trees/BST/index.js
Original file line number Diff line number Diff line change
@@ -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;