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

Alex #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
143 changes: 120 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,158 @@ 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) where n is the height of the tree
# or O(log n) if the tree is balanced
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
new_tree_node = TreeNode.new(key, value)

if @root.nil?
@root = new_tree_node
else
current = @root
while current
if new_tree_node.key <= current.key
if !current.left
current.left = new_tree_node
return
else
current = current.left
end
else
if !current.right
current.right = new_tree_node
return
else
current = current.right
end
end
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) if tree is balanced, n is size of tree
# O(n) in worst case where n is also size of tree
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if @root.nil?

current = @root
while current
if key > current.key
current = current.right
elsif key < current.key
current = current.left
else
return current.value
end
end

return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) where n is the height of the tree
# Space Complexity: O(n) where n is the height of the tree
# potentially O(n^2) with the arrays?
def inorder
raise NotImplementedError
array = []
return [] if @root.nil?
current = @root

return inorder_recursive(current, array)
end

def inorder_recursive(current, array)
if !current
return array
else
inorder_recursive(current.left, array)
array << {:key => current.key, :value => current.value}
inorder_recursive(current.right, array)
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) where n is the height of the tree
# Space Complexity: O(n) where n is the height of the tree
def preorder
raise NotImplementedError
array = []
return [] if @root.nil?
current = @root

return preorder_recursive(current, array)
end

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

# Time Complexity: O(n) where n is the height of the tree
# Space Complexity: O(n) where n is the height of the tree
def postorder
raise NotImplementedError
array = []
return [] if @root.nil?
current = @root

return postorder_recursive(current, array)
end

def postorder_recursive(current, array)
if !current
return array
else
postorder_recursive(current.left, array)
postorder_recursive(current.right, array)
array << {:key => current.key, :value => current.value}
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) where n is the size of the tree
# Space Complexity: O(n)
def height
raise NotImplementedError
return 0 if @root.nil?
current = @root
height_recursive(current)
end

def height_recursive(current)
return 0 if current.nil?

left_side = height_recursive(current.left)
right_side = height_recursive(current.right)

if left_side < right_side
return (right_side + 1)
else
return (left_side + 1)
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
return [] if @root.nil?
end

# Useful for printing
Expand Down
33 changes: 15 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,9 +60,9 @@
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

Expand All @@ -75,9 +72,9 @@
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"}]
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