Skip to content
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 - Bri #47

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
119 changes: 99 additions & 20 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,126 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add_helper(node, key, value)

Choose a reason for hiding this comment

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

👍

if node.nil?
node = TreeNode.new(key, value)
elsif key < node.key
node.left = add_helper(node.left, key, value)
elsif key > node.key
node.right = add_helper(node.right, key, value)
end
return node
end

# Time Complexity: O(1)
# Space Complexity: O(1)
def add(key, value)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

Since add_helper has a time/space complexity of O(log n), this method does as well.

raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def find(key)
Comment on lines +36 to 38

Choose a reason for hiding this comment

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

Remember that Binary search trees do binary search with a find, so it's O(log n) for time/space.

raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(node, key)
if node.nil?
return nil
elsif node.key == key
return node.value
elsif key < node.key
return find_helper(node.left, key)
elsif key > node.key
return find_helper(node.right, key)
end
end

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

Choose a reason for hiding this comment

The 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(node, list) # list is a reference, so can pass around
if node.nil?
return list
end

inorder_helper(node.left, list)
list << {key: node.key, value: node.value}
inorder_helper(node.right, list)
end

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

Choose a reason for hiding this comment

The 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) # list is a reference, so can pass around
if node.nil?
return list
end

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 +87 to 89

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return postorder_helper(@root, [])
end

def postorder_helper(node, list) # list is a reference, so can pass around
if node.nil?
return list
end

postorder_helper(node.left, list)
postorder_helper(node.right, list)
list << {key: node.key, value: node.value}
end

# Time Complexity:
# Space Complexity:
def height

Choose a reason for hiding this comment

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

👍 But no guesses on time/space compexity?

raise NotImplementedError
return height_helper(@root)
end

def height_helper(node)
if node.nil?
return 0
end
left_length = height_helper(node.left)
right_length = height_helper(node.right)

if left_length > right_length
longest_length = left_length
else
longest_length = right_length
end
longest_length += 1 # Add one for the current node
return longest_length

end

# Optional Method
# Time Complexity:
# Space Complexity:
# Space Complexity:
def bfs

Choose a reason for hiding this comment

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

Looks like you almost have this.

raise NotImplementedError
list = []
return list if @root.nil?
queue = [@root]

until queue.empty
current = queue.shift
queue.push(current.left) unless current.left.nil?
queue.push(current.right) unless current.right.nil?

# list << { key: current.key, value: current.value }
end
end

# Useful for printing
Expand Down