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
121 changes: 99 additions & 22 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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,40 +16,117 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
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(current, new_node)
return new_node if current.nil?
if new_node.key <= current.key
current.left = add_helper(current.left, new_node)
else
current.right = add_helper(current.right, new_node)
end
return current
end

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
if @root.nil?
return nil
else
find_helper(@root, key)
end
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
result = []
return inorder_helper(@root, result)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, result)
return [] if current.nil?
# traverse the left subtree
inorder_helper(current.left, result)
# visit the current node
result << {:key=>current.key, :value => current.value}
# traverse the right subtree
inorder_helper(current.right, result)
return result
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
result = []
preorder_helper(@root, result)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, result)
return [] if current.nil?
# visit the current node
result << {:key=>current.key, :value => current.value}
# traverse the left subtree
preorder_helper(current.left, result)
# traverse the right subtree
preorder_helper(current.right, result)
return result
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
raise NotImplementedError
result = []
return postorder_helper(@root, result)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, result)
return [] if current.nil?
# traverse the left subtree
postorder_helper(current.left, result)
# traverse the right subtree
postorder_helper(current.right, result)
# visit the current node
result << {:key=>current.key, :value => current.value}
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def height
raise NotImplementedError
return 0 if @root.nil?
return height_helper(@root)
end

def height_helper(current)
return 0 if current.nil?
# Otherwise return 1 plus the maximum of the heights of the right and left subtrees
heights = []
right_height = height_helper(current.right)
left_height = height_helper(current.left)
heights << right_height
heights << left_height
return heights.max + 1
end

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