From 90d2cbdfe4c4805c0b76ce867790df9e9e29ca44 Mon Sep 17 00:00:00 2001 From: Brandi Apetsi Date: Thu, 1 Feb 2018 20:58:13 -0500 Subject: [PATCH] BrandiApetsi: Graphs Incomplete --- src/binary-search-tree.js | 39 ++++++++++++++++++++++++++++++++++----- src/tree.js | 11 +++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/binary-search-tree.js b/src/binary-search-tree.js index 262a838..4d5015f 100644 --- a/src/binary-search-tree.js +++ b/src/binary-search-tree.js @@ -2,6 +2,8 @@ /* 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; @@ -12,18 +14,38 @@ class BinarySearchTree { // assigns it to either the left or right subtree, // depending on its value insert(value) { - + if (value >= this.value) { + if (!this.right) this.right = new BinarySearchTree(value) + else this.right.insert(value); + } + else if (value <= this.value) { + if (!this.left) this.left = new BinarySearchTree(value) + else this.left.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)return false + else return this.left.contains(target) + } + else if (target > this.value) { + if(!this.right) return false + else 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); // cb function takes action on each node as we traverse tree + if (this.left) this.left.depthFirstForEach(cb); // if root node has a left child node, it will call function + if (this.right) this.left.depthFirstForEach(cb); //if root node has a right child node, it will call function + } + + }; // 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 @@ -31,8 +53,15 @@ 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) { + let queue = [this] + + while (queue.length) { + let treeNode = queue.shift() + cb(treenode) + if (treeNode.left) queue.push(treeNode.left) + if (treeNode.right) queue.push(treeNode.right) + } } -} module.exports = BinarySearchTree; diff --git a/src/tree.js b/src/tree.js index b6b7908..130a2c4 100644 --- a/src/tree.js +++ b/src/tree.js @@ -7,13 +7,20 @@ class Tree { } // Adds a new Tree node with the input value to the current Tree node addChild(value) { - + const newTree = new Tree(value); + this.value.push(newTree); } // 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) return true; + for (let i = 0; i < this.children.length; i++) { + if (this.children[i].contains(value)) { + return true; + } + } + return false; } }