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
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
86 changes: 79 additions & 7 deletions src/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/* eslint-disable global-require */
/* eslint-disable no-unused-vars */
/* eslint-disable no-trailing-spaces */
const Queue = require('./queue-helper.js');
console.log(Queue);

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

// if (newBST < value...go left)
// else go right
const child = new BinarySearchTree(value); // create a child node and set it to BST(value) in order to inherit properties
if(child.value < this.value) { // check which way i need to start traversing the tree. if child's value < than current node value, go left
if(this.left === null) { // must check that there is something at this.left
this.left = child; // if there isn't, set this.left to be child (would have a value now)
} else {
this.left.insert(value); // otherwise call insert on this.left (recursively checking again which way to go)
}
}
if(child.value > this.value) { // if child is greater, check that this.right has a value, then do the same thing.
if(this.right === null) {
this.right = child;
} else {
this.right.insert(value);
}
}
}
// Checks the binary search tree for the input target
// Can be written recursively or iteratively
contains(target) {

// if value = target, done
// if target < value, this.left.contains(value)
// if target > value, this.right.contains(value)
if(target === this.value) { // if what you're looking for is that value you're done
return true;
}
if(target < this.value) { // if less, go left, check that there's something there, then run contains on that to check again
if(this.left !== null) {
return this.left.contains(target);
}
}
if(target > this.value) { // if more, go right, check that there's something there, then run contains on that to check again
if(this.right !== null) {
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) {

depthFirstForEach(cb) { // traverses deeply down each branch
// 5,2,3,7,9
// if left, cb(this.left(this.value))
cb(this.value); // first run the callback on the current value
if(this.left !== null) { // check that this.left exists, then call depthFirstForEach on this.left with the cb. it will run the cb on whatever is in there
this.left.depthFirstForEach(cb);
}
if(this.right !== null) {
this.right.depthFirstForEach(cb); // same things, but if you went right.
}
}
// 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) {

breadthFirstForEach(cb) { // traverse by levels
// make queue, do some stuff, shift out, next layer
// 5
// 2 7
// 3 9
// [5 2 7 3 9]
// enqueue root,
// check queue !== null
// enqueue children (this.right, this.left)
//dequeue first element, call cb(element)
// while queue !== null (2,7) check this.left && this.right for enqueue
// enqueue (this.right (3))
// dequeue 2, callback(2)
// queue (7,3)
// enqueue (this.right (9))
// dequeue 7 callback(7)
// queue (3,9)
// check --> dequeue 3, callback(3)
// check --> dequeue 9, callback (9)
const queue = new Queue(); // need a queue in order to store and pop off levels as i go
queue.enqueue(this); // enqueue the entire tree.
while(!queue.isEmpty()) { // while the queue isn't empty...
const node = queue.dequeue(); // set a node to be the queue without anything in there. I want a reference to the tree.
if(node.left) { // if i'm going left
queue.enqueue(node.left); // enqueue node.left
}
if(node.right) {
queue.enqueue(node.right); // if i went right, enqueue node.right
}
cb(node.value); // run the callback on the value
}
}
}

Expand Down
43 changes: 33 additions & 10 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class GraphNode {
this._edges = edges;
}

get value() {
get value() { // give me current value
return this._value;
}

get edges() {
get edges() { // give me current edge
return this._edges;
}

get numberOfEdges() {
get numberOfEdges() { // give me how many edges i have
return this._edges.length;
}

set edges(x) {
set edges(x) { // sets an edge to parameter
this._edges = x;
}

pushToEdges(y) {
pushToEdges(y) { // push parameter into edges array
this._edges.push(y);
}
}
Expand All @@ -35,19 +35,43 @@ class Graph {
this.vertices = [];
}
// Wraps the input value in a new GraphNode and adds it to the array of vertices
// If there are only two nodes in the graph, they need to be automatically
// If there are only two nodes in the graph, they need to be automatically
// connected via an edge
// Optionally accepts an array of other GraphNodes for the new vertex to be connected to
// Returns the newly-added vertex
addVertex(value, edges = []) {
// need new GraphNode
// add to array of vertices (this.vertices.push(node))
// if(only two nodes ---> conect via an edge(set edges? push to edges?))
// accept array of other nodes, connects new vertex, Returns
const graphNode = new GraphNode({value, edges});
// this.vertices.push(graphNode.value) is what i need to do;

// i might need edge values... const edgeValues = [].
const edgeValues = edges.map(edge => edge.value); // edgeValues is an array that changes each edge and spits out the corresponding edge value.

// add nodeValue to edges (node.pushToEdges)
if (edges.length !== null) { // if it's defined/has shit in there
edges.forEach(edgeValues => { // ... run forEach on each edge value...
graphNode.pushToEdges(edgeValues); // on graphnode, call pushToEdges with those edge values...
});
}

// now push shit
this.vertices.push(graphNode);


// if(this.vertices.length === 2) {
// this.vertices[?].pushToEdges(graphNode.value)
// }

}
// Checks all the vertices of the graph for the target value
// Returns true or false
contains(value) {

}
// Checks the graph to see if a GraphNode with the specified value exists in the graph
// 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) {
Expand All @@ -56,13 +80,13 @@ class Graph {
// 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
// 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) {

}
// Adds an edge between the two given vertices if no edge already exists between them
// Again, an edge means both vertices reference the other
// Again, an edge means both vertices reference the other
addEdge(fromVertex, toVertex) {

}
Expand All @@ -76,4 +100,3 @@ class Graph {
}

module.exports = Graph;

17 changes: 13 additions & 4 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ class Tree {
this.value = value;
this.children = [];
}
// Adds a new Tree node with the input value to the current Tree node
// Adds a new Tree node with the input value to the current Tree node
addChild(value) {

const child = new Tree(value); // create the child node that inherits the Tree properties (value of "value" and that it has access to the array)
this.children.push(child); // push child into the array
}

// 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(this.value === value) { // quickly check if your current value (root) is the value you're searching for
return true;
}
for(let i = 0; i < this.children.length; i++) { // otherwise.... iterate all the elements in children
if(this.children[i].contains(value)){ // run contains on that element you're on, passing in "value".. will loop to first if and check.
return true;
};
}
return false; // otherwise, return false (value not in tree)
}
}

module.exports = Tree;
1 change: 0 additions & 1 deletion tests/tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ describe('Tree', () => {
expect(tree.contains(8)).toBe(true);
});
});