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
114 changes: 91 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,127 @@ 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:
# Time Complexity: O(n) is worst case if tree is not balanced where n is the height of the tree
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root == nil
@root = new_node
return
end

current = @root

while current != nil
if current.key >= key
break if current.left == nil

Choose a reason for hiding this comment

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

This works, but you could streamline the if statements to insert the node here I think.

current = current.left
else
break if current.right == nil
current = current.right
end
end

current.key >= key ? current.left = new_node : current.right = new_node
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) is worst case if tree is not balanced where n is the height of the tree
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
current = @root

while current != nil
if current.key > key
current = current.left
elsif current.key < key
current = current.right
else
return current.value
end
end
return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) where n is the number of nodes in the tree
# Space Complexity: O(n) where n is the number of nodes in the tree
def inorder
raise NotImplementedError
return inorder_recursion(current = @root, tree = [])
end

# Time Complexity:
# Space Complexity:
def inorder_recursion(current, tree)
if current != nil
inorder_recursion(current.left, tree)
tree << { key: current.key, value: current.value }
inorder_recursion(current.right, tree)
else
return tree
end
end

# Time Complexity: O(n) where n is the number of nodes in the tree
# Space Complexity: O(n) where n is the number of nodes in the tree
def preorder
raise NotImplementedError
return preorder_recursion(current = @root, tree = [])
end

# Time Complexity:
# Space Complexity:
def preorder_recursion(current, tree)
if current != nil
tree << { key: current.key, value: current.value }
preorder_recursion(current.left, tree)
preorder_recursion(current.right, tree)
else
return tree
end
end

# Time Complexity: O(n) where n is the number of nodes in the tree
# Space Complexity: O(n) where n is the number of nodes in the tree xs4
def postorder
raise NotImplementedError
return postorder_recursion(current = @root, tree = [])
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
def postorder_recursion(current, tree)
if current != nil
postorder_recursion(current.left, tree)
postorder_recursion(current.right, tree)
tree << { key: current.key, value: current.value }
else
return tree
end
end

# Time Complexity: O(n) where n is the number of nodes in the tree
# Space Complexity: O(1)
def height(current = @root, current_height = 0, max_height = 0)
return max_height if current == nil

max_height = current_height if current_height > max_height
max_height = height(current.left, current_height += 1, max_height)

Choose a reason for hiding this comment

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

Nice use of tail recursion

current_height -= 1
max_height = height(current.right, current_height += 1, max_height)

return max_height
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end
Expand Down
51 changes: 29 additions & 22 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,31 @@
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 "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
describe "height" do
it "will find the height of a tree" do
expect(tree_with_nodes.height()).must_equal 3
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"}]
it "will return 0 for an empty tree" do
expect(tree.height()).must_equal 0
end
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
end