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 - Ana #24

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
118 changes: 97 additions & 21 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,130 @@ class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

def initialize(key, val)
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end
end

class Tree
attr_reader :root

def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
def add_helper(current_node, new_node)
return new_node if current_node.nil?

if new_node.key < current_node.key
current_node.left = add_helper(current_node.left, new_node)
elsif new_node.key > current_node.key
current_node.right = add_helper(current_node.right, new_node)
end

return current_node
end

# Time Complexity: o(log(n))
# Space Complexity: o(1)
def add(key, value = nil)
Comment on lines +32 to +34

Choose a reason for hiding this comment

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

👍 the time complexity is O(log n) if the tree is balanced and O(n) if it's unbalanced. The space complexity is the same due to recursion.

new_node = TreeNode.new(key, value)
if @root.nil?
@root = new_node
else
add_helper(@root, new_node)
end
end

# Time Complexity:
# Space Complexity:
def find_helper(current_node,key)
return nil if current_node.nil?

if current_node.key == key
return current_node.value
elsif key <= current_node.key
find_helper(current_node.left, key)
else key > current_node.key
return find_helper(current_node.right, key)
end
end


# Time Complexity: o(n)
# Space Complexity: o(n)
def find(key)
Comment on lines +56 to 58

Choose a reason for hiding this comment

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

👍 the time complexity is O(log n) if the tree is balanced and O(n) if it's unbalanced. The space complexity is the same due to recursion.

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

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, values)
return values if current_node.nil?

inorder_helper(current_node.left, values)
values.push({key: current_node.key, value: current_node.value})
inorder_helper(current_node.right, values)
return values
end


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

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 preorder_helper(current_node, values)
return values if current_node.nil?

values.push({key: current_node.key, value: current_node.value})
preorder_helper(current_node.left, values)
preorder_helper(current_node.right, values)
return values
end

# Time Complexity: o(n)
# Space Complexity: o(n)
def preorder
Comment on lines +93 to 95

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 postorder_helper(current, values)
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: o(n)
# Space Complexity: o(n)
def postorder
Comment on lines +109 to 111

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

def height_helper(current_node,height)
return height if current_node.nil?

# Time Complexity:
# Space Complexity:
leftmax = height_helper(current_node.left, height +1 )
rightmax = height_helper(current_node.right, height + 1)

return [leftmax, rightmax].max
end

# Time Complexity: o(n)
# Space Complexity: o(n)
def height
Comment on lines +125 to 127

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(log n) if the tree is balanced and O(n) if it's unbalanced due to the recursion. You have the time complexity right.

raise NotImplementedError
return height_helper(@root, 0)
end

# Optional Method
Expand Down
1 change: 0 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

require "minitest"
require "minitest/autorun"
require "minitest/reporters"
Expand Down
4 changes: 2 additions & 2 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
end
end

describe "breadth first search" do
describe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end
Expand All @@ -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