-
Notifications
You must be signed in to change notification settings - Fork 44
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 - Denise - Tree Practice #26
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 |
---|---|---|
|
@@ -16,47 +16,154 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: log(n) | ||
# Space Complexity: log(n) | ||
|
||
def add_helper(key, value, current) | ||
|
||
if current.key == key | ||
current.value = value | ||
elsif current.key > key | ||
if current.left == nil | ||
current.left = TreeNode.new(key,value) | ||
else | ||
add_helper(key, value, current.left) | ||
end | ||
else | ||
if current.right == nil | ||
current.right = TreeNode.new(key, value) | ||
else | ||
add_helper(key, value, current.right) | ||
end | ||
end | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value = nil) | ||
if @root.nil? | ||
@root = TreeNode.new(key,value) | ||
return @root | ||
else | ||
add_helper(key, value, @root) | ||
end | ||
|
||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find(key) | ||
Comment on lines
+52
to
54
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. 👍 The time complexity is right if the tree is balanced and O(n) if not. The space complexity (due to the call stack) is the same as the time complexity. |
||
raise NotImplementedError | ||
if @root.nil? | ||
return nil | ||
else | ||
return find_helper(key, @root) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
raise NotImplementedError | ||
def find_helper(key, current) | ||
return nil if current.nil? | ||
if current.key == key | ||
return current.value | ||
elsif current.key > key | ||
find_helper(key, current.left) | ||
else | ||
find_helper(key, current.right) | ||
end | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def inorder(current = @root, values = []) | ||
Comment on lines
+73
to
+76
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 current.nil? | ||
return values | ||
end | ||
|
||
inorder(current.left, values) | ||
values.push({key: current.key, value: current.value}) | ||
inorder(current.right, values) | ||
return values | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def preorder_helper(current, values) | ||
Comment on lines
+87
to
+90
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. 👍 |
||
return values if current.nil? | ||
|
||
values.push({key: current.key, value: current.value}) | ||
preorder_helper(current.left, values) | ||
preorder_helper(current.right, values) | ||
|
||
return values | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def preorder | ||
raise NotImplementedError | ||
current = @root | ||
values = [] | ||
return preorder_helper(current, values) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def postorder_helper(current, values) | ||
Comment on lines
+107
to
+110
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. 👍 |
||
return values if current.nil? | ||
|
||
postorder_helper(current.left, values) | ||
postorder_helper(current.right, values) | ||
values.push({key: current.key, value: current.value}) | ||
|
||
return values | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
raise NotImplementedError | ||
current = @root | ||
values = [] | ||
return postorder_helper(current, values) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
# Time Complexity: o(n) | ||
# Space Complexity: O(h), where h = height | ||
def height(current = @root) | ||
Comment on lines
+126
to
+128
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. 👍 Nice work on the space/time complexity! |
||
return 0 if current.nil? | ||
|
||
left_child = height(current.left) | ||
right_child = height(current.right) | ||
|
||
if left_child > right_child | ||
return left_child + 1 | ||
else | ||
return right_child + 1 | ||
end | ||
|
||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) if tree is not balanced; O(log(n)) if it's balanced | ||
# Space Complexity: o(n) | ||
def bfs | ||
raise NotImplementedError | ||
values = [] | ||
h = height | ||
i = 1 | ||
while i <= h | ||
bfs_helper(@root, i, values) | ||
i += 1 | ||
end | ||
|
||
return values | ||
end | ||
|
||
def bfs_helper(current, level, values) | ||
return values if current.nil? | ||
|
||
if level == 1 | ||
values.push({key: current.key, value: current.value}) | ||
elsif level > 1 | ||
bfs_helper(current.left, level - 1, values) | ||
bfs_helper(current.right, level - 1, values) | ||
end | ||
return values | ||
end | ||
|
||
# Useful for printing | ||
|
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.
👍 The space/time complexity is right if the tree is balanced and O(n) if not.