Skip to content

Time - Sharon Cheung #24

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 2 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
120 changes: 104 additions & 16 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,128 @@ 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.

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

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

if @root.nil?
@root = new_node
else
add_helper(@root, key, value)
end
end

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

if key < current.key
current.left = add_helper(current.left, key, value)
else
current.right = add_helper(current.right, key, value)
end

return current
end

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

Choose a reason for hiding this comment

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

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

raise NotImplementedError
return if @root.nil?

if @root == key
return @root.value
else
find_helper(@root, key)
end
end

# Time Complexity:
# Space Complexity:
def find_helper(current, key)
return if current.nil?

if key < current.key
current.left = find_helper(current.left, key)
elsif key > current.key
current.right = find_helper(current.right, key)
else
return current.value
end
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, list)
return list if current_node.nil?
# left
inorder_helper(current_node.left, list)
# middle
list << {key: current_node.key, value: current_node.value}
# right
inorder_helper(current_node.right, list)

return list
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, list)
return list if current_node.nil?
# middle
list << {key: current_node.key, value: current_node.value}
# left
preorder_helper(current_node.left, list)
#right
preorder_helper(current_node.right, list)

return list
end

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

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return postorder_helper(@root, [])
end

def postorder_helper(current_node, list)
return list if current_node.nil?
# left
postorder_helper(current_node.left, list)
#right
postorder_helper(current_node.right, list)
# middle
list << {key: current_node.key, value: current_node.value}

return list
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 121 to 123

Choose a reason for hiding this comment

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

👍 , but time complexity is O(n) and space complexity is O(h).

raise NotImplementedError
if @root.nil?
return 0
else
height_helper(@root)
end
end

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

if (left > right)
return left + 1
else
return right + 1
end
end

# Optional Method
Expand Down