-
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 - Ana #24
base: master
Are you sure you want to change the base?
Earth - Ana #24
Changes from all commits
50dd548
7db1a72
8933dc9
fac7235
8f88635
d623573
c6a2f4e
0376864
6f5701b
b7929e1
c606a66
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,54 +2,130 @@ 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: | ||
def add(key, value) | ||
raise NotImplementedError | ||
def add_helper(current_node, new_node) | ||
return new_node if current_node.nil? | ||
|
||
if new_node.key < current_node.key | ||
current_node.left = add_helper(current_node.left, new_node) | ||
elsif new_node.key > current_node.key | ||
current_node.right = add_helper(current_node.right, new_node) | ||
end | ||
|
||
return current_node | ||
end | ||
|
||
# Time Complexity: o(log(n)) | ||
# Space Complexity: o(1) | ||
def add(key, value = nil) | ||
new_node = TreeNode.new(key, value) | ||
if @root.nil? | ||
@root = new_node | ||
else | ||
add_helper(@root, new_node) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current_node,key) | ||
return nil if current_node.nil? | ||
|
||
if current_node.key == key | ||
return current_node.value | ||
elsif key <= current_node.key | ||
find_helper(current_node.left, key) | ||
else key > current_node.key | ||
return find_helper(current_node.right, key) | ||
end | ||
end | ||
|
||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def find(key) | ||
Comment on lines
+56
to
58
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(log n) if the tree is balanced and O(n) if it's unbalanced. The space complexity is the same due to recursion. |
||
raise NotImplementedError | ||
# raise NotImplementedError | ||
if @root.nil? | ||
return nil | ||
else | ||
return find_helper(@root, key) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(current_node, values) | ||
return values if current_node.nil? | ||
|
||
inorder_helper(current_node.left, values) | ||
values.push({key: current_node.key, value: current_node.value}) | ||
inorder_helper(current_node.right, values) | ||
return values | ||
end | ||
|
||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def inorder | ||
Comment on lines
+77
to
79
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 [] if @root.nil? | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(current_node, values) | ||
return values if current_node.nil? | ||
|
||
values.push({key: current_node.key, value: current_node.value}) | ||
preorder_helper(current_node.left, values) | ||
preorder_helper(current_node.right, values) | ||
return values | ||
end | ||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def preorder | ||
Comment on lines
+93
to
95
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 [] if @root.nil? | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(current, values) | ||
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: o(n) | ||
# Space Complexity: o(n) | ||
def postorder | ||
Comment on lines
+109
to
111
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 [] if @root.nil? | ||
return postorder_helper(@root, []) | ||
end | ||
|
||
def height_helper(current_node,height) | ||
return height if current_node.nil? | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
leftmax = height_helper(current_node.left, height +1 ) | ||
rightmax = height_helper(current_node.right, height + 1) | ||
|
||
return [leftmax, rightmax].max | ||
end | ||
|
||
# Time Complexity: o(n) | ||
# Space Complexity: o(n) | ||
def height | ||
Comment on lines
+125
to
127
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 unbalanced due to the recursion. You have the time complexity right. |
||
raise NotImplementedError | ||
return height_helper(@root, 0) | ||
end | ||
|
||
# Optional Method | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
require "minitest" | ||
require "minitest/autorun" | ||
require "minitest/reporters" | ||
|
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(log n) if the tree is balanced and O(n) if it's unbalanced. The space complexity is the same due to recursion.