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

Angele Z #41

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

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn) in worst case scenario
# Space Complexity: O(1) always just adding 1 new node
def add(key, value)

Choose a reason for hiding this comment

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

Actually O(log n) is the best and average case runtime.

Since you're doing recursion, you do incur some space complexity.

Copy link
Author

Choose a reason for hiding this comment

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

@CheezItMan for the time complexity - would it be O(n) for worst case, and is that possible only if the tree is unbalanced?

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(1) since adding nothing new
def find(key)

Choose a reason for hiding this comment

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

Similar issues with time/space to add

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(1) - nothing being made
def inorder

Choose a reason for hiding this comment

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

Space complexity is O(n) since you're building an array!

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(1) since it doesn't make anything new
def height

Choose a reason for hiding this comment

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

The method works, but you have recursion and so some space complexity. Predictions?

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
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