Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a56762c
Add info about properties
kuychaco Oct 12, 2015
d18b895
Move files
kuychaco Oct 12, 2015
b062427
Rename folder
kuychaco Oct 12, 2015
ba9ec63
Remove empty line
kuychaco Oct 13, 2015
b5b97d0
Implement solution
kuychaco Oct 13, 2015
aca6d4f
Implement solution
kuychaco Oct 13, 2015
305848c
Add exercise for prefix tree
kuychaco Oct 13, 2015
bbc51d3
Add solution for binary search in an array
kuychaco Nov 1, 2015
a6eb931
Add time complexity
kuychaco Nov 1, 2015
acec33d
Return node rather than just value, fix bug
kuychaco Nov 9, 2015
d5691bf
Implement graph data structure
kuychaco Nov 9, 2015
2f7b8a4
Return node instead of just value
kuychaco Nov 9, 2015
b22d12d
Implement traversal methods
kuychaco Nov 9, 2015
07b67d4
Change title of exercise section
kuychaco Nov 9, 2015
bef1fd1
Rename exercise section
kuychaco Nov 9, 2015
297d223
Add recursion questions and solutions
kuychaco Nov 17, 2015
3fecb15
add questions
kuychaco Nov 17, 2015
61260b9
Add exercise solutions
kuychaco Nov 17, 2015
fadfdc6
Add test for solution
kuychaco Nov 17, 2015
52eb253
Fix bug
kuychaco Nov 17, 2015
25b5386
Implement heap
kuychaco Nov 18, 2015
5da6bfa
Update quick.js
Nov 18, 2015
3f53a95
Use temp var for swap
kuychaco Nov 18, 2015
6509034
Implement heapsort solution
kuychaco Nov 18, 2015
b675981
Implement solution
kuychaco Nov 19, 2015
947e66a
Implement solution
kuychaco Nov 19, 2015
e4ac792
Implement factorial
kuychaco Nov 19, 2015
197c638
Add table resizing
kuychaco Nov 20, 2015
5868df8
Added the memoization of Fibonacci results to the memoization algorithm
diwadidu Nov 25, 2015
532ab1e
Merge pull request #2 from diwadidu/solutions
RebootJeff Nov 25, 2015
5b2fdde
changed variable name in enqueue function
LindsayElia Jun 14, 2016
0249970
recursionIntro solutions
atuckner Jun 14, 2016
9191961
Merge pull request #4 from abbiecat/solutions
Jun 14, 2016
0e9006d
Merge pull request #3 from LindsayElia/patch-1
Jun 14, 2016
3092c6b
fix typo 'Infinity'
francisngo Jan 19, 2017
22e8f8c
Merge pull request #8 from francisngo/solutions
RebootJeff Jan 19, 2017
6416d81
fix: Handle pop empty stack
Apr 2, 2017
58517aa
Merge pull request #10 from li-xinyang/patch-1
Jun 25, 2017
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
93 changes: 0 additions & 93 deletions data-structure-implementations/queue.js

This file was deleted.

189 changes: 189 additions & 0 deletions data-structures/binarySearchTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*

BINARY SEARCH TREES

Abstract data type

A binary search tree is a tree with the additional constraints:
- each node has only two child nodes (node.left and node.right)
- all the values in the left subtree of a node are less than or equal to the value of the node
- all the values in the right subtree of a node are greater than the value of the node


*** Operations:
bsTree.insert(value)
=> bsTree (return for chaining purposes)
Insert value into correct position within tree

bsTree.contains(value)
=> true/false
Return true if value is in tree, false if not

bsTree.traverseDepthFirst_inOrder(callback)
=> undefined
Invoke the callback for every node in a depth-first in-order (visit left branch, then current node, than right branch)
Note: In-Order traversal is most common type for binary trees. For binary search tree, this visits the nodes in ascending order (hence the name).

bsTree.traverseDepthFirst_preOrder(callback)
=> undefined
Invoke the callback for every node in a depth-first pre-order (visits current node before its child nodes)

bsTree.traverseDepthFirst_postOrder(callback)
=> undefined
Invoke the callback for every node in a depth-first post-order (visit the current node after its child nodes)

bsTree.traverseBreadthFirst(callback)
=> undefined
Invoke the callback for every node in a breadth-first order

bsTree.checkIfFull()
=> true/false
A binary tree is full if every node has either zero or two children (no nodes have only one child)

bsTree.checkIfBalanced()
=> true/false
For this exercise, let's say that a tree is balanced if the minimum height and the maximum height differ by no more than 1. The height for a branch is the number of levels below the root.

*** Additional Exercises:
A binary search tree was created by iterating over an array and inserting each element into the tree. Given a binary search tree with no duplicates, how many different arrays would result in the creation of this tree.

*/


