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 - Denise - Tree Practice #26

Open
wants to merge 2 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
155 changes: 131 additions & 24 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,154 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: log(n)
# Space Complexity: log(n)

def add_helper(key, value, current)
Comment on lines +19 to +22

Choose a reason for hiding this comment

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

👍 The space/time complexity is right if the tree is balanced and O(n) if not.


if current.key == key
current.value = value
elsif current.key > key
if current.left == nil
current.left = TreeNode.new(key,value)
else
add_helper(key, value, current.left)
end
else
if current.right == nil
current.right = TreeNode.new(key, value)
else
add_helper(key, value, current.right)
end
end

end

# Time Complexity:
# Space Complexity:
def add(key, value = nil)
if @root.nil?
@root = TreeNode.new(key,value)
return @root
else
add_helper(key, value, @root)
end

end

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

Choose a reason for hiding this comment

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

👍 The time complexity is right if the tree is balanced and O(n) if not. The space complexity (due to the call stack) is the same as the time complexity.

raise NotImplementedError
if @root.nil?
return nil
else
return find_helper(key, @root)
end
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
def find_helper(key, current)
return nil if current.nil?
if current.key == key
return current.value
elsif current.key > key
find_helper(key, current.left)
else
find_helper(key, current.right)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)

def inorder(current = @root, values = [])
Comment on lines +73 to +76

Choose a reason for hiding this comment

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

👍

if current.nil?
return values
end

inorder(current.left, values)
values.push({key: current.key, value: current.value})
inorder(current.right, values)
return values
end

# Time Complexity: O(n)
# Space Complexity: O(n)

def preorder_helper(current, values)
Comment on lines +87 to +90

Choose a reason for hiding this comment

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

👍

return values if current.nil?

values.push({key: current.key, value: current.value})
preorder_helper(current.left, values)
preorder_helper(current.right, values)

return values
end

# Time Complexity:
# Space Complexity:

def preorder
raise NotImplementedError
current = @root
values = []
return preorder_helper(current, values)
end

# Time Complexity: O(n)
# Space Complexity: O(n)

def postorder_helper(current, values)
Comment on lines +107 to +110

Choose a reason for hiding this comment

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

👍

return values if current.nil?

postorder_helper(current.left, values)
postorder_helper(current.right, values)
values.push({key: current.key, value: current.value})

return values
end

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

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
# Time Complexity: o(n)
# Space Complexity: O(h), where h = height
def height(current = @root)
Comment on lines +126 to +128

Choose a reason for hiding this comment

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

👍 Nice work on the space/time complexity!

return 0 if current.nil?

left_child = height(current.left)
right_child = height(current.right)

if left_child > right_child
return left_child + 1
else
return right_child + 1
end

end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) if tree is not balanced; O(log(n)) if it's balanced
# Space Complexity: o(n)
def bfs
raise NotImplementedError
values = []
h = height
i = 1
while i <= h
bfs_helper(@root, i, values)
i += 1
end

return values
end

def bfs_helper(current, level, values)
return values if current.nil?

if level == 1
values.push({key: current.key, value: current.value})
elsif level > 1
bfs_helper(current.left, level - 1, values)
bfs_helper(current.right, level - 1, values)
end
return values
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