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

Angela #6

Open
wants to merge 1 commit 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
127 changes: 104 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,142 @@ 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(logn) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data

Choose a reason for hiding this comment

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

It will be O(log n) if the tree is balanced but O(n) if the tree is totally unbalanced.

# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)
@root = new_node if @root.nil?
current = @root
add_helper(current, new_node)
end

# Time Complexity:
# Space Complexity:
def add_helper(current, new_node)
if current.nil?
return current = new_node
elsif new_node.key < current.key
current.left = add_helper(current.left, new_node)
elsif new_node.key > current.key
current.right = add_helper(current.right, new_node)
end
return current
end

# Time Complexity: O(logn) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
current = @root
return nil if current.nil?
return current.value if current.key == key
find_helper(current, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(current, key)
if key == current.key
return current.value
elsif key < current.key
current.left = find_helper(current.left, key)
else
current.right = find_helper(current.right, key)
end
end

# Time Complexity: O(n) because each node will need to be visited and added to the final array
# Space Complexity: O(n) because the final array will contain the size of the tree
# left, root, right
def inorder
raise NotImplementedError
array = []
return array if @root.nil?
inorder_helper(array, @root)
return array
end

def inorder_helper(array, node)
return if node.nil?
inorder_helper(array, node.left)
hash = { key: node.key, value: node.value }
array.push(hash)
inorder_helper(array, node.right)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) because each node will need to be visited and added to the final array
# Space Complexity: O(n) because the final array will contain the size of the tree
# root, left, right
def preorder
raise NotImplementedError
return [] if @root.nil?
array = []
preorder_helper(@root, array)
return array
end

# Time Complexity:
# Space Complexity:
def preorder_helper(node, array)
return if node.nil?
array.push({ key: node.key, value: node.value })
preorder_helper(node.left, array)
preorder_helper(node.right, array)
end

# Time Complexity: O(n) because each node will need to be visited and added to the final array
# Space Complexity: O(n) because the final array will contain the size of the tree
# left, right, root
def postorder
raise NotImplementedError
array = []
return array if @root.nil?
postorder_helper(array, @root)
return array
end

def postorder_helper(array, node)
return if node.nil?
postorder_helper(array, node.left)
postorder_helper(array, node.right)
# hash = { key: node.key, value: node.value }
array.push({ key: node.key, value: node.value })
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) because worst case each node will need to be visited to be counted
# Space Complexity: O(1)
def height
raise NotImplementedError
return 0 if @root.nil?
node = @root
return height_helper(@root)
end

def height_helper(node)
return 0 if node.nil?
left = height_helper(node.left)
right = height_helper(node.right)

if left >= right
return left + 1
else
return right + 1
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
array = []

Choose a reason for hiding this comment

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

Well it's a start!

return array if @root.nil?
end

def bfs_helper(node, level, array)
end

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

require_relative "test_helper"
require "minitest/skip_dsl"

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 +37,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 +61,35 @@
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 if tree is empty" 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
tree_with_nodes.add(2, "bob")
expect(tree_with_nodes.height).must_equal 4
tree_with_nodes.add(50, "angela")
expect(tree_with_nodes.height).must_equal 5
end
end

describe "breadth first search" do
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"}]
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