-
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
Fire - blaine #28
base: master
Are you sure you want to change the base?
Fire - blaine #28
Changes from 3 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 |
---|---|---|
|
@@ -2,12 +2,12 @@ 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 | ||
|
@@ -16,42 +16,132 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: O(log n) | ||
# Space Complexity: [O(1)-if it's a loop] - why do I wanna say O(n)?? help me. | ||
# Try with a while loop | ||
def add(key, value = nil) | ||
if @root.nil? | ||
@root = TreeNode.new(key, value) | ||
else | ||
add_helper(@root, key, value) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current, key, value) | ||
# if current = return TreeNode.new | ||
if key < current.key | ||
# if current.left is nil place the new node | ||
# otherwise make current.left the new current and run through the same checks | ||
if current.left.nil? | ||
current.left = TreeNode.new(key, value) | ||
return | ||
else | ||
add_helper(current.left, key, value) | ||
end | ||
else | ||
if current.right.nil? | ||
current.right = TreeNode.new(key, value) | ||
return | ||
else | ||
add_helper(current.right, key, value) | ||
end | ||
end | ||
end | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find(key) | ||
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. 👍 Ditto on space/time complexity |
||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current, key) | ||
if current.nil? | ||
return nil | ||
end | ||
if current.key == key | ||
return current.value | ||
end | ||
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) | ||
# left-root-right | ||
def inorder | ||
Comment on lines
+71
to
74
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 | ||
# current = @root | ||
inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(current, values) # Values is an array | ||
if current.nil? | ||
return values | ||
end | ||
inorder_helper(current.left, values) | ||
values.push({:key=>current.key, :value=>current.value}) | ||
inorder_helper(current.right, values) | ||
return values | ||
end | ||
|
||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
# root-left-right | ||
def preorder | ||
Comment on lines
+90
to
93
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 | ||
preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current, values) | ||
if current.nil? | ||
return values | ||
end | ||
values.push({:key=>current.key, :value=>current.value}) | ||
preorder_helper(current.left, values) | ||
preorder_helper(current.right, values) | ||
return values | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
# left-right-root | ||
def postorder | ||
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. 👍 |
||
raise NotImplementedError | ||
postorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current, values) | ||
if current.nil? | ||
return values | ||
end | ||
postorder_helper(current.left, values) | ||
postorder_helper(current.right, values) | ||
values.push({:key=>current.key, :value=>current.value}) | ||
return values | ||
end | ||
|
||
# Time Complexity: O(n)^2 | ||
# Space Complexity: O(n) | ||
def height | ||
Comment on lines
+124
to
126
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 O(n) (not n^2) The space complexity is O(log n) if the tree is balanced or O(n) if it's not. The space complexity is basically the height of the tree. |
||
raise NotImplementedError | ||
# raise NotImplementedError | ||
height_helper(@root) | ||
end | ||
def height_helper(current) | ||
if current.nil? | ||
return 0 | ||
end | ||
left_height = height_helper(current.left) | ||
right_height = height_helper(current.right) | ||
if left_height > right_height | ||
return left_height + 1 | ||
else | ||
return right_height + 1 | ||
end | ||
end | ||
|
||
|
||
# ********************************************************************** | ||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
|
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 time complexity is O(n) if the tree is balanced and O(log n) if it is balanced.
Since you're doing recursion in
add_helper
the space complexity is the same as the time.