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

Earth - Beauttie #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
149 changes: 127 additions & 22 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,152 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n) if balanced; O(n) if not
# Space Complexity: O(log n) if balanced; O(n) if not
def add(key, value = nil)
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.

👍 Correct on time/space given the assumptions.

if @root.nil?
@root = TreeNode.new(key, value)
return
end

add_helper(@root, key, value)
end

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) if balanced; O(n) if not
# Space Complexity: O(log n) if balanced; O(n) if not
def find(key)
Comment on lines +48 to 50

Choose a reason for hiding this comment

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

👍 Correct on time/space given the assumptions.

raise NotImplementedError
find_helper(@root, key)
end

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

# Time Complexity: visit all nodes => O(n)
# Space Complexity: array contains all nodes => O(n)
def inorder
Comment on lines +66 to 68

Choose a reason for hiding this comment

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

👍 Correct on time/space given the assumptions.

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

def inorder_helper(node, traversal)
return traversal if node.nil?

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
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.

👍 Correct on time/space given the assumptions.

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

def preorder_helper(node, traversal)
return traversal if node.nil?

traversal << {key: node.key, value: node.value}
preorder_helper(node.left, traversal)
preorder_helper(node.right, traversal)
end

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

Choose a reason for hiding this comment

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

👍 Correct on time/space given the assumptions.

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

# Time Complexity:
# Space Complexity:
def postorder_helper(node, traversal)
return traversal if node.nil?

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

# Time Complexity: visit all nodes => O(n)
# Space Complexity: O(log n) if balanced; O(n) if not
def height
Comment on lines +108 to 110

Choose a reason for hiding this comment

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

👍 Correct on time/space given the assumptions.

raise NotImplementedError
return height_helper(@root)
end

def height_helper(node)
return 0 if node.nil?

return 1 + [height_helper(node.left), height_helper(node.right)].max
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n^2)
# Space Complexity: O(n)
# def bfs
# traversal = []
# h = self.height
# i = 1
# while i <= h
# bfs_helper(@root, traversal, i)
# i += 1
# end

# return traversal
# end

# def bfs_helper(node, traversal, level)
# return traversal if node.nil?

# if level == 1
# traversal << {key: node.key, value: node.value}
# return traversal
# elsif level > 1
# bfs_helper(node.left, traversal, level - 1)
# bfs_helper(node.right, traversal, level - 1)
# end
# end

def bfs
raise NotImplementedError
traversal = []
return traversal if @root.nil?

bfs_helper(@root, traversal)
end

def bfs_helper(node, traversal)
queue = Queue.new
queue.enq(node)
while !queue.empty?
current_node = queue.deq(node)
traversal << {key: current_node.key, value: current_node.value}
queue.enq(current_node.left) if current_node.left
queue.enq(current_node.right) if current_node.right
end

return traversal
end

# Useful for printing
Expand Down
2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
end

it "will report the height for a balanced tree" do
expect(tree_with_nodes.height).must_equal 3
expect(tree_with_nodes.height).must_equal 4
end

it "will report the height for unbalanced trees" do
Expand Down