Skip to content

Space - Nikki V #21

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 94 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,115 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: best case O(log n) / worst case O(n)
# Space Complexity: O(n)
def add_helper(current, new_node)
Comment on lines +19 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , your space complexity is correct if the tree is unbalanced.

return new_node if current.nil?

if new_node.key <= current.key
current.left = add_helper(current.left, new_node)
else
current.right = add_helper(current.right, new_node)
end

return current
end

def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root.nil?
@root = new_node
else
add_helper(@root, new_node)
end

return @root
end

# Time Complexity:
# Space Complexity:
# Time Complexity: best case O(log n) / worst case O(n)
# Space Complexity: O(n)
def find(key)
Comment on lines +45 to 47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , your space complexity is correct if the tree is unbalanced.

raise NotImplementedError
current = @root
return nil if current.nil?

if current.key == key
return current.value
else
if key < current.key
return current.left.value
else
return current.right.value
end
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(current, order)
Comment on lines +62 to +64

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if current.nil?
return
end

inorder_helper(current.left, order) if current.left
order << {key: current.key, value: current.value}
inorder_helper(current.right, order) if current.right
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
order = []
inorder_helper(@root, order)
return order
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(current, order)
Comment on lines +80 to +82

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if current.nil?
return
end

order << {key: current.key, value: current.value}
preorder_helper(current.left, order) if current.left
preorder_helper(current.right, order) if current.right
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
order = []
preorder_helper(@root, order)
return order
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(current, order)
Comment on lines +98 to +100

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if current.nil?
return
end

postorder_helper(current.left, order) if current.left
postorder_helper(current.right, order) if current.right
order << {key: current.key, value: current.value}
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
order = []
postorder_helper(@root, order)
return order
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def height_helper(current)
Comment on lines +116 to +118

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if current.nil?
return 0
else
return 1 + [height_helper(current.left), height_helper(current.right)].max
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
height_helper(@root)
end

# Optional Method
Expand All @@ -64,3 +139,4 @@ def to_s
return "#{self.inorder}"
end
end