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 - Julia Bouvier #37

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

# Time Complexity:
# Space Complexity:
# Time Complexity: log(n)
# Space Complexity: 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.

👍

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

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

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

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


# Time Complexity: log(n)
# Space Complexity: 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.

👍

raise NotImplementedError
if @root.nil?
return nil
else
find_helper(key, @root)
end
end

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

def find_node(key)
if @root.nil?
return nil
else
find_node_helper(key, @root)
end
end

def find_node_helper(key, current)
return nil if current.nil?
if current.key == key
return current
elsif key > current.key
find_node_helper(key, current.right)
else
find_node_helper(key, current.left)
end
end

def delete(key)

Choose a reason for hiding this comment

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

Just noting this isn't working.

node = find_node(key)
unless node.nil?
remove(node)
end
end

def remove(node)
if node.left.nil? && node.right.nil?
node = nil

Choose a reason for hiding this comment

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

This only changes the local variable (node) reference.

elsif !node.left.nil? && node.right.nil?
node = node.left
elsif node.left.nil? && !node.right.nil?
node = node.right
puts node.value
else
min_node = find_min_node(node.right)
node = min_node
min_node = nil
end
end

def find_min_node(node)
if node.left.nil?
min_node = node
return min_node
else
find_min_node(node.left)
end
end

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

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, list)
return list if current.nil?

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

return list
end

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

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(node, list)
return list if node.nil?

list << { key: node.key, value: node.value}
preorder_helper(node.left, list)
preorder_helper(node.right, list)
end

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

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

# Time Complexity:
# Space Complexity:
def postorder_helper(node, list)
return list if node.nil?

postorder_helper(node.left, list)
postorder_helper(node.right, list)
list << { key: node.key, value: node.value}
end

# Time Complexity: O(n)
# Space Complexity: O(log n)
def height
Comment on lines +158 to 160

Choose a reason for hiding this comment

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

This method isn't actually working. See my note below.

Think about this:

If the node is nil then return 0
If the node is not 0, find the height of the left and the right.

Return the height of the bigger subtree and add one (for the current node).

raise NotImplementedError
return height_helper(@root, 0, 0)
end

def height_helper(node, left, right)
if node.nil?
if left >= right
return left
else
return right
end
end

height_helper(node.left, left + 1, right)
height_helper(node.right, left, right + 1)

Choose a reason for hiding this comment

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

This is only returning the right side's height!

end

# Optional Method
Expand Down