From 10f077910ced2d115ee555c058f0fb0f3c08ee75 Mon Sep 17 00:00:00 2001 From: jnandez0586 Date: Thu, 1 Feb 2018 22:24:38 -0600 Subject: [PATCH] Jorge Hernandez: Finished about 50% --- package.json | 4 ++- src/binary-search-tree.js | 72 +++++++++++++++++++++++++++++++++++++-- src/graph.js | 12 +++---- src/tree.js | 15 +++++++- 4 files changed, 92 insertions(+), 11 deletions(-) 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..d236de9 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -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 @@ -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; diff --git a/src/graph.js b/src/graph.js index 80eeb07..04e0a36 100644 --- a/src/graph.js +++ b/src/graph.js @@ -40,18 +40,18 @@ 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 @@ -59,19 +59,19 @@ class Graph { // 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; } } diff --git a/src/tree.js b/src/tree.js index b6b7908..48bfe84 100644 --- a/src/tree.js +++ b/src/tree.js @@ -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; } }