Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nara #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Nara #26

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: 104 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,128 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) for balanced trees, O(n) for unbalanced trees.
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since you have a recursive add_helper you are using the system stack and making O(log n) recursive calls which go on the stack. So you have O(log n) space complexity.

This applies to your other recursive methods as well.

def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root == nil
@root = new_node
else
current = @root
add_helper(current, new_node)
end
end

# Time Complexity:
# Space Complexity:
def add_helper(current, new_node)
if current == nil
return current = new_node
elsif 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) for balanced trees, O(n) for unbalanced trees.
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if @root == nil

current = @root
find_helper(current, key)
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
array = []
return array if @root == nil
current = @root

return inorder_helper(current, array)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, array)
if current == nil
return array
else
inorder_helper(current.left, array)
array.push({:key => current.key, :value => current.value})
inorder_helper(current.right, array)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
array = []
return array if @root == nil
current = @root

return preorder_helper(current, array)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, array)
if current == nil
return array
else
array.push({:key => current.key, :value => current.value})
preorder_helper(current.left, array)
preorder_helper(current.right, array)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
raise NotImplementedError
array = []
return array if @root == nil
current = @root

return postorder_helper(current, array)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, array)
if current == nil
return array
else
postorder_helper(current.left, array)
postorder_helper(current.right, array)
array.push({:key => current.key, :value => current.value})
end
end

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

def height_helper(current)
return 0 if current == nil
left_side = height_helper(current.left)
right_side = height_helper(current.right)

if left_side < right_side
return (right_side + 1)
else
return (left_side + 1)
end
end
# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down