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

Dani - Leaves #49

Open
wants to merge 4 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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Tree Exercise

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
Expand Down
123 changes: 102 additions & 21 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,128 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log(n))
# Space Complexity: O(1), because we are adding one node at the time.
def add(key, value)
raise NotImplementedError
@root = add_helper(@root, key, value)
end

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

if root.key > key
root.left = add_helper(root.left, key, value)
else
root.right = add_helper(root.right, key, value)
end

return root
end

# Time Complexity: O(log(n))
# Space Complexity: O(log(n)), because of the stack
def find(key)
raise NotImplementedError
return find_helper(@root, key)
end

def find_helper(node, key)
return nil if node.nil?

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n), if tree is not balanced (stack)
def inorder
raise NotImplementedError
result = []
inorder_recursion(@root, result)
return result
end

# Time Complexity:
# Space Complexity:
def inorder_recursion(node, result)
return if node.nil?

inorder_recursion(node.left, result)
result.push({ :key => node.key, :value => node.value })
inorder_recursion(node.right, result)
end

# Time Complexity: O(n)
# Space Complexity: O(n) if tree is not balanced (stack)
def preorder
raise NotImplementedError
result = []
preorder_recursion(@root, result)
return result
end

def preorder_recursion(node, result)
return if node.nil?

result.push({ :key => node.key, :value => node.value })
preorder_recursion(node.left, result)
preorder_recursion(node.right, result)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n) if tree is not balanced (stack)
def postorder
raise NotImplementedError
result = []
postorder_recursion(@root, result)
return result
end

def postorder_recursion(node, result)
return if node.nil?

postorder_recursion(node.left, result)
postorder_recursion(node.right, result)
result.push({ :key => node.key, :value => node.value })
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n) if tree is not balanced (stack)
def height
raise NotImplementedError
return get_depth(@root, 0)
end

def get_depth(node, depth)
return depth if node.nil?

return [get_depth(node.left, depth + 1), get_depth(node.right, depth + 1)].max
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n) (result array)
def bfs
raise NotImplementedError
return bfs_helper(@root)
end

def bfs_helper(node)
result = []
queue = Queue.new()
queue.enq(node)

while !queue.empty?
curr = queue.deq()

next if curr.nil?

result.push({:key => curr.key, :value => curr.value})
queue.enq(curr.left)
queue.enq(curr.right)
end

return result
end

# Useful for printing
Expand Down
12 changes: 11 additions & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
end
end

describe "height" do
it "returns 0 for an empty tree" do
expect(tree.height).must_equal 0
end

it "returns correct height of tree" do
expect(tree_with_nodes.height).must_equal 4
end
end

describe "breadth first search" do
it "will give an empty array for an empty tree" do
Expand All @@ -80,4 +90,4 @@
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
end
end