Skip to content
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
91 changes: 81 additions & 10 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,52 @@ def initialize
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Space Complexity:
def add(key, value = nil)
Comment on lines +20 to +21

Choose a reason for hiding this comment

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

👍 Space & Time complexity?

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

# Time Complexity:
# Space Complexity:
# Space Complexity:
def find(key)
Comment on lines +31 to 32

Choose a reason for hiding this comment

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

👍 Space & Time complexity?

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

end

# Time Complexity:
# Space Complexity:
def inorder

Choose a reason for hiding this comment

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

👍 Space & Time complexity?

raise NotImplementedError
return [] if @root.nil?
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def preorder

Choose a reason for hiding this comment

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

👍 Space & Time complexity?

raise NotImplementedError
return [] if @root.nil?
return preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder
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.

👍 Space & Time complexity?

raise NotImplementedError
return [] if @root.nil?
return postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 62 to 64

Choose a reason for hiding this comment

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

👍 Space & Time complexity?

raise NotImplementedError
return height_helper(@root)
end

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

private
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)
else
current_node.right = add_helper(current_node.right, new_node)
end

return current_node
end

def find_helper(node, key)
if node.nil?
return nil
elsif node.key > key
return find_helper(node.left, key)
elsif node.key < key
return find_helper(node.right, key)
else
return node.value
end
end

def inorder_helper(node, array)
if node
inorder_helper(node.left, array)
array << {key: node.key, value: node.value}
inorder_helper(node.right, array)
end
return array
end

def preorder_helper(node, array)
if node
array << {key: node.key, value: node.value}
preorder_helper(node.left, array)
preorder_helper(node.right, array)
end
return array
end

def postorder_helper(node, array)
if node
postorder_helper(node.left, array)
postorder_helper(node.right, array)
array << {key: node.key, value: node.value}
end
return array
end

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

end
2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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