-
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 - Julia Bouvier #37
base: master
Are you sure you want to change the base?
Changes from all commits
7f097f5
31da491
631067e
77cde87
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 |
---|---|---|
|
@@ -16,40 +16,158 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: log(n) | ||
# Space Complexity: log(n) | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
|
||
if @root.nil? | ||
@root = new_node | ||
else | ||
add_helper(@root, new_node, key) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current, new_node, key) | ||
return new_node if current.nil? | ||
|
||
if key <= current.key | ||
current.left = add_helper(current.left, new_node, key) | ||
else | ||
current.right = add_helper(current.right, new_node, key) | ||
end | ||
return current | ||
end | ||
|
||
|
||
# Time Complexity: log(n) | ||
# Space Complexity: log(n) | ||
def find(key) | ||
Comment on lines
+43
to
45
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 | ||
if @root.nil? | ||
return nil | ||
else | ||
find_helper(key, @root) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(key, current) | ||
return nil if current.nil? | ||
if current.key == key | ||
return current.value | ||
elsif key > current.key | ||
find_helper(key, current.right) | ||
else | ||
find_helper(key, current.left) | ||
end | ||
end | ||
|
||
def find_node(key) | ||
if @root.nil? | ||
return nil | ||
else | ||
find_node_helper(key, @root) | ||
end | ||
end | ||
|
||
def find_node_helper(key, current) | ||
return nil if current.nil? | ||
if current.key == key | ||
return current | ||
elsif key > current.key | ||
find_node_helper(key, current.right) | ||
else | ||
find_node_helper(key, current.left) | ||
end | ||
end | ||
|
||
def delete(key) | ||
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. Just noting this isn't working. |
||
node = find_node(key) | ||
unless node.nil? | ||
remove(node) | ||
end | ||
end | ||
|
||
def remove(node) | ||
if node.left.nil? && node.right.nil? | ||
node = nil | ||
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. This only changes the local variable (node) reference. |
||
elsif !node.left.nil? && node.right.nil? | ||
node = node.left | ||
elsif node.left.nil? && !node.right.nil? | ||
node = node.right | ||
puts node.value | ||
else | ||
min_node = find_min_node(node.right) | ||
node = min_node | ||
min_node = nil | ||
end | ||
end | ||
|
||
def find_min_node(node) | ||
if node.left.nil? | ||
min_node = node | ||
return min_node | ||
else | ||
find_min_node(node.left) | ||
end | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+114
to
116
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 inorder_helper(current, list) | ||
return list if current.nil? | ||
|
||
inorder_helper(current.left, list) | ||
list << { key: current.key, value: current.value} | ||
inorder_helper(current.right, list) | ||
|
||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
Comment on lines
+130
to
132
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 preorder_helper(node, list) | ||
return list if node.nil? | ||
|
||
list << { key: node.key, value: node.value} | ||
preorder_helper(node.left, list) | ||
preorder_helper(node.right, list) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+144
to
146
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: | ||
def postorder_helper(node, list) | ||
return list if node.nil? | ||
|
||
postorder_helper(node.left, list) | ||
postorder_helper(node.right, list) | ||
list << { key: node.key, value: node.value} | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) | ||
def height | ||
Comment on lines
+158
to
160
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. This method isn't actually working. See my note below. Think about this: If the node is Return the height of the bigger subtree and add one (for the current node). |
||
raise NotImplementedError | ||
return height_helper(@root) | ||
end | ||
|
||
def height_helper(current) | ||
return 0 if current.nil? | ||
|
||
left_height = height_helper(current.left) | ||
right_height = height_helper(current.right) | ||
|
||
return [left_height, right_height].max + 1 | ||
end | ||
|
||
# Optional Method | ||
|
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.
👍