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
| //create graph | |
| var graph = new Graph(); | |
| //create an array of vertices to add to graph | |
| var vertices = ['K','T','J','M','N','F']; | |
| //iterate vertices and add each vertex to the graph | |
| vertices.map(function(vertex){ | |
| graph.addVertex(vertex); | |
| }); | |
| //create edge connections |
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
| function Graph() { | |
| this.vertices = []; | |
| this.adjacentList = new Map(); | |
| } | |
| Graph.prototype = { | |
| addVertex: function(v){ | |
| //push vertex into the a vertices array | |
| this.vertices.push(v); | |
| //for each new vertex we add we need to create an empty array which |
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
| /* ES5 version */ | |
| function Graph() { | |
| this.vertices = []; | |
| this.adjacentList = new Map(); | |
| } | |
| Graph.prototype = { | |
| }; |
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
| BinarySearchTree.prototype = { | |
| //other code here | |
| remove: function(key) { | |
| /* | |
| First we find out if the node exists. If it doesn't exist, we return null and exit the function | |
| */ | |
| if(bst.root === null) { | |
| return false; | |
| } |
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
| BinarySearchTree.prototype = { | |
| /* | |
| other code here | |
| */ | |
| find: function(node, key) { | |
| var currentNode = node; | |
| if (currentNode === null){ | |
| return false; | |
| } |
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
| BinarySearchTree.prototype = { | |
| /* | |
| other code here | |
| */ | |
| inOrder: function(node){ | |
| if (node !== null) { | |
| //print the left subtree recursively | |
| this.inOrder(node.left); | |
| //print the root 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
| BinarySearchTree.prototype = { | |
| /* | |
| other code here | |
| */ | |
| inOrder: function(node){ | |
| if (node !== null) { | |
| //print the left subtree recursively | |
| this.inOrder(node.left); | |
| //print the root node | |
| console.log(node.toString()); |
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 bst = new BinarySearchTree(); | |
| bst.insert(60); | |
| bst.insert(30); | |
| bst.insert(85); | |
| bst.insert(95); | |
| bst.insert(80); | |
| bst.insert(35); | |
| bst.insert(20); | |
| console.log(bst); |
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
| //ES5 VERSION | |
| BinarySearchTree.prototype = { | |
| insert: function(key) { | |
| //create a new tree | |
| var newNode = new TreeNode(key); | |
| //if root is empty set the new node to be the root, else add recursively | |
| this.root === null ? this.root = newNode : this.insertTo(this.root, newNode); | |
| //increment counter | |
| this.count++; | |
| }, |
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
| //ES5 version | |
| function TreeNode(keyValue) { | |
| this.keyValue = keyValue; | |
| this.left = null; | |
| this.right = null; | |
| this.toString = function() { | |
| return this.keyValue; | |
| } | |
| } |