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

BrandiApetsi: Graphs Incomplete #154

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
39 changes: 34 additions & 5 deletions src/binary-search-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,27 +14,54 @@ 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
// 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) {
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;
11 changes: 9 additions & 2 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down