-
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
Space - Stephanie #37
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -19,44 +19,119 @@ def initialize | |
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
@root.nil? ? @root = new_node : comparative_helper(@root, new_node) | ||
end | ||
|
||
def comparative_helper(current_node, new_node) | ||
return new_node if current_node.nil? | ||
|
||
if new_node.key <= current_node.key | ||
current_node.left = comparative_helper(current_node.left, new_node) | ||
else | ||
current_node.right = comparative_helper(current_node.right, new_node) | ||
end | ||
|
||
return current_node | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
Comment on lines
38
to
40
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. 👍 , But what about time/space complexity. Nice work with the loop solution |
||
raise NotImplementedError | ||
return nil if @root.nil? | ||
return @root.value if @root.key == key | ||
|
||
current_node = @root | ||
|
||
until current_node == nil | ||
return current_node.value if key == current_node.key | ||
current_node = current_node.left if key < current_node.key | ||
current_node = current_node.right if key > current_node.key | ||
end | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
Comment on lines
54
to
56
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. 👍 , But what about time/space complexity. Wow, doing a loop solution for inorder. |
||
raise NotImplementedError | ||
return [] if @root.nil? | ||
ordered_list = [] | ||
holder = [] | ||
current_node = @root | ||
|
||
while !current_node.nil? || !holder.empty? | ||
until current_node.nil? | ||
holder << current_node | ||
current_node = current_node.left | ||
end | ||
|
||
current_node = holder.pop | ||
list_pusher(current_node, ordered_list) | ||
current_node = current_node.right | ||
end | ||
|
||
return ordered_list | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
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. 👍 , But what about time/space complexity. |
||
raise NotImplementedError | ||
return [] if @root.nil? | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
def preorder_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
list_pusher(current_node, list) | ||
preorder_helper(current_node.left, list) | ||
preorder_helper(current_node.right, list) | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
Comment on lines
94
to
96
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. 👍 , Nice work doing the recursive postorder |
||
raise NotImplementedError | ||
return [] if @root.nil? | ||
postorder_helper(@root, []) | ||
end | ||
|
||
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_pusher(current_node, list) | ||
|
||
return list | ||
end | ||
|
||
def list_pusher(current_node, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
Comment on lines
115
to
117
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. 👍 , but time/space complexity? |
||
raise NotImplementedError | ||
return 0 if @root.nil? | ||
height_helper(@root) | ||
end | ||
|
||
def height_helper(current_node) | ||
return 0 if current_node.nil? | ||
left = height_helper(current_node.left) | ||
right = height_helper(current_node.right) | ||
left > right ? (return left + 1) : (return right + 1) | ||
end | ||
|
||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
|
||
end | ||
|
||
# Useful for printing | ||
|
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.
👍 , Time & Space Complexity?