This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Graph = function() { | |
| this.nodes = {}; | |
| this.edges = {}; | |
| }; | |
| Graph.prototype.addNode = function(node) { | |
| this.nodes[node] = node; | |
| }; | |
| Graph.prototype.contains = function(node) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Node = function(value) { | |
| var node = {}; | |
| node.value = value; | |
| node.next = null; | |
| return node; | |
| }; | |
| var LinkedList = function() { | |
| var list = {}; | |
| list.head = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var BinarySearchTree = function(value) { | |
| var bst = Object.create(bstMethods); | |
| bst.left = null; | |
| bst.right = null; | |
| bst.value = value; | |
| return bst; | |
| }; | |
| var bstMethods = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Queue = function() { | |
| this._storage = {}; | |
| this._head = 0; | |
| this._tail = 0; | |
| }; | |
| Queue.prototype.enqueue = function(value) { | |
| this._storage[this._tail++] = value; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Tree = function(value) { | |
| this.value = value; | |
| this.children = []; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var elm = new Tree("Elm"); | |
| var numberTree = new Tree(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Tree.prototype.addChild = function(value) { | |
| this.children.push(new Tree(value)); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |