-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
} | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨Nice iterative solution!