Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jorge Hernandez: Finished about 50% #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"test": "eslint tests/*.js && eslint src/*.js && jest --verbose",
"lint": "eslint",
"test:watch": "npm test -- --watch"
"test:watch": "npm test -- --watch",
"test:nolint": "jest --verbose",
"mytest": "npm run test:nolint -- --watch"
},
"repository": {
"type": "git",
Expand Down
72 changes: 69 additions & 3 deletions src/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,64 @@
/* eslint-disable global-require */
/* eslint-disable no-unused-vars */
/* eslint-disable no-trailing-spaces */
const Queue = require('./queue-helper');

class BinarySearchTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
this.queue = new Queue();
}
// Wraps the input value in a new BinarySearchTree and
// assigns it to either the left or right subtree,
// depending on its value
insert(value) {

if (value <= this.value) {
if (this.left === null) {
this.left = new BinarySearchTree(value);
} else {
this.left.insert(value);
}
} else if (value > this.value) {
if (this.right === null) {
this.right = new BinarySearchTree(value);
} else {
this.right.insert(value);
}
}
}

// Checks the binary search tree for the input target
// Can be written recursively or iteratively
contains(target) {
if (this.value === target) {
return true;
}

if (target < this.value) {
if (this.left === null) {
return false;
}
return this.left.contains(target);
} else if (target > this.value) {
if (this.right === null) {
return false;
}
return this.right.contains(target);
}
}

// 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) {

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
// at the root node, going down to the root node's children, and iterating
Expand All @@ -31,8 +68,37 @@ 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 container = this.queue.storage;
const arr = [];
if (!this) {
return;
}
this.queue.enqueue(this);
while (container.length > 0) {
const currentNode = container[0];

if (currentNode.left !== null) {
this.queue.enqueue(currentNode.left);
}
if (currentNode.right !== null) {
this.queue.enqueue(currentNode.right);
}
arr.push(this.queue.dequeue());
}
}
}

const fam = new BinarySearchTree(1);
fam.insert(2);
fam.insert(6);
fam.insert(1);
fam.insert(9);
fam.insert(1);
// console.log(fam);
// console.log('------------');
// console.log(fam.right);
// console.log('------------');
// console.log(fam.left);
console.log(fam.breadthFirstForEach());

module.exports = BinarySearchTree;
12 changes: 6 additions & 6 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,38 @@ 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.value = value;
}
// Checks all the vertices of the graph for the target value
// Returns true or false
contains(value) {

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

this.value = 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.value = fromVertex;
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@ class Tree {
}
// 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) {
return !!this.findBFS(value);
}

findBFS(value) {
while (this.children.length) {
const node = this.children.shift();
if (node.value === value) {
return node;
}
for (let i = 0; i < node.children.length; i++) {
this.children.push(node.children[i]);
}
}
return null;
}
}

Expand Down