From 96cf28b7151f2dd8a77e5bd1ddb7311da2dfc3ca Mon Sep 17 00:00:00 2001 From: GingerDragon7 Date: Sat, 3 Oct 2020 16:23:57 +0530 Subject: [PATCH 1/4] binary-search-tree --- Data Structures/binary_search_tree.js | 244 ++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 Data Structures/binary_search_tree.js diff --git a/Data Structures/binary_search_tree.js b/Data Structures/binary_search_tree.js new file mode 100644 index 0000000..df81edb --- /dev/null +++ b/Data Structures/binary_search_tree.js @@ -0,0 +1,244 @@ +/* Binary Search Tree */ + +/* Binary search trees allow fast lookup, addition and removal of items. +The way that they are set up means that, on average, each comparison allows +the operations to skip about half of the tree, so that each lookup, insertion + or deletion takes time proportional to the logarithm of the number of items + stored in the tree. */ + +class Node { + constructor(data, left = null, right = null) { + this.data = data; + this.left = left; + this.right = right; + } +} + +class BST { + constructor() { + this.root = null; + } + add(data) { + const node = this.root; + if (node === null) { + this.root = new Node(data); + return; + } else { + const searchTree = function(node) { + if (data < node.data) { + if (node.left === null) { + node.left = new Node(data); + return; + } else if (node.left !== null) { + return searchTree(node.left); + } + } else if (data > node.data) { + if (node.right === null) { + node.right = new Node(data); + return; + } else if (node.right !== null) { + return searchTree(node.right); + } + } else { + return null; + } + }; + return searchTree(node); + } + } + findMin() { + let current = this.root; + while (current.left !== null) { + current = current.left; + } + return current.data; + } + findMax() { + let current = this.root; + while (current.right !== null) { + current = current.right; + } + return current.data; + } + find(data) { + let current = this.root; + while (current.data !== data) { + if (data < current.data) { + current = current.left; + } else { + current = current.right; + } + if (current === null) { + return null; + } + } + return current; + } + isPresent(data) { + let current = this.root; + while (current) { + if (data === current.data) { + return true; + } + if (data < current.data) { + current = current.left; + } else { + current = current.right; + } + } + return false; + } + remove(data) { + const removeNode = function(node, data) { + if (node == null) { + return null; + } + if (data == node.data) { + // node has no children + if (node.left == null && node.right == null) { + return null; + } + // node has no left child + if (node.left == null) { + return node.right; + } + // node has no right child + if (node.right == null) { + return node.left; + } + // node has two children + var tempNode = node.right; + while (tempNode.left !== null) { + tempNode = tempNode.left; + } + node.data = tempNode.data; + node.right = removeNode(node.right, tempNode.data); + return node; + } else if (data < node.data) { + node.left = removeNode(node.left, data); + return node; + } else { + node.right = removeNode(node.right, data); + return node; + } + } + this.root = removeNode(this.root, data); + } + isBalanced() { + return (this.findMinHeight() >= this.findMaxHeight() - 1) + } + findMinHeight(node = this.root) { + if (node == null) { + return -1; + }; + let left = this.findMinHeight(node.left); + let right = this.findMinHeight(node.right); + if (left < right) { + return left + 1; + } else { + return right + 1; + }; + } + findMaxHeight(node = this.root) { + if (node == null) { + return -1; + }; + let left = this.findMaxHeight(node.left); + let right = this.findMaxHeight(node.right); + if (left > right) { + return left + 1; + } else { + return right + 1; + }; + } + inOrder() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traverseInOrder(node) { + node.left && traverseInOrder(node.left); + result.push(node.data); + node.right && traverseInOrder(node.right); + } + traverseInOrder(this.root); + return result; + }; + } + preOrder() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traversePreOrder(node) { + result.push(node.data); + node.left && traversePreOrder(node.left); + node.right && traversePreOrder(node.right); + }; + traversePreOrder(this.root); + return result; + }; + } + postOrder() { + if (this.root == null) { + return null; + } else { + var result = new Array(); + function traversePostOrder(node) { + node.left && traversePostOrder(node.left); + node.right && traversePostOrder(node.right); + result.push(node.data); + }; + traversePostOrder(this.root); + return result; + } + } + + levelOrder() { + let result = []; + let Q = []; + if (this.root != null) { + Q.push(this.root); + while(Q.length > 0) { + let node = Q.shift(); + result.push(node.data); + if (node.left != null) { + Q.push(node.left); + }; + if (node.right != null) { + Q.push(node.right); + }; + }; + return result; + } else { + return null; + }; + }; +} + + + +const bst = new BST(); + +bst.add(9); +bst.add(4); +bst.add(17); +bst.add(3); +bst.add(6); +bst.add(22); +bst.add(5); +bst.add(7); +bst.add(20); + +console.log(bst.findMinHeight()); +console.log(bst.findMaxHeight()); +console.log(bst.isBalanced()); +bst.add(10); +console.log(bst.findMinHeight()); +console.log(bst.findMaxHeight()); +console.log(bst.isBalanced()); +console.log('inOrder: ' + bst.inOrder()); +console.log('preOrder: ' + bst.preOrder()); +console.log('postOrder: ' + bst.postOrder()); + +console.log('levelOrder: ' + bst.levelOrder()); \ No newline at end of file From 3c82571cd7d55b5ab0aec0486d67f8b6d056d1eb Mon Sep 17 00:00:00 2001 From: GingerDragon7 Date: Sat, 3 Oct 2020 16:28:08 +0530 Subject: [PATCH 2/4] sets-javascript --- Data Structures/sets.js | 100 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Data Structures/sets.js diff --git a/Data Structures/sets.js b/Data Structures/sets.js new file mode 100644 index 0000000..804b45b --- /dev/null +++ b/Data Structures/sets.js @@ -0,0 +1,100 @@ +/* Sets */ + +function mySet() { + // the var collection will hold the set + var collection = []; + // this method will check for the presence of an element and return true or false + this.has = function(element) { + return (collection.indexOf(element) !== -1); + }; + // this method will return all the values in the set + this.values = function() { + return collection; + }; + // this method will add an element to the set + this.add = function(element) { + if(!this.has(element)){ + collection.push(element); + return true; + } + return false; + }; + // this method will remove an element from a set + this.remove = function(element) { + if(this.has(element)){ + index = collection.indexOf(element); + collection.splice(index,1); + return true; + } + return false; + }; + // this method will return the size of the collection + this.size = function() { + return collection.length; + }; + // this method will return the union of two sets + this.union = function(otherSet) { + var unionSet = new mySet(); + var firstSet = this.values(); + var secondSet = otherSet.values(); + firstSet.forEach(function(e){ + unionSet.add(e); + }); + secondSet.forEach(function(e){ + unionSet.add(e); + }); + return unionSet; + }; + // this method will return the intersection of two sets as a new set + this.intersection = function(otherSet) { + var intersectionSet = new mySet(); + var firstSet = this.values(); + firstSet.forEach(function(e){ + if(otherSet.has(e)){ + intersectionSet.add(e); + } + }); + return intersectionSet; + }; + // this method will return the difference of two sets as a new set + this.difference = function(otherSet) { + var differenceSet = new mySet(); + var firstSet = this.values(); + firstSet.forEach(function(e){ + if(!otherSet.has(e)){ + differenceSet.add(e); + } + }); + return differenceSet; + }; + // this method will test if the set is a subset of a different set + this.subset = function(otherSet) { + var firstSet = this.values(); + return firstSet.every(function(value) { + return otherSet.has(value); + }); + }; +} + +var setA = new mySet(); +var setB = new mySet(); +setA.add("a"); +setB.add("b"); +setB.add("c"); +setB.add("a"); +setB.add("d"); +console.log(setA.subset(setB)); +console.log(setA.intersection(setB).values()); +console.log(setB.difference(setA).values()); + +var setC = new Set(); +var setD = new Set(); +setC.add("a"); +setD.add("b"); +setD.add("c"); +setD.add("a"); +setD.add("d"); +console.log(setD.values()) +setD.delete("a"); +console.log(setD.has("a")); +console.log(setD.add("d")); From c75ea9ea94119b0d1c70710b2313fcd8b968a054 Mon Sep 17 00:00:00 2001 From: GingerDragon7 Date: Sat, 3 Oct 2020 16:31:25 +0530 Subject: [PATCH 3/4] rotate-array --- Data Structures/rotate_array.js | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Data Structures/rotate_array.js diff --git a/Data Structures/rotate_array.js b/Data Structures/rotate_array.js new file mode 100644 index 0000000..bcedd12 --- /dev/null +++ b/Data Structures/rotate_array.js @@ -0,0 +1,34 @@ + + +// Given an array, rotate the array to the right by k steps, where k is non-negative. + + + +var rotate = function(nums, k) { + let length = nums.length; + let result = [...nums]; + + for (let i = 0; i < length; i++) { + let index = (i + k) % length; + nums[index] = result[i]; + }; + +}; + + + +// Javascript O(1) space complexity + + +var rotate = function(nums, k) { + for(var i = 0; i < k; i++){ + nums.unshift(999); + } + var limit = nums.length - k; + for(var i = nums.length - 1; i >= limit; i--){ + nums[i-limit] = nums[i] + nums.pop(); + } +}; + + From 36c911e88d415ce3c5364d6d0915a3ec8a2d6a5f Mon Sep 17 00:00:00 2001 From: GingerDragon7 Date: Sat, 3 Oct 2020 17:08:14 +0530 Subject: [PATCH 4/4] hash-maps --- Data Structures/hash_map.js | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Data Structures/hash_map.js diff --git a/Data Structures/hash_map.js b/Data Structures/hash_map.js new file mode 100644 index 0000000..c59173c --- /dev/null +++ b/Data Structures/hash_map.js @@ -0,0 +1,77 @@ +/* Hash Table */ + +var hash = (string, max) => { + var hash = 0; + for (var i = 0; i < string.length; i++) { + hash += string.charCodeAt(i); + } + return hash % max; +}; + +let HashTable = function() { + + let storage = []; + const storageLimit = 14; + + this.print = function() { + console.log(storage) + } + + this.add = function(key, value) { + var index = hash(key, storageLimit); + if (storage[index] === undefined) { + storage[index] = [ + [key, value] + ]; + } else { + var inserted = false; + for (var i = 0; i < storage[index].length; i++) { + if (storage[index][i][0] === key) { + storage[index][i][1] = value; + inserted = true; + } + } + if (inserted === false) { + storage[index].push([key, value]); + } + } + }; + + this.remove = function(key) { + var index = hash(key, storageLimit); + if (storage[index].length === 1 && storage[index][0][0] === key) { + delete storage[index]; + } else { + for (var i = 0; i < storage[index].length; i++) { + if (storage[index][i][0] === key) { + delete storage[index][i]; + } + } + } + }; + + this.lookup = function(key) { + var index = hash(key, storageLimit); + if (storage[index] === undefined) { + return undefined; + } else { + for (var i = 0; i < storage[index].length; i++) { + if (storage[index][i][0] === key) { + return storage[index][i][1]; + } + } + } + }; + +}; + + +console.log(hash('quincy', 10)) + +let ht = new HashTable(); +ht.add('beau', 'person'); +ht.add('fido', 'dog'); +ht.add('rex', 'dinosour'); +ht.add('tux', 'penguin') +console.log(ht.lookup('tux')) +ht.print(); \ No newline at end of file