Skip to content

space - nora #30

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
118 changes: 100 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,122 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(key, value)
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.

👍 , your time/space complexities are correct assuming the tree is balanced.

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)
if new_node.key <= current.key
if current.left.nil?
return current.left = new_node
else
add_helper(current.left, new_node)
end
else
if current.right.nil?
return current.right = new_node
else
add_helper(current.right, new_node)
end
end
end

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(key)
Comment on lines +48 to 50

Choose a reason for hiding this comment

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

👍 Since you are using a loop the space complexity is actually O(1) since you are not building a system stack.

raise NotImplementedError
return nil if @root.nil?
current = @root

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

# if not found
return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +68 to 70

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# This method returns an array of all the elements in the tree, in order. (left, root, right)
inorder_array = []
inorder_helper(@root, inorder_array)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, array)
return array if current.nil?

inorder_helper(current.left, array)
array << { key: current.key, value: current.value }
inorder_helper(current.right, array)
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# This method returns an array of all the elements in a preorder fashion (root, left, right).
preorder_array = []

preorder_helper(@root, preorder_array)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, array)
return array if current.nil?

array << { key: current.key, value: current.value }
preorder_helper(current.left, array)
preorder_helper(current.right, array)
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# This method returns an array of all the elements in a postorder fashion (left, right, root).
postorder_array = []

postorder_helper(@root, postorder_array)
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, array)
return array if current.nil?

postorder_helper(current.left, array)
postorder_helper(current.right, array)
array << { key: current.key, value: current.value }
end

# Time Complexity: O(n) - worst case for unbalanced, best case O(log n)
# Space Complexity: O(1)
def height
Comment on lines +118 to 120

Choose a reason for hiding this comment

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

👍 Since you are doing recursion the space complexity is O(log n) assuming the tree is balanced and O(n) otherwise.

The time complexity is O(n) since you visit each node.

raise NotImplementedError
# return 0 if @root == nil

height_helper(@root)
end

def height_helper(current, count = 0)
return count if current.nil?


left_height = height_helper(current.left, count + 1)
right_height = height_helper(current.right, count + 1)
return [left_height, right_height].max

# return left_height > right_height ? left_height : right_height
end

# Optional Method
Expand Down