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

Ida - Fire - Binary Search Trees #35

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

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: o(n) or (log n)?
# Space Complexity: o(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.

👍 This works, the time complexity and space complexity are O(log n) if the tree is balanced and O(n) if it's not.

node = TreeNode.new(key, value)
current_node = root
if root.nil?
@root = node
else
# if true, new node is going to right of root
while current_node
if key > current_node.key
if current_node.right.nil?
current_node.right = node
return
end
current_node = current_node.right
else
# new node is going to the left of root
if current_node.left.nil?
current_node.left = node
return
end
current_node = current_node.left
end
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: o(n) or (log n)?
# Space Complexity: o(n)
def find(key)
Comment on lines +47 to 49

Choose a reason for hiding this comment

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

👍 See the add method for time/space complexity.

raise NotImplementedError
current_node = @root
return nil if current_node == nil

while current_node != nil
if key < current_node.key
current_node = current_node.left
elsif key > current_node.key
current_node = current_node.right
else
return current_node.value
end
end

return nil

end

# Time Complexity:
# Space Complexity:
# Time Complexity: o(n)
# Space Complexity: o(n)
def inorder
Comment on lines +67 to 69

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return [] if @root.nil?
return inorder_helper(@root)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(node)
tree = []
tree += inorder_helper(node.left) if node.left
tree << {key: node.key, value: node.value}
tree += inorder_helper(node.right) if node.right

return tree

end
# Time Complexity: o(n)
# Space Complexity: o(n)
def preorder
Comment on lines +83 to 85

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return [] if @root.nil?
return preorder_helper(@root)

end

# Time Complexity:
# Space Complexity:
def preorder_helper(node)
tree = []
tree << {key: node.key, value: node.value}
tree += preorder_helper(node.left) if node.left
tree += preorder_helper(node.right) if node.right
return tree
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return [] if @root.nil?
return postorder_helper(@root)
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
def postorder_helper(node)
tree = []
tree += postorder_helper(node.left) if node.left
tree += postorder_helper(node.right) if node.right
tree << {key: node.key, value: node.value}

return tree
end

# Time Complexity: O(n)
# Space Complexity: o(n)
def height # somewhere between log_2(N+1) and N
Comment on lines +115 to +117

Choose a reason for hiding this comment

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

👍

return 0 if @root.nil?
return height_helper(@root)
end

def height_helper(node)
return 0 if node.nil?
count = 1
left_height = height_helper(node.left)
right_height = height_helper(node.right)
count += [right_height,left_height].max
return count
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
return [] if @root.nil?
# raise NotImplementedError
end

# Useful for printing
Expand Down