-
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
Leaves - Samantha #28
base: master
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
@@ -1,64 +1,126 @@ | ||
class TreeNode | ||
attr_reader :key, :value | ||
attr_accessor :left, :right | ||
|
||
def initialize(key, val) | ||
def initialize(key, val) | ||
@key = key | ||
@value = val | ||
@left = nil | ||
@right = nil | ||
end | ||
end | ||
end | ||
|
||
class Tree | ||
attr_reader :root | ||
def initialize | ||
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
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 | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find_helper(current_node, key) | ||
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 space complexity is O(log n) if the tree is balanced and O(n) if it's not. 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. Fixed. |
||
return nil if current_node.nil? | ||
|
||
if key > current_node.key | ||
current_node.right = find_helper(current_node.right, key) | ||
elsif key < current_node.key | ||
current_node.left = find_helper(current_node.left, key) | ||
else | ||
return current_node.value | ||
end | ||
end | ||
|
||
def find(key) | ||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
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. 👍 |
||
raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def inorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
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: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
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. 👍 |
||
raise NotImplementedError | ||
return pre_order_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def pre_order_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
list << {key: current_node.key, value: current_node.value} | ||
pre_order_helper(current_node.left, list) | ||
pre_order_helper(current_node.right, list) | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def 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. 👍 |
||
raise NotImplementedError | ||
return post_order_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def post_order_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
post_order_helper(current_node.left, list) | ||
post_order_helper(current_node.right, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height_helper(current_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. The space complexity here depends on the recursive calls which depend on the height of the tree. So the space complexity is O(log n) for balanced trees and O(n) for unbalanced ones. Otherwise the method is quite well done! 😃 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. Fixed. |
||
return 0 if current_node.nil? | ||
|
||
if height_helper(current_node.right) > height_helper(current_node.left) | ||
return height_helper(current_node.right) + 1 | ||
else | ||
return height_helper(current_node.left) + 1 | ||
end | ||
end | ||
|
||
def height | ||
raise NotImplementedError | ||
return height_helper(@root) | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
|
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 complexity is O(log n) if the tree is balanced and O(n) if it's not.
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.
Fixed.