forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Angele Z #41
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
Open
geli-gel
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
geli-gel:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Angele Z #41
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,47 +16,121 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Time Complexity: O(logn) in best case scenario | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current_node, key, value) | ||
return TreeNode.new(key, value) if current_node.nil? | ||
|
||
if key < current_node.key | ||
current_node.left = add_helper(current_node.left, key, value) | ||
else | ||
current_node.right = add_helper(current_node.right, key, value) | ||
end | ||
|
||
return current_node | ||
end | ||
|
||
# Time Complexity: O(logn) since binary | ||
# Space Complexity: O(n) since | ||
def find(key) | ||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current_node, key) | ||
return nil if current_node.nil? | ||
return current_node.value if key == current_node.key | ||
|
||
if key < current_node.key | ||
find_helper(current_node.left, key) | ||
else | ||
find_helper(current_node.right, key) | ||
end | ||
end | ||
|
||
|
||
# Time Complexity: O(n) since going to each node once, backtracking but still backtracking a smaller number of times than there are nodes, so it depends on the number of nodes no matter what. | ||
# Space Complexity: O(n) making an array that is number-of-nodes long | ||
def inorder | ||
raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# helper | ||
def inorder_helper(current_node, list) | ||
# left, root, right | ||
return list if current_node.nil? | ||
# recursion! left side first | ||
inorder_helper(current_node.left, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
inorder_helper(current_node.right, list) | ||
return list | ||
end | ||
|
||
# Time Complexity: same as inorder | ||
# Space Complexity: same as inorder | ||
def preorder | ||
raise NotImplementedError | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current_node, list) | ||
# root, left, right | ||
return list if current_node.nil? | ||
list << {key: current_node.key, value: current_node.value} | ||
preorder_helper(current_node.left, list) | ||
preorder_helper(current_node.right, list) | ||
return list | ||
end | ||
|
||
# Time Complexity: same as inorder | ||
# Space Complexity: same as inorder | ||
def postorder | ||
Comment on lines
+87
to
89
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. Similar issues |
||
raise NotImplementedError | ||
return postorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current_node, list) | ||
# left, right, root | ||
return list if current_node.nil? | ||
postorder_helper(current_node.left, list) | ||
postorder_helper(current_node.right, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) since each node is visited once | ||
# Space Complexity: O(logn) assuming balanced tree since max mem stack being used is height of tree | ||
def height | ||
raise NotImplementedError | ||
return height_helper(@root, 0) | ||
end | ||
|
||
def height_helper(current_node, current_height) | ||
return 0 if current_node.nil? | ||
left_height = height_helper(current_node.left, current_height) | ||
right_height = height_helper(current_node.right, current_height) | ||
if left_height > right_height | ||
return left_height + 1 | ||
else | ||
return right_height + 1 | ||
end | ||
|
||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
list = [] | ||
return list if @root.nil? | ||
queue = [@root] | ||
until queue.empty | ||
current = queue.shift | ||
queue.push(current.left) unless current.left.nil? | ||
queue.push(current.right) unless current.right.nil? | ||
|
||
list << {key: current.key, value: current.value } | ||
end | ||
end | ||
|
||
# Useful for printing | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Same issues as inorder!