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
53 changes: 45 additions & 8 deletions src/binary-search-tree.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://msdn.microsoft.com/en-us/library/aa289150(v=vs.71).aspx
/* eslint-disable global-require */
/* eslint-disable no-unused-vars */
/* eslint-disable no-trailing-spaces */
/* eslint-disable */
const Queue = require('./queue-helper');

class BinarySearchTree {
constructor(value) {
this.value = value;
Expand All @@ -12,27 +12,64 @@ class BinarySearchTree {
// assigns it to either the left or right subtree,
// depending on its value
insert(value) {

const newNode = new BinarySearchTree(value);
if (value < this.value) {
if (!this.left) {
this.left = newNode;
} else {
this.left.insert(value);
}
} else {
if (!this.right) {
this.right = newNode;
} else {
this.right.insert(value);
}
}
}

// Checks the binary search tree for the input target
// Can be written recursively or iteratively
contains(target) {

if (target === null) return;
if (target === this.value) return true; // base case
if (target < this.value) {
if (!this.left) return false;
return this.left.contains(target);
} else if (target > this.value) {
if (this.right === null) return false;
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) {
if (this.value !== null) cb(this.value);

if (this.left) this.left.depthFirstForEach(cb);
if (this.right) this.right.depthFirstForEach(cb);
}
// Traverses the tree in a breadth-first manner, i.e. in layers, starting
// 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
// through all those nodes first before moving on to the next layer of nodes
// Applies the given callback to each tree node in the process
// You'll need the queue-helper file for this. Or could you roll your own queue
// again. Whatever floats your boat.
breadthFirstForEach(cb) {

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

}
module.exports = BinarySearchTree;
32 changes: 26 additions & 6 deletions src/graph.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-unused-vars */

/* eslint-disable no-underscore-dangle */
/* eslint-disable no-trailing-spaces */
// Do not modify this GraphNode class
Expand Down Expand Up @@ -40,38 +41,57 @@ 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 = []) {

this._edges = {};
const node = new GraphNode(value, edges);
const graph = new Graph(node);

for (let i = 0; i < node.edges.length; i++) {
if (graph.vertices === 2) {
graph.vertices[i].pushToEdges(graph.vertices[i + 1]);
graph.vertices[i + 1].pushToEdges(graph.vertices[i]);
}
}
return graph;
}
// Checks all the vertices of the graph for the target value
// Returns true or false
contains(value) {

if (this.vertices.indexOf(value) === -1) {
return false;
}
return true;
}
// 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) {

this._edges = value;
}
// 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) {

this._value = fromVertex;
if (fromVertex.edges.includes(toVertex) && toVertex.edges.includes(fromVertex)) {
return true;
}
}
// 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) {

this._edges = fromVertex;
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/tree.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
/* eslint-disable no-trailing-spaces */
/* eslint-disable no-unused-vars */
/* eslint-disable */
class Tree {
constructor(value) {
this.value = value;
this.children = [];
}
// Adds a new Tree node with the input value to the current Tree node
addChild(value) {

this.children.push(new Tree(value));
}
// 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) {

if (value === this.value) return true;
for (let i = 0; i < this.children.length; i++) {
if(this.children[i].contains(value)) {
return true;
}
}
return false;
}
}

Expand Down