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
171 changes: 142 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,177 @@
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:
# Time Complexity: O(log n)
# Space Complexity: O(n)
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.

Space complexity n? Why?

Otherwise the method works.

raise NotImplementedError

current = @root
temp = TreeNode.new(key, value)

if @root == nil
@root = temp
return

else
add_helper(@root, temp)
end
end

# Time Complexity:
# Space Complexity:

def add_helper(current_node, temp)
if current_node == nil
return temp
end

if temp.key < current_node.key
current_node.left = add_helper(current_node.left, temp)
else
current_node.right = add_helper(current_node.right, temp)
end
return current_node
end

# Time Complexity: O(log n)
# Space Complexity: O(n)
def find(key)
Comment on lines +48 to 50

Choose a reason for hiding this comment

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

Similar to add with space complexity.

raise NotImplementedError
current = @root
if current == nil
return nil
elsif current.key == key
return current.value
else
find_helper(current, key)
end
end

# Time Complexity:
# Space Complexity:

def find_helper (current, key)
if current.key == key
return current.value
elsif current.key > key && current.left != nil
find_helper(current.left, key)
elsif current.key > key
return nil
elsif current.key < key && current.right != nil
find_helper(current.right, key)
elsif current.key < key
return nil
end
end

# Time Complexity: O(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.

👍

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

# Time Complexity:
# Space Complexity:

def inorder_helper(current_node, list)

if current_node == nil
return list
end

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

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

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 []
else
return preorder_helper(@root, [])
end
end

# Time Complexity:
# Space Complexity:

def preorder_helper(current_node, list)
if current_node == nil
return list
end

list.push({key: current_node.key, value: current_node.value})
preorder_helper(current_node.left, list)
preorder_helper(current_node.right, list)

return list
end

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

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 []
else
return postorder_helper(@root, [])
end
end

# Time Complexity:
# Space Complexity:

def postorder_helper(current_node, list)
if current_node == nil
return list
end

postorder_helper(current_node.left, list)
postorder_helper(current_node.right, list)
list.push({key: current_node.key, value: current_node.value})

return list
end

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

Choose a reason for hiding this comment

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

You're visiting each node so the time complexity is O(n), but because the call stack doesn't go deeper than the height of the tree the space complexity is O(log n) for a balanced tree and O(n) for an unbalanced one.

raise NotImplementedError
if @root == nil
return 0
elsif @root.left == nil && @root.right == nil
return 1
end

return height_helper(@root)

end


def height_helper(current_node)

if current_node == nil
return 0
end

left = height_helper(current_node.left)
right = height_helper(current_node.right)
if left > right
return left + 1
else
return right + 1
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
19 changes: 10 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"


Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

require_relative "../lib/tree"
require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"


Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

require_relative "../lib/tree"

Loading