Skip to content
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
122 changes: 101 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,47 +16,127 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n) if balanced, otherwise O(n) where n is number of nodes
# Space Complexity: O(n), n is height
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.

👍 Yeah recursion! You threw me with the O(n) space complexity until I saw the note on height.

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(curr_node, new_node)
return new_node if curr_node.nil?

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

return curr_node
end

# Time Complexity: O(log n) if balanced
# Space Complexity: O(n)
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.

👍 I assume space complexity is related to the height.

raise NotImplementedError
find_helper(@root, key) #starts at root
end

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

# Time Complexity: O(n), n is number of nodes
# Space Complexity: O(n)
def inorder
Comment on lines +60 to 62

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

def inorder_helper(current, list)
return list if current.nil?
inorder_helper(current.left, list)
list << {key: current.key, value: current.value}
inorder_helper(current.right, list)
return list
end

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

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, list)
return list if current.nil?
list << {key: current.key, value: current.value}
preorder_helper(current.left, list)
preorder_helper(current.right, list)
end

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

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

def postorder_helper(current, list)
return list if current.nil?
postorder_helper(current.left, list)
postorder_helper(current.right, list)
list << {key: current.key, value: current.value}
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
def height(current = @root)

Choose a reason for hiding this comment

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

👍 Time/space complexity?

return 0 if current.nil?
return 1 if current.right.nil? && current.left.nil?

left_count = 0
right_count = 0
left_count += 1 + height(current.left)
right_count += 1 + height(current.right)
left_count > right_count ? left_count : right_count
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
# Time Complexity:
# Space Complexity:
def bfs
Comment on lines 125 to 127

Choose a reason for hiding this comment

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

Yeah BFS!

Time/space complexity

raise NotImplementedError
return [] if @root.nil?
queue = []
list = []
queue.push(@root)
while queue.length > 0
current = queue[0]
list << {key: current.key, value: current.value}
queue.push(current.left) if current.left
queue.push(current.right) if current.right
queue.shift
end
return list
end

# Useful for printing
Expand Down