Skip to content

Time - Yesenia #39

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 1 commit 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
121 changes: 103 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,127 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(key, value)
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.

👍

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

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

# Time Complexity:
# Space Complexity:
def add_helper(current, new_node)

Choose a reason for hiding this comment

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

Nice helper method.

return new_node if current.nil? # base case

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

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(key)
Comment on lines +43 to 45

Choose a reason for hiding this comment

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

👍 , however since you're doing this with a loop instead of recursively you have a space complexity of O(1)

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

until current.nil?
if key == current.key
return current.value
elsif key < current.key
current = current.left
elsif key > current.key
current = current.right
end
end

return nil # case when key not found
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +63 to 65

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
root = @root
list = []

inorder_helper(root, list)

return list
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, list)
return 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)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +82 to 84

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
root = @root
list = []

preorder_helper(root, list)

return list
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, list)
return 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)
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +101 to 103

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
root = @root
list = []

postorder_helper(root, list)

return list
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, list)
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}
end

# Time Complexity: O(n)
# Space Complexity: O(log n) - balanced
def height
Comment on lines +120 to 122

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @root
return height_helper(current, 0)
end

def height_helper(current_node, height_count)
return 0 if current_node.nil?

left_size = height_helper(current_node.left, height_count+1)
right_size = height_helper(current_node.right, height_count+1)

if left_size >= right_size
return left_size + 1
else
return right_size + 1
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down