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

Sockets - Erica #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project is due **Monday September 2nd, 2019**
In this exercise you will implement, in Ruby, several Tree methods.

- `add(value)` - This method adds a value to the Binary Search Tree
- `find(value)` - This method returns true if the given value is in the tree and false otherwise.
- `find(value)` - This method returns the corresponding value if the given key is in the tree and nil otherwise.
- `inorder` - This method returns an array of all the elements in the tree, in order.
- `postorder` - This method returns an array of all the elements in a postorder fashion (left, right , root).
- `preorder` - This method returns an array of all the elements in a preorder fashion (root, left, right).
Expand Down
127 changes: 109 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,79 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) (O(logn) if tree is balanced), where n is the height of the tree
# Space Complexity: O(1)
def add(key, value)
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:
# Time Complexity: O(n) (O(logn) if tree is balanced), where n is the height of the tree
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if @root == nil

current = @root

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

return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the number of nodes
def inorder
raise NotImplementedError
return [] if @root == nil

current = @root
tree = []

return inorder_helper(current, tree)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the number of nodes
def preorder
raise NotImplementedError
return [] if @root == nil

current = @root
tree = []

return preorder_helper(current, tree)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(n), where n is the number of nodes
def postorder
raise NotImplementedError
return [] if @root == nil

current = @root
tree = []

return postorder_helper(current, tree)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n), where n is the number of nodes
# Space Complexity: O(1)
def height
raise NotImplementedError
return 0 if @root == nil

current = @root

return height_helper(current, 1, 1)
end

# Optional Method
Expand All @@ -63,4 +102,56 @@ def bfs
def to_s
return "#{self.inorder}"
end

private

def add_helper(current, new_node)
return new_node if current == nil

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

return current
end

def inorder_helper(current, tree)
return tree if current == nil

inorder_helper(current.left, tree)
tree.push({ :key => current.key, :value => current.value })
inorder_helper(current.right, tree)

end

def preorder_helper(current, tree)
return tree if current == nil

tree.push({ :key => current.key, :value => current.value })

preorder_helper(current.left, tree)
preorder_helper(current.right, tree)
end

def postorder_helper(current, tree)
return tree if current == nil

postorder_helper(current.left, tree)
postorder_helper(current.right, tree)

tree.push({ :key => current.key, :value => current.value })
end

def height_helper(current, current_height, max_height)
return max_height if current == nil

max_height = current_height if current_height > max_height

max_height = height_helper(current.left, current_height + 1, max_height)

Choose a reason for hiding this comment

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

I'm not sure you would need to pass in the current_height here, but it does work.

max_height = height_helper(current.right, current_height +1, max_height)

return max_height
end
end