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
149 changes: 120 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,155 @@
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
attr_reader :root
def initialize
@root = nil
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(log n)
# Space Complexity: O(log n)
def add(key, value)
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
new_node = TreeNode.new(key,value)

if @root.nil?
@root = new_node
else
add_helper(@root, key, value)
end
end

# Time Complexity:
# Space Complexity:

# Time Complexity: Best: O(log n), Worst: O(n)
# Space Complexity: O(n)
Comment on lines +43 to +44

Choose a reason for hiding this comment

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

Both space/time complexity are O(log n) for a balanced tree and O(n) for an unbalanced tree.

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

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

end

def find(key)
raise NotImplementedError
return if @root.nil?

if @root == key
return @root.value
else
find_helper(@root, key)
end
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(current_node, list)
Comment on lines +68 to +70

Choose a reason for hiding this comment

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

👍

return list if current_node.nil?
# left side
inorder_helper(current_node.left, list)
# middle node
list << { key: current_node.key, value: current_node.value }
# right side
inorder_helper(current_node.right, 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(current_node, list)
Comment on lines +85 to +87

Choose a reason for hiding this comment

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

👍

return list if current_node.nil?
# middle node
list << { key: current_node.key, value: current_node.value }
# left side
preorder_helper(current_node.left, list)
# right side
preorder_helper(current_node.right, list)

return list
end

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

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(current_node, list)
return list if current_node.nil?
# left side
postorder_helper(current_node.left, list)
# right side
postorder_helper(current_node.right, list)
# middle node
list << { key: current_node.key, value: current_node.value }

return list
end

def postorder

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity:O(n)
def height_helper(current_node)

Choose a reason for hiding this comment

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

👍 Nice work, the space complexity however is O(log n) for a balanced tree and O(n) for an unbalanced tree.

if current_node.nil?
return 0
else
left = height_helper(current_node.left)
right = height_helper(current_node.right)

if (left > right)
return left + 1
else
return right + 1
end
end
end

def height
raise NotImplementedError
if @root.nil?
return 0
else
height_helper(@root)
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
177 changes: 89 additions & 88 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

let (:tree_with_nodes) {
tree.add(5, "Peter")
tree.add(3, "Paul")
Expand All @@ -12,111 +12,112 @@
tree.add(25, "Kari")
tree
}

describe "add and find" do
it "add & find values" do
tree.add(5, "Peter")
expect(tree.find(5)).must_equal "Peter"

tree.add(15, "Ada")
expect(tree.find(15)).must_equal "Ada"

tree.add(3, "Paul")
expect(tree.find(3)).must_equal "Paul"
end

it "can't find anything when the tree is empty" do
expect(tree.find(50)).must_be_nil
end
end

describe "inorder" do
it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
end

it "will return the tree in order" do
expect(tree_with_nodes.inorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end


describe "preorder" do
it "will give an empty array for an empty tree" do
expect(tree.preorder).must_equal []
end

it "will return the tree in preorder" do
expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

describe "postorder" do
it "will give an empty array for an empty tree" do
expect(tree.postorder).must_equal []
end

it "will return the tree in postorder" do
expect(tree_with_nodes.postorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"},
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
end
end

describe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end

it "will return an array of a level-by-level output of the tree" do
expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

describe "height" do
it "will return 0 if tree is empty" do
expect(tree.height()).must_equal 0
end

it "will return the nuber of nodes in the longest path" do
expect(tree_with_nodes.height).must_equal 4
tree_with_nodes.add(60, "sam")
tree_with_nodes.add(58, "penny")
tree_with_nodes.add(65, "sam")
expect(tree_with_nodes.height).must_equal 6
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

it "will give the correct height of a binary search tree" do
tree_with_nodes.add(30, "Tatiana")
expect(tree_with_nodes.height).must_equal 5
end
end

describe "delete" do
it "can delete a note in the tree" do
# Arrange & Assert
expect(tree_with_nodes.find(15)).must_equal "Ada"

# Act
tree_with_nodes.delete(15)

# Assert
expect(tree_with_nodes.find(15)).must_be_nil
end

it "will return nil if the node is not in the tree when it's deleted" do
# Arrange & Act
answer = tree_with_nodes.delete(47)

# Assert
expect(answer).must_be_nil
expect(tree_with_nodes.find(47)).must_be_nil
end
end
end

describe "preorder" do
it "will give an empty array for an empty tree" do
expect(tree.preorder).must_equal []
end

it "will return the tree in preorder" do
expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

describe "postorder" do
it "will give an empty array for an empty tree" do
expect(tree.postorder).must_equal []
end

it "will return the tree in postorder" do
expect(tree_with_nodes.postorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"},
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
end
end

xdescribe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end

it "will return an array of a level-by-level output of the tree" do
expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

describe "height" do
it "will return 0 if tree is empty" do
expect(tree.height()).must_equal 0
end

it "will return the number of nodes in the longest path" do
expect(tree_with_nodes.height).must_equal 4
tree_with_nodes.add(60, "sam")
tree_with_nodes.add(58, "penny")
tree_with_nodes.add(65, "sam")
expect(tree_with_nodes.height).must_equal 6
end

it "will give the correct height of a binary search tree" do
tree_with_nodes.add(30, "Tatiana")
expect(tree_with_nodes.height).must_equal 5
end
end

describe "delete" do
it "can delete a node in the tree" do
# Arrange & Assert
expect(tree_with_nodes.find(15)).must_equal "Ada"

# Act
tree_with_nodes.delete(15)

# Assert
expect(tree_with_nodes.find(15)).must_be_nil
end

it "will return nil if the node is not in the tree when it's deleted" do
# Arrange & Act
answer = tree_with_nodes.delete(47)

# Assert
expect(answer).must_be_nil
expect(tree_with_nodes.find(47)).must_be_nil
end
end
end