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

C16 - Cedar - Afina Walton #1

Open
wants to merge 3 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
118 changes: 99 additions & 19 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,120 @@ class Tree {
this.root = null;
}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n)
// Space Complexity: O(1)
add(key, value) {
throw new Error("This method hasn't been implemented yet!");
let newNode = new TreeNode(key, value);
if (!this.root) {
this.root = newNode;
return;
}

let current = this.root;
while (current) {
if (newNode.key < current.key) {
if (!current.left) {
current.left = newNode;
break;
}
else { current = current.left }
} else if (newNode.key > current.key) {
if (!current.right) {
current.right = newNode;
break;
}
else { current = current.right }
}
}

return;

Choose a reason for hiding this comment

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

✨Nice iterative solution!


}

// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n)
// Space Complexity: O(1)
find(key) {
throw new Error("This method hasn't been implemented yet!");
// compare keys

Choose a reason for hiding this comment

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

if (!this.root) return null
let current = this.root;
while (current) {
if (key === current.key) return current.value;
else if (key < current.key) {
if (!current.left) return null

Choose a reason for hiding this comment

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

Is this necessary?

current = current.left;
} else if (key > current.key) {
if (!current.right) return null

Choose a reason for hiding this comment

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

Same goes here - do you need this line? Think about why you might not need it! 🤠

current = current.right;
}
}
}

// Time Complexity: ?
// Space Complexity: ?
createNodeObject(node) {

Choose a reason for hiding this comment

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

🧡 Love this helper function

return { key: node.key, value: node.value }
}

inorderHelper(node, arr) {
if (!node) return;
this.inorderHelper(node.left, arr);
arr.push(this.createNodeObject(node));
this.inorderHelper(node.right, arr);
}

// Time Complexity: O(n)
// Space Complexity: O(n)
inorder() {
throw new Error("This method hasn't been implemented yet!");

Choose a reason for hiding this comment

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

let arr = [];
if (!this.root) return arr;

this.inorderHelper(this.root, arr);

return arr

}

// Time Complexity: ?
// Space Complexity: ?
preorderHelper(node, arr) {
if (!node) return;
arr.push(this.createNodeObject(node));
this.preorderHelper(node.left, arr);
this.preorderHelper(node.right, arr);
}

// Time Complexity: O(n)
// Space Complexity: O(n)
preorder() {
throw new Error("This method hasn't been implemented yet!");

Choose a reason for hiding this comment

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

let arr = [];
if (!this.root) return arr;

this.preorderHelper(this.root, arr);

return arr
}

// Time Complexity: ?
// Space Complexity: ?
postorderHelper(node, arr) {
if (!node) return;
this.postorderHelper(node.left, arr);
this.postorderHelper(node.right, arr)
arr.push(this.createNodeObject(node))
}

// Time Complexity: O(n)
// Space Complexity: O(n)
postorder() {

Choose a reason for hiding this comment

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

throw new Error("This method hasn't been implemented yet!");
let arr = [];
if (!this.root) return arr;

this.postorderHelper(this.root, arr);

return arr;
}

// Time Complexity: ?
// Space Complexity: ?
height() {
throw new Error("This method hasn't been implemented yet!");
// Time Complexity: O(n)
// Space Complexity: O(1)
height(root = this.root) {

Choose a reason for hiding this comment

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

✨Awesome work!

if (!root) return 0;

return 1 + Math.max(this.height(root.left), this.height(root.right));

}

// Optional Method
Expand Down