Skip to content

Angele Z #41

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 5 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
110 changes: 92 additions & 18 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,121 @@ def initialize
@root = nil
end

# Time Complexity:
# Time Complexity: O(logn) in best case scenario
# Space Complexity:
def add(key, value)
raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
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(logn) since binary
# Space Complexity: O(n) since
def find(key)
raise NotImplementedError
return find_helper(@root, key)
end

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

if key < current_node.key
find_helper(current_node.left, key)
else
find_helper(current_node.right, key)
end
end


# Time Complexity: O(n) since going to each node once, backtracking but still backtracking a smaller number of times than there are nodes, so it depends on the number of nodes no matter what.
# Space Complexity: O(n) making an array that is number-of-nodes long
def inorder
raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
# helper
def inorder_helper(current_node, list)
# left, root, right
return list if current_node.nil?
# recursion! left side first
inorder_helper(current_node.left, list)
list << {key: current_node.key, value: current_node.value}
inorder_helper(current_node.right, list)
return list
end

# Time Complexity: same as inorder
# Space Complexity: same as inorder
def preorder
Comment on lines +72 to 74

Choose a reason for hiding this comment

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

Same issues as inorder!

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

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, list)
# root, left, right
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: same as inorder
# Space Complexity: same as inorder
def postorder
Comment on lines +87 to 89

Choose a reason for hiding this comment

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

Similar issues

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

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, list)
# left, right, root
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) since each node is visited once
# Space Complexity: O(logn) assuming balanced tree since max mem stack being used is height of tree
def height
raise NotImplementedError
return height_helper(@root, 0)
end

def height_helper(current_node, current_height)
return 0 if current_node.nil?
left_height = height_helper(current_node.left, current_height)
right_height = height_helper(current_node.right, current_height)
if left_height > right_height
return left_height + 1
else
return right_height + 1
end

end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
list = []
return list if @root.nil?
queue = [@root]
until queue.empty
current = queue.shift
queue.push(current.left) unless current.left.nil?
queue.push(current.right) unless current.right.nil?

list << {key: current.key, value: current.value }
end
end

# Useful for printing
Expand Down
45 changes: 44 additions & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

describe Tree do
let (:tree) {Tree.new}
let (:tree2) {Tree.new}
let (:tree3) {Tree.new}

let (:tree_with_nodes) {
tree.add(5, "Peter")
Expand All @@ -16,6 +18,29 @@
tree
}

let (:tree_with_nodes2) {
tree2.add(5, "Peter")
tree2.add(3, "Paul")
tree2.add(1, "Mary")
tree2.add(10, "Karla")
tree2.add(15, "Ada")
tree2.add(25, "Kari")
tree2.add(13, "Karino")
tree2
}

let (:tree_with_nodes3) {
tree3.add(5, "Peter")
tree3.add(3, "Paul")
tree3.add(1, "Mary")
tree3.add(10, "Karla")
tree3.add(15, "Ada")
tree3.add(25, "Kari")
tree3.add(17, "Karino")
tree3.add(30, "Karenina")
tree3
}

it "add & find values" do
tree.add(5, "Peter")
expect(tree.find(5)).must_equal "Peter"
Expand Down Expand Up @@ -68,6 +93,24 @@
{: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

it "returns correct height of 2nd tree" do
expect(tree_with_nodes2.height).must_equal 4
end

it "returns correct height of 3rd tree" do
expect(tree_with_nodes3.height).must_equal 5
end
end

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