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

Fire - Roshni #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
193 changes: 169 additions & 24 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -2,12 +2,12 @@ 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
@@ -16,51 +16,196 @@ def initialize
@root = nil
end

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

👍

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

# Time Complexity:
# Space Complexity:
def add_helper(current, key, value)
if current.nil?
current = TreeNode.new(key, value)
elsif key <= current.key
current.left = add_helper(current.left, key, value)
else
current.right = add_helper(current.right, key, value)
end
return current
end

# iterative add helper
# def add_helper(current, key, value)
# added_to_tree = false

# until added_to_tree
# if key <= current.key
# next_node = current.left
# if next_node.nil?
# new_node = TreeNode.new(key, value)
# current.left = new_node
# added_to_tree = true
# else
# current = current.left
# end
# else
# next_node = current.right
# if next_node.nil?
# new_node = TreeNode.new(key, value)
# current.right = new_node
# added_to_tree = true
# else
# current = current.right
# end
# end
# end
# end
Comment on lines +40 to +65

Choose a reason for hiding this comment

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

Nice!




# Time Complexity: O(log n) for balanced, O(n) for unbalanced
# Space Complexity: O(log n) for balanced, O(n) for unbalanced
def find(key)
Comment on lines +69 to 71

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
find_helper(@root, key)
end

def find_helper(current, key)
return nil if current.nil?

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

# Time Complexity:
# Space Complexity:
# iterative find helper
# def find_helper(current, key)
# return nil if current.nil?
# found = false
# until found
# if current.key == key
# found = true
# return current.value
# else
# if key < current.key
# current = current.left
# else
# current = current.right
# end
# end
# return nil if current.nil?
# end
# end

# Time Complexity: O(n) to visit all nodes
# Space Complexity: O(n) values array depends on how many nodes there are
def inorder
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
values = []
return inorder_helper(@root, values)
end

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

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

# Time Complexity: O(n) to visit all nodes
# Space Complexity: O(n) values array depends on how many nodes there are
def preorder
Comment on lines +122 to 124

Choose a reason for hiding this comment

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

👍

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

def preorder_helper(current, values)
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:
# Time Complexity: O(n) to visit all nodes
# Space Complexity: O(n) values array depends on how many nodes there are
def postorder
Comment on lines +138 to 140

Choose a reason for hiding this comment

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

👍

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

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:
# Space Complexity:
# Time Complexity: O(n) to visit all the nodes
# Space Complexity: O(log n) if balanced, O(n) if unbalanced
def height
Comment on lines +154 to 156

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
height_helper(@root)
end

def height_helper(current)
return 0 if current.nil?

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

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

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n^2) due to use of .shift
# Space Complexity: O(n)
# Solution from class
def bfs
Comment on lines +170 to 173

Choose a reason for hiding this comment

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

👍 This should work, and good insight with the .shift's affect on the time complexity.

raise NotImplementedError
return [] if @root.nil?

queue = [root]
output = []

until queue.empty?
node = queue.shift
output << { key: node.key, value: node.value }

queue << node.left if node.left
queue << node.right if node.right
end

return output
end

# Useful for printing
def to_s
return "#{self.inorder}"
end
end

#### testing ####
# tree = Tree.new
# tree.add(5, "Roshni")
# tree.add(3, "Test")
# tree.add(7, "Ada")
# tree.add(34, "Grace")
# tree.add(67, "Margaret")

# puts tree.root.value
# puts tree.root.left.value
# puts tree.root.right.value

# puts tree.inorder
# puts tree.preorder
# puts tree.postorder
# puts tree.height
2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -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