Skip to content
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

18.2 #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Chapter11-SortingAndSearching/sortByAnagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Problem: Write a method to sort an array of strings so that all the anagrams are next to each other.
*/

var sortByAnagram = function(array) {
var hashtable = {};
array.forEach(function(str){
var key = str.split('').sort().join('');
if (hashtable[key]) hashtable[key].push(str);
else hashtable[key] = [str];
});
var result = [];
for (var k in hashtable) {
for (var i = 0, len = hashtable[k].length; i < len; i++) {
result.push(hashtable[k][i]);
}
}
return result;
};

//runtime: n^2 log n
17 changes: 17 additions & 0 deletions Chapter18-Hard/shuffleDeck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Problem: Write a method to shuffle a deck of cards. It must be a perfect shuffle-each of the 52! permutations of the deck has to be equally likely.
* Assume that you are given a random number generator which is perfect.
*/

var shuffle = function(arr, n) {
if (!n) n = 0;
if (arr.length === n) return arr;
else {
//sub this with whatever random number generator is provided
var random = Math.floor(Math.random() * n);
var newVal = arr[random];
arr[random] = arr[n];
arr[n] = newVal;
return shuffle(arr, n+1);
};
};
22 changes: 22 additions & 0 deletions Chapter4-TreesAndGraphs/4.1-IsBalanced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Problem: Implement a function to check if a binary tree is balanced.
Here that is defined as a tree that the heights of the two subtrees of any node never differ by more than one.
*/

//building off of the binary search tree defined in the structures folder

BST.prototype.isBalanced = function() {
//check if node has both right & left (or neither) to see if balanced
function hasRightAndLeft(node){
if (!node.right && !node.left) return true;
else if (node.right && node.left) {
return true;
} else {
return false;
}
}

while (this.left && this.right) {
return (hasRightAndLeft(this.left) && hasRightAndLeft(this.right))
}
};
123 changes: 123 additions & 0 deletions DataStructures/BinarySearchTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
var BST = function(val) {
this.val = val;
};

BST.prototype.contains = function(val) {
var curr = this;
var found = false;

while(!found && curr) {
if (val === curr.val) {
found = true;
}
if (val > curr.val){
curr = curr.right;
} else if (val < curr.val) {
curr = curr.left;
}
}
return found;
};

BST.prototype.insert = function(val){
var valNode = new BST(val);

while(true) {
if (val < this.val) {
if (!this.left) {
this.left = valNode;
break;
} else {
return this.left.insert(val);
}
} else if (val > this.val) {
if (!this.right) {
this.right = valNode;
break;
} else {
return this.right.insert(val)
}
} else return;
}
};

BST.prototype.traverse = function(process){
function processNode(node) {
if (node.left) {
processNode(node.left)
}
process.call(this, node.val);
if (node.right) {
processNode(node.right);
}
}
processNode(this);
};

BST.prototype.size = function(){
var length = 0;

this.traverse(function(node){
length ++;
});
return length;
};

BST.prototype.toArray = function(){
var array = [];

this.traverse(function(node){
array.push(node.val);
});
return array;
};

BST.prototype.remove = function(value) {
var curr = this;
var parent = null;
var found = false;
var direction = null;
while (!found) {
if ( value === curr.val) {
found = true;
}
if (value < curr.val) {
parent = curr;
direction = 'left';
curr = curr.left;
} else if (value > curr.val) {
parent = curr;
direction = 'right';
curr = curr.right;
}
}
//no children - just remove node
if (!curr.left && !curr.right) {
parent[direction] = null;
//two children
} else if (curr.left && curr.right) {
var opposite = direction === 'right' ? 'left' : 'right';
var last = curr[opposite];
while (last[opposite]) {
last = last[opposite];
}
curr.val = last.val;

if (direction === 'left') {
last = curr.right;
while (last.right) {
last = last.right;
}
curr.val = last.val;
}
//one child - replace node with child
} else {
if (direction === 'left') {
if (curr.left) parent.left = curr.left;
else parent.left = curr.right;
} else {
if (curr.left) parent.right = curr.left;
else parent.right = curr.right;
}
}
}