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
108 changes: 87 additions & 21 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,42 +16,108 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n) if balanced, up to O(n) if unbalanced
# Space Complexity: O(log n) if balanced, up to O(n) if 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_node, key, value)
if current_node.nil?
current_node = TreeNode.new(key, value)
elsif key <= current_node.key
current_node.left = add_helper(current_node.left, key, value)
else
current_node.right = add_helper(current_node.right, key, value)
end

return current_node
end


# Time Complexity: O(log n) if balanced, up to O(n) if unbalanced
# Space Complexity: O(log n) if balanced, up to O(n) if unbalanced
def find(key)
Comment on lines +43 to 45

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:
def find_helper(current_node, key)
if current_node.nil?
return nil
elsif current_node.key == key
return current_node.value
elsif key < current_node.key
find_helper(current_node.left, key)
else
find_helper(current_node.right, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +65 to 67

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return inorder_helper(@root, [])
end

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

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
Comment on lines +79 to 81

Choose a reason for hiding this comment

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

👍

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

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

values << {key: current.key, value: current.value}
preorder_helper(current.left, values)
preorder_helper(current.right, values)
end


# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +94 to 96

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return postorder_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 << {key: current.key, value: current.value}
end

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

Choose a reason for hiding this comment

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

👍 The space complexity here is O(h) where h is the height. Otherwise correct.

raise NotImplementedError
return height_helper(@root)
end

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

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


# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down
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