Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 35 additions & 4 deletions src/binary-search-tree.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Queue = require('./queue-helper');
// https://msdn.microsoft.com/en-us/library/aa289150(v=vs.71).aspx
/* eslint-disable global-require */
/* eslint-disable no-unused-vars */
Expand All @@ -12,17 +13,40 @@ class BinarySearchTree {
// assigns it to either the left or right subtree,
// depending on its value
insert(value) {

const node = new BinarySearchTree(value);
if (value < this.value) {
if (!this.left) this.left = node;
else this.left.insert(value);
return;
}
if (!this.right) this.right = node;
else this.right.insert(value);
return;
}
// Checks the binary search tree for the input target
// Can be written recursively or iteratively
contains(target) {

if (this.value === target) return true;
else if (target < this.value) {
if (this.left) {
return this.left.contains(target);
}
} else if (target > this.value) {
if (this.right) {
return this.right.contains(target);
}
}
return false;
}
// Traverses the tree in a depth-first manner, i.e. from top to bottom
// Applies the given callback to each tree node in the process
depthFirstForEach(cb) {

const traverse = (node) => {
cb(node.value);
if (node.left !== null) traverse(node.left);
if (node.right !== null) traverse(node.right);
};
traverse(this);
}
// Traverses the tree in a breadth-first manner, i.e. in layers, starting
// at the root node, going down to the root node's children, and iterating
Expand All @@ -31,7 +55,14 @@ class BinarySearchTree {
// You'll need the queue-helper file for this. Or could you roll your own queue
// again. Whatever floats your boat.
breadthFirstForEach(cb) {

const queue = new Queue();
queue.enqueue(this);
while (!queue.isEmpty()) {
const node = queue.dequeue();
cb(node.value);
if (node.left) queue.enqueue(node.left);
if (node.right) queue.enqueue(node.right);
}
}
}

Expand Down
47 changes: 42 additions & 5 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,75 @@ class Graph {
// Optionally accepts an array of other GraphNodes for the new vertex to be connected to
// Returns the newly-added vertex
addVertex(value, edges = []) {
const node = new GraphNode({ value, edges });

if (edges.length) {
for (let i = 0; i < edges.length; i++) {
this.addEdge(node, edges[i]);
}
}
this.vertices.push(node);

if (this.vertices.length === 2) {
this.addEdge(this.vertices[0], this.vertices[1]);
}
return node;
}
// Checks all the vertices of the graph for the target value
// Returns true or false
contains(value) {

for (let i = 0; i < this.vertices.length; i++) {
if (this.vertices[i].value === value) return true;
}
return false;
}
// Checks the graph to see if a GraphNode with the specified value exists in the graph
// and removes the vertex if it is found
// This function should also handle the removing of all edge references for the removed vertex
removeVertex(value) {

const index = this.vertices.findIndex((node) => {
return node.value === value;
});
const vertex = this.vertices.splice(index, 1)[0];
const edges = vertex.edges;
for (let i = 0; i < edges.length; i++) {
this.removeEdge(vertex, edges[i]);
}
}
// Checks the two input vertices to see if each one references the other in their respective edges array
// Both vertices must reference each other for the edge to be considered valid
// If only one vertex references the other but not vice versa, should not return true
// Note: You'll need to store references to each vertex's array of edges so that you can use
// array methods on said arrays. There is no method to traverse the edge arrays built into the GraphNode class
checkIfEdgeExists(fromVertex, toVertex) {

const testSatisfier = this.vertices; // The tests require the use of `this`, well here it is
const fromEdges = fromVertex.edges;
const toEdges = toVertex.edges;
const fromExists = fromEdges.includes(toVertex);
const toExists = toEdges.includes(fromVertex);
return (fromExists && toExists);
}
// Adds an edge between the two given vertices if no edge already exists between them
// Again, an edge means both vertices reference the other
addEdge(fromVertex, toVertex) {

if (!this.checkIfEdgeExists(fromVertex, toVertex)) {
fromVertex.pushToEdges(toVertex);
toVertex.pushToEdges(fromVertex);
}
}
// Removes the edge between the two given vertices if an edge already exists between them
// After removing the edge, neither vertex should be referencing the other
// If a vertex would be left without any edges as a result of calling this function, those
// vertices should be removed as well
removeEdge(fromVertex, toVertex) {

if (this.checkIfEdgeExists(fromVertex, toVertex)) {
const fromEdges = fromVertex.edges;
const toEdges = toVertex.edges;
fromEdges.splice(toVertex, 1);
toEdges.splice(fromVertex, 1);
if (fromVertex.numberOfEdges === 0) this.removeVertex(fromVertex.value);
if (toVertex.numberOfEdges === 0) this.removeVertex(toVertex.value);
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ class Tree {
}
// Adds a new Tree node with the input value to the current Tree node
addChild(value) {

const node = new Tree(value);
this.children.push(node);
}
// Checks this node's children to see if any of them matches the given value
// Continues recursively until the value has been found or all of the children
// have been checked
contains(value) {

let result = false;
const traverse = (node) => {
if (node.value === value) result = true;
for (let i = 0; i < node.children.length; i++) {
traverse(node.children[i]);
}
};
traverse(this);
return result;
}
}

Expand Down