// Binary Search Tree
function BinarySearchTree (value) {
this.value = value;
this.left = null;
this.right = null;
}

// O(log(n))
BinarySearchTree.prototype.insert = function(value) {
if (value <= this.value) {
if (this.left) this.left.insert(value);
else this.left = new BinarySearchTree(value);
}
else {
if (this.right) this.right.insert(value);
else this.right = new BinarySearchTree(value);
}
return this;
};

// O(log(n));
BinarySearchTree.prototype.contains = function(value) {
if (this.value === value) return true;
if (value < this.value) {
// if this.left doesn't exist return false
// if it does exist, check if its subtree contains the value
return !!this.left && this.left.contains(value);
}
if (value > this.value) {
// if this.right doesn't exist return false
// if it does exist, check if its subtree contains the value
return !!this.right && this.right.contains(value);
}
return false;
};

var bsTree = new BinarySearchTree(10);
bsTree.insert(5).insert(15).insert(8).insert(3).insert(7).insert(20).insert(17).insert(9).insert(14);

// In-Order traversal is most common
// visit left branch, then current node, than right branch
// For binary search tree, this visits the nodes in ascending order (hence the name)
// O(n)
BinarySearchTree.prototype.traverseDepthFirst_inOrder = function(fn) {
if (!this.left && !this.right) return fn(this);
if (this.left) this.left.traverseDepthFirst_inOrder(fn);
fn(this);
if (this.right) this.right.traverseDepthFirst_inOrder(fn);
};

var result_traverseDepthFirst_inOrder = [];
bsTree.traverseDepthFirst_inOrder(function(node) {
result_traverseDepthFirst_inOrder.push(node.value);
});
console.log(result_traverseDepthFirst_inOrder, 'should be [3,5,7,8,9,10,14,15,17,20]');

// Pre-Order traversal
// visits current node before its child nodes
// O(n)
BinarySearchTree.prototype.traverseDepthFirst_preOrder = function(fn) {
fn(this);
if (this.left) this.left.traverseDepthFirst_preOrder(fn);
if (this.right) this.right.traverseDepthFirst_preOrder(fn);
};

var result_traverseDepthFirst_preOrder = [];
bsTree.traverseDepthFirst_preOrder(function(node) {
result_traverseDepthFirst_preOrder.push(node.value);
});
console.log(result_traverseDepthFirst_preOrder, 'should be [10,5,3,8,7,9,15,14,20,17]');

// Post-Order traversal
// visit the current node after its child nodes
// O(n)
BinarySearchTree.prototype.traverseDepthFirst_postOrder = function(fn) {
if (this.left) this.left.traverseDepthFirst_postOrder(fn);
if (this.right) this.right.traverseDepthFirst_postOrder(fn);
fn(this);
};

var result_traverseDepthFirst_postOrder = [];
bsTree.traverseDepthFirst_postOrder(function(node) {
result_traverseDepthFirst_postOrder.push(node.value);
});
console.log(result_traverseDepthFirst_postOrder, 'should be [3,7,9,8,5,14,17,20,15,10]');

// O(n)
BinarySearchTree.prototype.traverseBreadthFirst = function(fn) {
var queue = [this];
while (queue.length) {
var node = queue.shift();
fn(node);
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
};

var result_traverseBreadthFirst = [];
bsTree.traverseBreadthFirst(function(node) {
result_traverseBreadthFirst.push(node.value);
});
console.log(result_traverseBreadthFirst, 'should be [10,5,15,3,8,14,20,7,9,17]');

// O(n)
// A binary tree is full if every node has either zero or two children (no nodes have only one child)
BinarySearchTree.prototype.checkIfFull = function() {
var result = true;
this.traverseBreadthFirst(function(node) {
if (!node.left && node.right) result = false;
else if (node.left && !node.right) result = false;
});
return result;
};

console.log(bsTree.checkIfFull(), 'should be false');

var fullBSTree = new BinarySearchTree(10);
fullBSTree.insert(5).insert(20).insert(15).insert(21).insert(16).insert(13);
console.log(fullBSTree.checkIfFull(), 'should be true');

// For this exercise, let's say that a tree is balanced if the minimum height and the maximum height differ by no more than 1. The height for a branch is the number of levels below the root.
// O(n)
BinarySearchTree.prototype.checkIfBalanced = function() {
var heights = [];
var recurse = function(node, height) {
if (!node.left && !node.right) return heights.push(height);
node.left && recurse(node.left, height+1);
node.right && recurse(node.right, height+1);
};
recurse(this, 1);
var min = Math.min.apply(null, heights);
var max = Math.max.apply(null, heights);
return max-min <= 1;
};

console.log(bsTree.checkIfBalanced(), 'should be true');
console.log(fullBSTree.checkIfBalanced(), 'should be false');
Loading