-
Notifications
You must be signed in to change notification settings - Fork 44
Branches - Brianna #36
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
base: master
Are you sure you want to change the base?
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,66 +1,145 @@ | ||
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_helper(current_node, key, value) | ||
return TreeNode.new(key, value) if current_node.nil? | ||
|
||
if key < current_node.key | ||
current_node.left = add_helper(current_node.left, key, value) | ||
else | ||
current_node.right = add_helper(current_node.right, key, value) | ||
end | ||
|
||
return current_node | ||
end | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def find_helper(current_node, target) | ||
return current_node if current_node == nil | ||
return current_node.value if current_node.key == target | ||
|
||
if target < current_node.key | ||
find_helper(current_node.left, target) | ||
else | ||
find_helper(current_node.right, target) | ||
end | ||
|
||
end | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def find(key) | ||
Comment on lines
+49
to
51
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 inorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
inorder_helper(current_node.left, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
inorder_helper(current_node.right, list) | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+65
to
67
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 preorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
list << {key: current_node.key, value: current_node.value} | ||
preorder_helper(current_node.left, list) | ||
preorder_helper(current_node.right, list) | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
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 preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def postorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
postorder_helper(current_node.left, list) | ||
postorder_helper(current_node.right, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+97
to
99
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: | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
|
||
def height_helper(current_node) | ||
Comment on lines
+103
to
+106
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 have to visit each node, the time complexity is going to be O(n). However since the recursive call stack won't be bigger than the height or O(log n) for a balanced tree. |
||
return 0 if current_node.nil? | ||
|
||
left = height_helper(current_node.left) | ||
right = height_helper(current_node.right) | ||
|
||
if left > right | ||
return left += 1 | ||
else | ||
return right += 1 | ||
end | ||
end | ||
|
||
def height | ||
raise NotImplementedError | ||
return 0 if @root.nil? | ||
return height_helper(@root) | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
end | ||
end | ||
|
||
# tree = Tree.new | ||
# tree.add(1, "Brianna") | ||
# tree.add(2, "Brianna") | ||
# tree.add(3, "Brianna") | ||
# # p tree | ||
|
||
# # p tree.inorder | ||
# p tree.find(2) | ||
# p tree.find(3) |
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.
👍