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
155 changes: 132 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,168 @@ 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

def inorder(array)
#left, root, right
if @left != nil
@left.inorder(array)
end

array << {key: @key, value: @value}

if @right != nil
@right.inorder(array)
end
end

def preorder(array)
#Root, Left, Right
array << {key: @key, value: @value}

if @left != nil
@left.preorder(array)
end

if @right != nil
@right.preorder(array)
end
end

def postorder(array)
#left, right, root

if @left != nil
@left.postorder(array)
end

if @right != nil
@right.postorder(array)
end

array << {key: @key, value: @value}
end
end

class Tree
attr_reader :root

def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) if it is balanced, O(n) if it is not
# Space Complexity: Constant

Choose a reason for hiding this comment

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

Actually since add_helper is recursive the space complexity will be O(log n) if the tree is balanced and O(n) if it's not due to the function call stack.

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

def add_helper(current, new_node)
if current == nil
return current = new_node
end

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:
# Space Complexity:
# Time Complexity: O(log n) if it is balanced, O(n) if it is not balanced
# Space Complexity: constant
def find(key)
raise NotImplementedError
current = @root

if current == nil
return nil
end

while current != nil
if key == current.key
return current.value
elsif key <= current.key
current = current.left
else
current = current.right
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity:O(log n)

Choose a reason for hiding this comment

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

Since you are building an array in this example, it would be O(n)

Same for all the traversals

def inorder
raise NotImplementedError
list = []
if @root != nil
@root.inorder(list)
end
return list
end

# Time Complexity:
# Space Complexity:
# Time Complexity:O(n)
# Space Complexity:O(log n)
def preorder
raise NotImplementedError
#Root, Left, Right
list = []

if @root == nil #done
return []
else
@root.preorder(list)
end
return list
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity:O(log n)
def postorder
raise NotImplementedError
#left, right, root
list = []

if @root == nil
return []
else
@root.postorder(list)
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
# Time Complexity:O(n)
# Space Complexity:O(n)

Choose a reason for hiding this comment

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

Space complexity would be O(n) if the tree is widely unbalanced, but O(log n) if it is balanced.

def height(current = @root)
return 0 if current == nil
heightLeft = 0
heightRight = 0

if current.left != nil
heightLeft = height(current.left)
end

if current.right != nil
heightRight = height(current.right)
end

if heightLeft < heightRight
return heightRight + 1
else
return heightLeft + 1
end
end

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