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
133 changes: 113 additions & 20 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,40 +16,133 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add_while_loop(key, value)
if @root.nil?
@root = TreeNode.new(key, value)
else
current = @root
while true
if key < current.key
if !current.left.nil?
current = current.left
else
current.left = TreeNode.new(key, value)
return
end
else
if !current.right.nil?
current = current.right
else
current.right = TreeNode.new(key, value)
return
end
end
end
end
end

# Time Complexity: O(logn)
# Space Complexity: O(logn)
def add(key, value)
Comment on lines +44 to 46

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
def add_helper(current_node, key, value)
return TreeNode.new(key, value) if current_node.nil?

if 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(logn)
# Space Complexity: O(logn)
def find(key)
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.

👍

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

# Time Complexity:
# Space Complexity:
def find_helper(current_node, key)
return current_node if current_node.key == key

if key < current_node.key
return find_helper(current_node.left, key)
else
return find_helper(current_node.right, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
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
inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, list)
return list if current_node.nil?

inorder_helper(current_node.left, list)
list << { 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
preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, list)
return list if current_node.nil?

list << { 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 +114 to 116

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, list)
return list if current_node.nil?

postorder_helper(current_node.left, list)

postorder_helper(current_node.right, list)

list << { key: current_node.key, value: current_node.value}

return list
end

# Time Complexity: O(n)
# Space Complexity: O(logn)
def height
Comment on lines +132 to 134

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return height_helper(@root, i = 0)
end

def height_helper(current_node, i = 0)
return i if current_node.nil?

left = height_helper(current_node.left, i + 1)

right = height_helper(current_node.right, i + 1)

return [left, right].max
end

# Optional Method
Expand Down