Skip to content

Instantly share code, notes, and snippets.

View Cfeusier's full-sized avatar

Clark Feusier Cfeusier

View GitHub Profile
@Cfeusier
Cfeusier / undirected-graph.js
Created December 14, 2014 09:20
Simple undirected graph implementation in JavaScript
var Graph = function() {
this.nodes = {};
this.edges = {};
};
Graph.prototype.addNode = function(node) {
this.nodes[node] = node;
};
Graph.prototype.contains = function(node) {
@Cfeusier
Cfeusier / singly-linked-list.js
Created December 14, 2014 09:25
Singly Linked List implementation in JavaScript
var Node = function(value) {
var node = {};
node.value = value;
node.next = null;
return node;
};
var LinkedList = function() {
var list = {};
list.head = null;
@Cfeusier
Cfeusier / binary-search-tree.js
Last active August 29, 2015 14:11
Binary Search Tree implementation in JavaScript
var BinarySearchTree = function(value) {
var bst = Object.create(bstMethods);
bst.left = null;
bst.right = null;
bst.value = value;
return bst;
};
var bstMethods = {};
@Cfeusier
Cfeusier / set.js
Created December 14, 2014 09:40
Simple String Set implementation in JavaScript
var Set = function() {
var set = Object.create(setPrototype);
set._storage = {};
return set;
};
var setPrototype = {};
setPrototype.add = function(item) {
if (!this.contains(item)) this._storage[item] = item;
@Cfeusier
Cfeusier / stack.js
Last active August 29, 2015 14:11
Stack implementation in JavaScript
var Stack = function() {
var stack = Object.create(stackMethods);
stack.storage = {};
stack.count = 0;
return stack;
};
var stackMethods = {
push: function (value) {
this.storage[this.count++] = value;
@Cfeusier
Cfeusier / queue.js
Created December 14, 2014 09:58
Queue implementation in JavaScript
var Queue = function() {
this._storage = {};
this._head = 0;
this._tail = 0;
};
Queue.prototype.enqueue = function(value) {
this._storage[this._tail++] = value;
};
@Cfeusier
Cfeusier / tree_constructor.js
Created December 27, 2014 20:22
Constructor Function for Tree
var Tree = function(value) {
this.value = value;
this.children = [];
};
@Cfeusier
Cfeusier / tree_instance.js
Created December 27, 2014 20:30
Example of a tree instance
var elm = new Tree("Elm");
var numberTree = new Tree(1);
@Cfeusier
Cfeusier / tree_addChild.js
Created December 27, 2014 20:42
addChild functionality for Tree implementation
Tree.prototype.addChild = function(value) {
this.children.push(new Tree(value));
};
@Cfeusier
Cfeusier / tree_contains.js
Created December 27, 2014 21:01
contains functionality for Tree implementation
Tree.prototype.contains = function(targetValue, root) {
root = root || this;
// base case
if (root.value === targetValue) return true;
for (var i = 0; i < root.children.length; i++) {
// recurse through children
if (this.contains(targetValue, root.children[i])) {
// target found
return true;