-
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
Branches - Erika #45
base: master
Are you sure you want to change the base?
Branches - Erika #45
Changes from all commits
c72dda7
e00ae25
d5cdd1b
34ec8e8
30d69ee
3290df4
ddc5b96
7ca98a0
d989611
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 |
---|---|---|
|
@@ -8,6 +8,87 @@ def initialize(key, val) | |
@left = nil | ||
@right = nil | ||
end | ||
|
||
def add(key, value) | ||
|
||
if key <= @key | ||
return @left = TreeNode.new(key, value) if @left.nil? | ||
@left.add(key, value) | ||
else | ||
return @right = TreeNode.new(key, value) if @right.nil? | ||
@right.add(key, value) | ||
end | ||
|
||
end | ||
|
||
def find(key) | ||
|
||
return @value if @key == key | ||
|
||
if key > @key | ||
return nil if @right.nil? | ||
@right.find(key) | ||
else | ||
return nil if @left.nil? | ||
@left.find(key) | ||
end | ||
|
||
end | ||
|
||
def inorder | ||
|
||
inorder_array = [] | ||
|
||
inorder_array += @left.inorder unless @left.nil? | ||
inorder_array << { key: @key, value: @value } | ||
inorder_array += @right.inorder unless @right.nil? | ||
|
||
return inorder_array | ||
|
||
end | ||
|
||
def preorder | ||
|
||
preorder_array = [] | ||
|
||
preorder_array << { key: @key, value: @value } | ||
preorder_array += @left.preorder unless left.nil? | ||
preorder_array += @right.preorder unless right.nil? | ||
|
||
|
||
return preorder_array | ||
|
||
end | ||
|
||
def postorder | ||
|
||
postorder_array = [] | ||
|
||
postorder_array += @left.postorder unless left.nil? | ||
postorder_array += @right.postorder unless right.nil? | ||
postorder_array << { key: @key, value: @value } | ||
|
||
return postorder_array | ||
|
||
end | ||
|
||
def height | ||
|
||
return 1 if @left.nil? && @right.nil? | ||
|
||
if @left.nil? | ||
@right.height + 1 | ||
elsif @right.nil? | ||
@left.height + 1 | ||
else | ||
if @left.height >= @right.height | ||
@left.height + 1 | ||
else | ||
@right.height + 1 | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
class Tree | ||
|
@@ -16,51 +97,74 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def add(key, value) | ||
Comment on lines
+100
to
102
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. 👍 , time complexity is right if the tree is balanced. |
||
raise NotImplementedError | ||
|
||
if @root.nil? | ||
return @root = TreeNode.new(key, value) | ||
end | ||
|
||
return @root.add(key, value) | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O (log n) | ||
# Space Complexity: O(1) | ||
def find(key) | ||
Comment on lines
+113
to
115
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. 👍 , time complexity is right if the tree is balanced. |
||
raise NotImplementedError | ||
|
||
return nil if @root.nil? | ||
return @root.find(key) | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+122
to
124
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 @root.inorder | ||
|
||
end | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+132
to
134
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 @root.preorder | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+141
to
143
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 @root.postorder | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def height | ||
Comment on lines
+150
to
152
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. 👍 , time & space complexity? |
||
raise NotImplementedError | ||
end | ||
|
||
return 0 if @root.nil? | ||
return @root.height | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
end | ||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# def bfs | ||
# raise NotImplementedError | ||
# end | ||
|
||
# # Useful for printing | ||
# def to_s | ||
# return "#{self.inorder}" | ||
# end | ||
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.
Nice adding these methods to the TreeNode class.