Skip to content
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
128 changes: 105 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

def initialize(key, val)
def initialize(key, val)
@key = key
@value = val
@left = nil
@right = nil
end
end
end

class Tree
Expand All @@ -16,47 +16,129 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)/ O(n)
# Space Complexity: O(log n) for a balanced BST; O(n) for unbalanced BST
def add_helper(curr, key, value)
return TreeNode.new(key, value) if curr.nil?

if key < curr.key
curr.left = add_helper(curr.left, key, value)
else
curr.right = add_helper(curr.right, key, value)
end

return curr
end

def add(key, value)
raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)/O(n)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
curr = @root

until curr.nil?
if key > curr.key
curr = curr.right
elsif key < curr.key
curr = curr.left
else
return curr.value
end
end

return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(curr, list)
Comment on lines +55 to +57

Choose a reason for hiding this comment

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

👍

return list if curr.nil?

# inorder: left - root - right
inorder_helper(curr.left, list)
list << {key: curr.key, value: curr.value}
inorder_helper(curr.right, list)

Choose a reason for hiding this comment

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

Just for clarity on what's being returned, this is a minor suggestion.

Suggested change
inorder_helper(curr.right, list)
inorder_helper(curr.right, list)
return list


return list
end

def inorder
raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(curr, list)
return list if curr.nil?

# preorder: root - left - right
list << { key: curr.key, value: curr.value }
preorder_helper(curr.left, list)
preorder_helper(curr.right, list)

return list
end

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

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

# postorder: left - right - root
postorder_helper(curr.left, list)
postorder_helper(curr.right, list)
list << { key: curr.key, value: curr.value }

return list
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
return postorder_helper(@root, [])
end

# Time Complexity: O(n)
# Space Complexity: O(log n)/ O(n)
def height_helper(curr, height)
return height if curr.nil?

height += 1
left_height = height_helper(curr.left, height)
right_height = height_helper(curr.right, height)

return [left_height, right_height].max
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
return height_helper(@root, 0)
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def bfs
raise NotImplementedError
visted = []
return visted if @root.nil?

to_visit = [@root]
until to_visit.empty?
curr = to_visit.shift
visted << {
key: curr.key,
value: curr.value
}

to_visit << curr.left if curr.left
to_visit << curr.right if curr.right
end

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