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 - Beatrice #25

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(n) - call stack
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.

👍 The time & space complexities are both O(log n) if the tree is balanced and O(n) if unbalanced.

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

add_helper(key, value, @root)
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
def add_helper(key, value, current)
return TreeNode.new(key, value) if current.nil?

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

return current
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
# Time Complexity: O(log n)
# Space Complexity: O(n) - call stack
def find(key, current = @root)
Comment on lines +42 to +44

Choose a reason for hiding this comment

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

👍 The time & space complexities are both O(log n) if the tree is balanced and O(n) if unbalanced.

return nil if current.nil?
return current.value if current.key == key

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

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(current = @root, answer = [])
Comment on lines +55 to +57

Choose a reason for hiding this comment

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

👍


if current
inorder(current.left, answer)
answer.push({key: current.key, value: current.value})
inorder(current.right, answer)
end

return answer
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(current = @root, answer = [])
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.

👍

if current
answer.push({key: current.key, value: current.value})
preorder(current.left, answer)
preorder(current.right, answer)
end

return answer
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(current = @root, answer = [])
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.

👍

if current
postorder(current.left, answer)
postorder(current.right, answer)
answer.push({key: current.key, value: current.value})
end

return answer
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def height(current = @root)
Comment on lines +92 to +94

Choose a reason for hiding this comment

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

👍 The time complexity here is right, but the space complexity is O(log n) if the tree is balanced and O(n) if it's not.

unless current
return 0
end

return 1 + [height(current.left), height(current.right)].max
end

# Optional Method
Expand All @@ -63,4 +110,4 @@ def bfs
def to_s
return "#{self.inorder}"
end
end
end
24 changes: 12 additions & 12 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end

it "will return the tree in preorder" do
expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
Expand Down Expand Up @@ -91,7 +91,7 @@
it "will return 1 for a tree of height 1" do
my_tree = Tree.new

my_tree.add(100)
my_tree.add(100, 100)
expect(my_tree.height).must_equal 1
end

Expand All @@ -102,23 +102,23 @@
it "will report the height for unbalanced trees" do
my_tree = Tree.new

my_tree.add(100)
my_tree.add(110)
my_tree.add(120)
my_tree.add(130)
my_tree.add(140)
my_tree.add(100, 100)
my_tree.add(110, 110)
my_tree.add(120, 120)
my_tree.add(130, 130)
my_tree.add(140, 140)

expect(my_tree.height).must_equal 5

my_tree = Tree.new

my_tree = Tree.new

my_tree.add(100)
my_tree.add(90)
my_tree.add(80)
my_tree.add(70)
my_tree.add(60)
my_tree.add(100, 100)
my_tree.add(90, 90)
my_tree.add(80, 80)
my_tree.add(70, 70)
my_tree.add(60, 60)

expect(my_tree.height).must_equal 5
end
Expand Down