-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Katie #26
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?
Space - Katie #26
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 |
---|---|---|
|
@@ -19,48 +19,139 @@ def initialize | |
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
if @root == nil | ||
@root = new_node | ||
return new_node | ||
else | ||
add_helper(@root, new_node) | ||
end | ||
end | ||
|
||
def add_helper(current, new_node) | ||
if new_node.key <= current.key | ||
if current.left == nil | ||
return current.left = new_node | ||
else | ||
add_helper(current.left, new_node) | ||
end | ||
else | ||
if current.right == nil | ||
return current.right = new_node | ||
else | ||
add_helper(current.right, new_node) | ||
end | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
Comment on lines
47
to
49
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 | ||
if @root == nil | ||
return nil | ||
elsif @root.key == key | ||
return @root.value | ||
else | ||
current_node = @root | ||
while current_node != nil | ||
if key == current_node.key | ||
return current_node.value | ||
elsif key <= current_node.key | ||
current_node = current_node.left | ||
else | ||
current_node = current_node.right | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
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 | ||
ordered_array = [] | ||
if @root != nil | ||
inorder_helper(@root, ordered_array) | ||
end | ||
return ordered_array | ||
end | ||
|
||
def inorder_helper(node, array) | ||
return if node.nil? | ||
inorder_helper(node.left, array) | ||
array.push({"key": node.key, "value": node.value}) | ||
inorder_helper(node.right, array) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
Comment on lines
87
to
89
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 | ||
preordred_array = [] | ||
if @root != nil | ||
preorder_helper(@root, preordred_array) | ||
end | ||
return preordred_array | ||
end | ||
|
||
def preorder_helper(node, array) | ||
return if node.nil? | ||
array << {key: node.key, value: node.value} | ||
|
||
preorder_helper(node.left, array) | ||
preorder_helper(node.right, array) | ||
end | ||
|
||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
Comment on lines
108
to
110
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 | ||
preordred_array = [] | ||
if @root != nil | ||
postorder_helper(@root, preordred_array) | ||
end | ||
return preordred_array | ||
end | ||
|
||
def postorder_helper(node, array) | ||
return if node.nil? | ||
|
||
postorder_helper(node.left, array) | ||
postorder_helper(node.right, array) | ||
array << {key: node.key, value: node.value} | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
Comment on lines
126
to
128
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 | ||
count = 0 | ||
if @root != nil | ||
count = height_helper(@root) | ||
end | ||
return count | ||
end | ||
|
||
def height_helper(root) | ||
return 0 if root.nil? | ||
|
||
return find_max(height_helper(root.left), height_helper(root.right)) + 1 | ||
end | ||
|
||
def find_max(a,b) | ||
a >= b ? a : b | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
# raise NotImplementedError | ||
end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
# 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.
👍 , but time & space complexity?