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

Water - Kareha #19

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
132 changes: 109 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,137 @@ def initialize
@root = nil
end

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

👍 The time/complexities are correct if the tree is balanced and O(n) if unbalanced.

unless root
@root = TreeNode.new(key, value)
else
add_helper(@root, key, value)
end
end

# Time Complexity:
# Space Complexity:
def add_helper(current, key, value)
return TreeNode.new(key, value) unless current

if key < current.key
current.left = add_helper(current.left, key, value)
else
current.right = add_helper(current.right, key, value)
end

return current

end

# Time Complexity: O(logn)
# Space Complexity: O(logn)
def find(key)
Comment on lines +68 to 70

Choose a reason for hiding this comment

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

👍 The time/complexities are correct if the tree is balanced and O(n) if unbalanced.

raise NotImplementedError
return find_helper(@root, key)
end

def find_helper(current, key)
return current unless current
return current.value if key == current.key

if current.key < key
find_helper(current.right, key)
else
find_helper(current.left, key)
end
end

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

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:
def inorder_helper(tree, current)
return tree unless current

inorder_helper(tree, current.left)
tree.push({:key=>current.key, :value=>current.value})
inorder_helper(tree, current.right)

return tree
end

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

Choose a reason for hiding this comment

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

👍

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

def preorder_helper(current, tree)
return tree unless current

tree.push({:key=>current.key, :value=>current.value})
preorder_helper(current.left, tree)
preorder_helper(current.right, tree)

return tree
end

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

Choose a reason for hiding this comment

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

👍

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

def postorder_helper(current, tree)
return tree unless current

postorder_helper(current.left, tree)
postorder_helper(current.right, tree)
tree.push({:key=>current.key, :value=>current.value})

return tree
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def height
Comment on lines +140 to 142

Choose a reason for hiding this comment

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

👍 The space complexity is O(n) if the tree is unbalanced and O(log n) if the tree is balanced.

raise NotImplementedError
return height_helper(@root)
end

def height_helper(current)
return 0 unless current

left_height = height_helper(current.left)
right_height = height_helper(current.right)

return left_height < right_height ? right_height + 1 : left_height + 1
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n*m)
# Space Complexity: O(n)
def bfs
Comment on lines +156 to 158

Choose a reason for hiding this comment

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

Because you are using .shift the time complexity is O(n^2), otherwise well done.

raise NotImplementedError
q = []
tree_bfs = []
return tree_bfs unless @root

q.push(@root)

until q.empty?
current = q.shift

tree_bfs.push({:key=>current.key, :value=>current.value})

q.push(current.left) if current.left
q.push(current.right) if current.right
end

return tree_bfs

end

# Useful for printing
def to_s
return "#{self.inorder}"
end
end
end
5 changes: 2 additions & 3 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
end

it "will return the tree in order" do

expect(tree_with_nodes.inorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
Expand Down Expand Up @@ -95,8 +94,8 @@
expect(my_tree.height).must_equal 1
end

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

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