Skip to content

Instantly share code, notes, and snippets.

View scriptonian's full-sized avatar

I-am Konstantin scriptonian

View GitHub Profile
//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
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
/* ES5 version */
function Graph() {
this.vertices = [];
this.adjacentList = new Map();
}
Graph.prototype = {
};
@scriptonian
scriptonian / bst-remove.js
Created September 11, 2017 15:58
Javascript Binary Search Tree - Remove
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;
}
BinarySearchTree.prototype = {
/*
other code here
*/
find: function(node, key) {
var currentNode = node;
if (currentNode === null){
return false;
}
@scriptonian
scriptonian / bst-inorder-preorder-postorder.js
Last active September 9, 2017 15:32
Binary Search Tree Traversal
BinarySearchTree.prototype = {
/*
other code here
*/
inOrder: function(node){
if (node !== null) {
//print the left subtree recursively
this.inOrder(node.left);
//print the root node
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());
@scriptonian
scriptonian / bst-insert-test.js
Created September 6, 2017 14:57
Binary Search Tree - Insert Test
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);
@scriptonian
scriptonian / bst-insert-es5.js
Last active September 6, 2017 14:13
Binary Search Tree Insert Method
//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++;
},
@scriptonian
scriptonian / bst-shell-es5.js
Last active September 5, 2017 15:41
Adding the BST methods to class. Insert, find, remove, max, min and traversal
//ES5 version
function TreeNode(keyValue) {
this.keyValue = keyValue;
this.left = null;
this.right = null;
this.toString = function() {
return this.keyValue;
}
}