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
105 changes: 76 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,108 @@ 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(key, value)
raise NotImplementedError
# Time Complexity: O(log n) if the tree is balanced, O(n) if the tree is unbalanced
# - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(1) - constant
def add(key, value, current = @root)
new_node = TreeNode.new(key, value)

if @root
if key <= current.key
current.left ? add(key, value, current.left) : current.left = new_node
else
current.right ? add(key, value, current.right) : current.right = new_node
end
else
@root = new_node
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
# Time Complexity: O(log n) if the tree is balance, O(n) is the tree is unbalanced
# - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(1) - constant
def find(key, current = @root)
if current
if current.key == key
return current.value
elsif key <= current.key
find(key, current.left)
elsif key > current.key
find(key, current.right)
end
else
return
end
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
# Time Complexity: O(n) - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(n) where n is equal to the number of TreeNodes in the tree
def inorder(current = @root, array = [])
if current
inorder(current.left, array)
array << { key: current.key, value: current.value }
inorder(current.right, array)
else
return array
end
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
# Time Complexity: O(n) - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(n) where n is equal to the number of TreeNodes in the tree
def preorder(current = @root, array = [])
if current
array << { key: current.key, value: current.value }
preorder(current.left, array)
preorder(current.right, array)
else
return array
end
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
# Time Complexity: O(n) - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(n) where n is equal to the number of TreeNodes in the tree
def postorder(current = @root, array = [])
if current
postorder(current.left, array)
postorder(current.right, array)
array << { key: current.key, value: current.value }
else
return array
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
# Time Complexity: O(n) - where n is equal to the number of TreeNodes in the tree
# Space Complexity: O(1) - constant
def height(current = @root)
if current
left_height = height(current.left)
right_height = height(current.right)

return (left_height > right_height ? left_height : right_height) + 1
else
return 0
end
end

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

# Useful for printing
Expand Down
44 changes: 26 additions & 18 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require_relative 'test_helper'

require_relative "test_helper"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

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

let (:tree_with_nodes) {
tree.add(5, "Peter")
Expand Down Expand Up @@ -37,23 +36,21 @@
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"}]
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"}]
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

Expand All @@ -63,21 +60,32 @@
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"}]
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 "height" do
it "will return 0 for an empty tree" do
expect(tree.height).must_equal 0
end
it "will return the height of the 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
skip
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"}]
skip
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
end
end