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

Branches - Diana Han #38

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
129 changes: 109 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,129 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# def add(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

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(n)
# Space Complexity: O(1)
def add(key, value)
Comment on lines +57 to 59

Choose a reason for hiding this comment

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

This method is recursive so it's going to have some space complexity. What do you think it is?

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
current = @root

return nil if current.nil?

until current.key == key
if current.key > key
current = current.left
else
current = current.right
end
end
Comment on lines +70 to +76

Choose a reason for hiding this comment

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

What about if the key is not in the tree? What about when current is nil?

Suggested change
until current.key == key
if current.key > key
current = current.left
else
current = current.right
end
end
until current.key == key
if current.key > key
current = current.left
else
current = current.right
end
return nil if current.nil?
end

return current.value
end

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

inorder_helper(current_node.left, list)
list << { key: current_node.key, value: current_node.value} # root is getting pushed to the list
inorder_helper(current_node.right, list)
return list
end

# Time Complexity: O(n)
# Space Complexity: O(n) n represents the number of nodes in a tree
def inorder
Comment on lines +89 to 91

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 preorder_helper(current_node, list) #n, l, r
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 preorder
Comment on lines +104 to 106

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 postorder_helper(current_node, list) #l, r, n
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(n)
def postorder
Comment on lines +119 to 121

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 height_helper(current_node)
return 0 if current_node.nil?

left_depth = height_helper(current_node.left)
right_depth = height_helper(current_node.right)

if left_depth > right_depth
return left_depth + 1
else
return right_depth + 1
end
end

# Time Complexity: O(n)
# Space Complexity: O(1)
def height
Comment on lines +138 to 140

Choose a reason for hiding this comment

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

Note that this is a recursive method so you're going to have space complexity due to the call stack.

raise NotImplementedError
height_helper(@root)
end

# Optional Method
Expand Down