-
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
Kristy, Branches #35
base: master
Are you sure you want to change the base?
Kristy, Branches #35
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 |
---|---|---|
@@ -1,64 +1,124 @@ | ||
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(n) | ||
# Space Complexity: O(n) | ||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current, key, value) | ||
return TreeNode.new(key, value) if current.nil? | ||
if key < current.key | ||
current.left = add_helper(current.left, key, value) | ||
else | ||
current.right = add_helper(current.right, key, value) | ||
end | ||
return current | ||
end | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def find(key) | ||
Comment on lines
+35
to
37
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 find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current, key) | ||
return nil if current.nil? | ||
return current.value if current.key == key | ||
if key > current.key | ||
return find_helper(current.right, key) | ||
else | ||
return find_helper(current.left, key) | ||
end | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+51
to
53
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, list) | ||
return list if current.nil? | ||
|
||
inorder_helper(current.left, list) | ||
list << { key: current.key, value: current.value } | ||
inorder_helper(current.right, list) | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+67
to
69
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 preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current, list) | ||
return list if current.nil? | ||
list << {key: current.key, value: current.value} | ||
preorder_helper(current.left, list) | ||
preorder_helper(current.right, list) | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+81
to
83
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 postorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current, list) | ||
return list if current.nil? | ||
postorder_helper(current.left, list) | ||
postorder_helper(current.right, list) | ||
list << {key: current.key, value: current.value} | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height | ||
Comment on lines
+95
to
97
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. Since you're visiting each node, the time complexity is right, but the space complexity is O(log n) for a balanced tree since the call stack never gets greater than height of the tree. |
||
raise NotImplementedError | ||
return height_helper(@root, 0, 1) | ||
end | ||
|
||
def height_helper(current, max, count) | ||
return max if current.nil? | ||
|
||
max = count if count > max | ||
height_helper(current.left, max, (count + 1)) | ||
height_helper(current.right, max, (count + 1)) | ||
|
||
end | ||
|
||
# def delete(key) | ||
# return nil if self.find(key).nil? | ||
|
||
# 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/time complexity is O(log n) if the tree is balanced and O(n) if it's not.