diff --git a/package.json b/package.json index 60e89dc..bd18027 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/binary-search-tree.js b/src/binary-search-tree.js index 262a838..7de2ef6 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -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; @@ -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 + } } } diff --git a/src/graph.js b/src/graph.js index 80eeb07..2ae26f5 100644 --- a/src/graph.js +++ b/src/graph.js @@ -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); } } @@ -35,11 +35,35 @@ 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 @@ -47,7 +71,7 @@ class Graph { 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) { @@ -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) { } @@ -76,4 +100,3 @@ class Graph { } module.exports = Graph; - diff --git a/src/tree.js b/src/tree.js index b6b7908..44a0b7b 100644 --- a/src/tree.js +++ b/src/tree.js @@ -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; diff --git a/tests/tree.test.js b/tests/tree.test.js index b20f227..67b0cb0 100644 --- a/tests/tree.test.js +++ b/tests/tree.test.js @@ -43,4 +43,3 @@ describe('Tree', () => { expect(tree.contains(8)).toBe(true); }); }); -