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

Earth - Ringo #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
138 changes: 117 additions & 21 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,142 @@ class Tree {
this.root = null;
}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n) -- assuming the tree is balanced, as the number of nodes increases
// the number of times addHelper has to be called will only increase logarithmically
// Space Complexity: O(log n) -- assuming the tree is balanced, the number of calls to addHelper
// that will be on the stack at one time increases logarithmically, rather than linearly
add(key, value) {
Comment on lines +15 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
const newNode = new TreeNode(key, value);

if (this.root === null) {
this.root = newNode;
return;
} else {
const currentNode = this.root;
return this.addHelper(newNode, currentNode);
}
}

addHelper(newNode, currentNode) {
if (newNode.key > currentNode.key && currentNode.right === null) {
currentNode.right = newNode;
return;
} else if (newNode.key <= currentNode.key && currentNode.left === null) {
currentNode.left = newNode;
return;
} else if (newNode.key > currentNode.key) {
return this.addHelper(newNode, currentNode.right);
} else {
return this.addHelper(newNode, currentNode.left);
}
}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n) -- assuming the tree is balanced, as tree increases in size,
// the number of times findHelper has to be called will only increase logarithmically
// Space Complexity: O(log n) -- assuming the tree is balanced, the number of calls to findHelper
// on the stack at one time increases logarithmically
find(key) {
Comment on lines +45 to 49

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
const current = this.root;
return this.findHelper(key, current);
}

findHelper(key, current) {
if (current === null) return null
if (current.key === key) return current.value

if (current.key < key) {
return this.findHelper(key, current.right);
} else {
return this.findHelper(key, current.left);
}
}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n). Method needs to visit every node in the tree
// Space Complexity: O(log n). The number of function calls on the stack will only be as great
// as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced)
inorder() {
Comment on lines +65 to 68

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
return this.inorderHelper(this.root, []);
}

// Time Complexity: ?
// Space Complexity: ?
inorderHelper(current, inorderList) {
if (current === null) return inorderList;

const leftBranch = this.inorderHelper(current.left, inorderList);
leftBranch.push({key: current.key, value: current.value});
const rightBranch = this.inorderHelper(current.right, leftBranch);
return rightBranch;
}

// Time Complexity: O(n). Method needs to visit every node in the tree
// Space Complexity: O(log n). The number of function calls on the stack will only be as great
// as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced)
preorder() {
Comment on lines +81 to 84

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
return this.preorderHelper(this.root, []);
}

// Time Complexity: ?
// Space Complexity: ?
preorderHelper(current, preorderList) {
if (current === null) return preorderList;

preorderList.push({key: current.key, value: current.value});
const leftBranch = this.preorderHelper(current.left, preorderList);
const rightBranch = this.preorderHelper(current.right, leftBranch);
return rightBranch;
}

// Time Complexity: O(n). Method needs to visit every node in the tree
// Space Complexity: O(log n). The number of function calls on the stack will only be as great
// as the maximum height of the tree, which increases logarithmically to # of nodes (assuming balanced)
postorder() {
Comment on lines +97 to 100

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
return this.postorderHelper(this.root, []);
}

// Time Complexity: ?
// Space Complexity: ?
postorderHelper(current, postorderList) {
if (current === null) return postorderList;

const leftBranch = this.postorderHelper(current.left, postorderList);
const rightBranch = this.postorderHelper(current.right, leftBranch);
postorderList.push({key: current.key, value: current.value});
return postorderList
}

// Time Complexity: O(n). Method needs to follow every branch to its end
// and therefore visits every node.
// Space Complexity: O(log n). The max number of function calls on the stack will
// only be the maximum height of the tree, which scales logarithmically to the
// size of the tree (if balanced)
height() {
Comment on lines +113 to 118

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity, with the assumption

throw new Error("This method hasn't been implemented yet!");
return this.heightHelper(this.root, 0)
}

heightHelper(currentNode, count) {
if (currentNode === null) return count

const rightBranch = this.heightHelper(currentNode.right, count + 1);
const leftBranch = this.heightHelper(currentNode.left, count + 1);

if (rightBranch > leftBranch) {
return rightBranch;
} else {
return leftBranch;
}
}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n). The method needs to visit every node in the tree so the time taken
// will increase linear to the size of the tree increasing.
// Space Complexity: O(n). The size of the queue at any point in time will increase logarithmically
// with the size of the tree, but the results array will increase linear to the size of the tree.
bfs() {
Comment on lines +135 to 139

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Correct on complexity

throw new Error("This method hasn't been implemented yet!");
const queue = [ this.root ];
const result = [];

for (const node of queue) {
if (node) {
result.push({key: node.key, value: node.value});
queue.push(node.left, node.right);
}
}

return result;
}

// Useful for printing
Expand Down