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

Fire - blaine #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
132 changes: 111 additions & 21 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ 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
Expand All @@ -16,42 +16,132 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n)
# Space Complexity: [O(1)-if it's a loop] - why do I wanna say O(n)?? help me.
# Try with a while loop
def add(key, value = nil)
Comment on lines +19 to +22

Choose a reason for hiding this comment

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

👍 The time complexity is O(n) if the tree is balanced and O(log n) if it is balanced.
Since you're doing recursion in add_helper the space complexity is the same as the time.

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

# Time Complexity:
# Space Complexity:
def add_helper(current, key, value)
# if current = return TreeNode.new
if key < current.key
# if current.left is nil place the new node
# otherwise make current.left the new current and run through the same checks
if current.left.nil?
current.left = TreeNode.new(key, value)
return
else
add_helper(current.left, key, value)
end
else
if current.right.nil?
current.right = TreeNode.new(key, value)
return
else
add_helper(current.right, key, value)
end
end
end

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +51 to 53

Choose a reason for hiding this comment

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

👍 Ditto on space/time complexity

raise NotImplementedError
return find_helper(@root, key)
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
# left-root-right
def inorder
Comment on lines +71 to 74

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# current = @root
inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, values) # Values is an array
if current.nil?
return values
end
inorder_helper(current.left, values)
values.push({:key=>current.key, :value=>current.value})
inorder_helper(current.right, values)
return values
end


# Time Complexity: O(n)
# Space Complexity: O(n)
# root-left-right
def preorder
Comment on lines +90 to 93

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, values)
if current.nil?
return values
end
values.push({:key=>current.key, :value=>current.value})
preorder_helper(current.left, values)
preorder_helper(current.right, values)
return values
end

# Time Complexity: O(n)
# Space Complexity: O(n)
# left-right-root
def postorder
Comment on lines +107 to 110

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, values)
if current.nil?
return values
end
postorder_helper(current.left, values)
postorder_helper(current.right, values)
values.push({:key=>current.key, :value=>current.value})
return values
end

# Time Complexity: O(n)^2
# Space Complexity: O(n)
def height
Comment on lines +124 to 126

Choose a reason for hiding this comment

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

👍 The time complexity is O(n) (not n^2)

The space complexity is O(log n) if the tree is balanced or O(n) if it's not. The space complexity is basically the height of the tree.

raise NotImplementedError
# raise NotImplementedError
height_helper(@root)
end
def height_helper(current)
if current.nil?
return 0
end
left_height = height_helper(current.left)
right_height = height_helper(current.right)
if left_height > right_height
return left_height + 1
else
return right_height + 1
end
end


# **********************************************************************
# Optional Method
# Time Complexity:
# Space Complexity:
Expand Down
17 changes: 9 additions & 8 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'test_helper'
require 'pry'


Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Expand Down Expand Up @@ -39,8 +40,8 @@
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"}]
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

Expand All @@ -64,20 +65,20 @@

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"}]
{:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"},
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
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"}]
{:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end

Expand All @@ -96,7 +97,7 @@
end

it "will report the height for a balanced tree" do
expect(tree_with_nodes.height).must_equal 3
expect(tree_with_nodes.height).must_equal 4
end

it "will report the height for unbalanced trees" do
Expand Down