-
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 - Noor #27
base: master
Are you sure you want to change the base?
Fire - Noor #27
Changes from all commits
20d4039
367947a
73947a3
6542adf
2ce75ac
f29b5b2
22d8ce0
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,40 +16,40 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add(key, value = nil) | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def find(key) | ||
Comment on lines
+25
to
27
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/space complexity is only correct for balanced trees. |
||
raise NotImplementedError | ||
find_helper(@root,key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+31
to
33
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 | ||
inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+37
to
39
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: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+43
to
45
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: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height | ||
raise NotImplementedError | ||
height_helper(@root) | ||
end | ||
|
||
# Optional Method | ||
|
@@ -64,3 +64,74 @@ def to_s | |
return "#{self.inorder}" | ||
end | ||
end | ||
|
||
# Helper methods | ||
def add_helper(current, key, value = nil) | ||
if current.nil? | ||
current = TreeNode.new(key, value) | ||
elsif key >= current.key | ||
current.right = add_helper(current.right, key, value) | ||
else | ||
current.left = add_helper(current.left, key, value) | ||
end | ||
|
||
return current | ||
|
||
end | ||
|
||
def find_helper(current, key) | ||
return nil if current.nil? | ||
|
||
if key == current.key | ||
return current.value | ||
elsif key > current.key | ||
return find_helper(current.right, key) | ||
elsif key < current.key | ||
return find_helper(current.left, key) | ||
end | ||
end | ||
|
||
def inorder_helper(node, array) | ||
# Left, Current, Right | ||
return array if node.nil? | ||
|
||
inorder_helper(node.left, array) | ||
array << { | ||
key: node.key, | ||
value: node.value | ||
} | ||
inorder_helper(node.right, array) | ||
end | ||
|
||
def preorder_helper(node, array) | ||
# Current, Left, Right | ||
return array if node.nil? | ||
|
||
array << { | ||
key: node.key, | ||
value: node.value | ||
} | ||
preorder_helper(node.left, array) | ||
preorder_helper(node.right, array) | ||
end | ||
|
||
def postorder_helper(node, array) | ||
# Left, Right, Current | ||
return array if node.nil? | ||
|
||
postorder_helper(node.left, array) | ||
postorder_helper(node.right, array) | ||
array << { | ||
key: node.key, | ||
value: node.value | ||
} | ||
end | ||
|
||
def height_helper(node) | ||
return 0 if node.nil? | ||
|
||
right_count = height_helper(node.right) + 1 | ||
left_count = height_helper(node.left) + 1 | ||
|
||
return [right_count, left_count].max | ||
end |
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/space complexity is only correct for balanced trees.