Skip to content

Space - Faezeh #19

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

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

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

👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.

raise NotImplementedError
# raise NotImplementedError
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 add_helper(current, new_node)
return new_node if current.nil?

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

current
end

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +45 to 47

Choose a reason for hiding this comment

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

👍 , but the space complexity is O(log n) because of the call stack, assuming the tree is balanced.

raise NotImplementedError
# raise NotImplementedError
current = @root
return nil if current.nil?

if current.key == key
return current.value
else
find_helper(current, key)
end

end

# Time Complexity:
# Space Complexity:
def find_helper(current, key)
return current.value if key == current.key

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

# Me, Yieni and Haben got together to do the traverse methods.
# We found this link but worked on it for a few hours to understand
# http://rubyalgorithms.com/binary_search_tree.html
# whet &block and yield are doing before using them

# Time Complexity: O(Log n)
# Space Complexity: O(n)
def inorder
Comment on lines +75 to 77

Choose a reason for hiding this comment

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

👍 Since we hit each node in the list, the the time complexity is also O(n)

raise NotImplementedError
# raise NotImplementedError
root = @root
traverse_array = []
return traverse_array if root.nil?

inorder_helper(root) do |node|
traverse_array << { key: node.key, value: node.value }
end

traverse_array
end

# Time Complexity:
# Space Complexity:
# any method in ruby can take a block
# &block is usuful for sending in the block when we want to pass it on
# but with yield alone we did not need to pass in the &block into the main
# inorder_helper method
# if we want to use block.call(node) we need to pass in &block in the main
# helper method

# yield returns the variable - the information back up :)

def inorder_helper(node, &block)
return if node.nil?

inorder_helper(node.left, &block)
yield node
inorder_helper(node.right, &block)
end
Comment on lines +99 to +105

Choose a reason for hiding this comment

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

Clever use of a block-based method.


# Haben's refactor
# def inorder_recursion(current, traverse_array)
# return if current.nil?
# inorder_recursion(current.left, traverse_array) # process left side
# traverse_array << { key: current.key, value: current.value }
# inorder_recursion(current.right, traverse_array) # process right side
# end
# def inorder
# traverse_array = []
# inorder_recursion(@root, traverse_array)
# traverse_array
# end

# Time Complexity: O(Log n)
# Space Complexity: O(n)
def preorder
Comment on lines +120 to 122

Choose a reason for hiding this comment

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

The traversals are all O(n) in time complexity.

raise NotImplementedError
# raise NotImplementedError
root = @root
traverse_array = []
return traverse_array if root.nil?

preorder_helper(root) do |node|
traverse_array << { key: node.key, value: node.value }
end

traverse_array
end

# Time Complexity:
# Space Complexity:
def preorder_helper(node, &block)
return if node.nil?

yield node
preorder_helper(node.left, &block)
preorder_helper(node.right, &block)
end

# Time Complexity: O(Log n)
# Space Complexity: O(n)
def postorder
Comment on lines +143 to 145

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
root = @root
traverse_array = []
return traverse_array if root.nil?

postorder_helper(root) do |node|
traverse_array << { key: node.key, value: node.value }
end

traverse_array
end

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

postorder_helper(node.left, &block)
postorder_helper(node.right, &block)
yield node
end

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

Choose a reason for hiding this comment

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

⚠ This doesn't quite work as it goes down the left and right sides of the tree but doesn't investigate all paths to the leaves.

A better solution:

def height_helper(current_node)
    return 0 if current_node.nil?
    return 1 +[height_helper(current_node.left), height_helper(current_node.right)].max
end

raise NotImplementedError
# raise NotImplementedError
node = @root
return 0 if node.nil?

count_right = 0
count_left = 0

count_left = height_helper(node, 'left', count_left)
count_right = height_helper(node, 'right', count_right)

if count_right > count_left
return count_right
else
return count_left
end
end

def height_helper(node, side, count)
return count if node.nil?
count += 1
if side == 'left'
height_helper(node.left, 'left', count)
else
height_helper(node.right, 'right', count)
end
end

# Optional Method
Expand All @@ -63,4 +204,5 @@ def bfs
def to_s
return "#{self.inorder}"
end

end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

require "minitest"
require "minitest/autorun"
require "minitest/reporters"
Expand Down
2 changes: 0 additions & 2 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@

my_tree = Tree.new

my_tree = Tree.new

my_tree.add(100, "Paula")
my_tree.add(90, "Ann")
my_tree.add(80, "Grace")
Expand Down