-
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
Leaves - Kristina #29
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 | ||||
---|---|---|---|---|---|---|
|
@@ -16,40 +16,118 @@ def initialize | |||||
@root = nil | ||||||
end | ||||||
|
||||||
def add_helper(curr_node, key, value) | ||||||
if curr_node.nil? | ||||||
return TreeNode.new(key, value) | ||||||
else | ||||||
if key < curr_node.key | ||||||
curr_node.left = add_helper(curr_node.left, key, value) | ||||||
else | ||||||
curr_node.right = add_helper(curr_node.right, key, value) | ||||||
end | ||||||
end | ||||||
|
||||||
return curr_node | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def add(key, value) | ||||||
raise NotImplementedError | ||||||
def add(key, value) | ||||||
@root = add_helper(@root, key, value) | ||||||
end | ||||||
|
||||||
def find_helper(curr_node, key) | ||||||
if curr_node.nil? | ||||||
return nil | ||||||
end | ||||||
|
||||||
if curr_node.key == key | ||||||
return curr_node.value | ||||||
end | ||||||
|
||||||
if curr_node.key > key | ||||||
return find_helper(curr_node.left, key) | ||||||
elsif curr_node.key < key | ||||||
return find_helper(curr_node.right, key) | ||||||
end | ||||||
|
||||||
return nil | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def find(key) | ||||||
Comment on lines
57
to
59
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 the space and time complexity? |
||||||
raise NotImplementedError | ||||||
return find_helper(@root, key) | ||||||
end | ||||||
|
||||||
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: | ||||||
# Space Complexity: | ||||||
def inorder | ||||||
raise NotImplementedError | ||||||
def inorder(node,) | ||||||
Comment on lines
73
to
+75
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 the space and time complexity? 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.
Suggested change
|
||||||
list = [] | ||||||
inorder_helper(@root, list) | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def preorder | ||||||
raise NotImplementedError | ||||||
def preorder_helper(current_node, list) | ||||||
Comment on lines
80
to
+82
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 the space and time complexity? |
||||||
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 | ||||||
|
||||||
def pre_order | ||||||
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.
Suggested change
|
||||||
list = [] | ||||||
return preorder_helper(@root, list) | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def postorder_helper(current_node, list) | ||||||
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. 👍 , but the space and time complexity? |
||||||
return [] 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 | ||||||
|
||||||
def postorder | ||||||
raise NotImplementedError | ||||||
list = [] | ||||||
return postorder_helper(@root, list) | ||||||
end | ||||||
|
||||||
def height_helper(current_node) | ||||||
return 0 if current_node.nil? | ||||||
|
||||||
left_height = height_helper(current_node.left) | ||||||
right_height = height_helper(current_node.right) | ||||||
|
||||||
if left_height > right_height | ||||||
return left_height + 1 | ||||||
else | ||||||
return right_height + 1 | ||||||
end | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def height | ||||||
raise NotImplementedError | ||||||
height = 0 | ||||||
return height_helper(@root) | ||||||
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.
👍 , but the space and time complexity?