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

Shirley #27

Open
wants to merge 2 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
145 changes: 122 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,162 @@ 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
@parent = nil
end
end

class Tree
attr_reader :root

def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn)
# Space Complexity: O(1)

Choose a reason for hiding this comment

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

Since you're using a recursive add_node helper method, you are using the system stack and this impacts space complexity. Since you are making O(log n) recursive calls on a balanced tree this would have a space complexity of O(log n).

def add(key, value)
raise NotImplementedError
new_node = TreeNode.new(key, value)
return @root = new_node unless @root
add_node(@root, new_node)
end

def add_node(root, new_node)
if new_node.key < root.key
return root.left = new_node unless root.left
add_node(root.left, new_node)
elsif new_node.key > root.key
return root.right = new_node unless root.right
add_node(root.right, new_node)
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) / O(logn)
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return find_helper(key, @root)
end

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

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
raise NotImplementedError
array = []
inorder_helper(array, @root)
end

def inorder_helper(array, node)
return array unless node
inorder_helper(array, node.left)
node_kv = {}
node_kv[:key] = node.key
node_kv[:value] = node.value
array.push(node_kv)
inorder_helper(array, node.right)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder
raise NotImplementedError
array = []
return preorder_helper(array, @root)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(array, node)
return array unless node
node_kv = {}
node_kv[:key] = node.key
node_kv[:value] = node.value
array.push(node_kv)
preorder_helper(array, node.left)
preorder_helper(array, node.right)
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
array = []
postorder_helper(@root, array)
return array
end

def postorder_helper(root, array)
return unless root
postorder_helper(root.left, array)
postorder_helper(root.right, array)
array.push({ key: root.key, value: root.value })
end

# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
height_counter(@root)
end

def height_counter(node)
return 0 unless node
left_counter = height_counter(node.left)
right_counter = height_counter(node.right)
counter = left_counter > right_counter ? left_counter : right_counter

Choose a reason for hiding this comment

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

👍

return counter += 1
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(2n)

Choose a reason for hiding this comment

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

In Big-O we drop the coefficient so this should be Space complexity of O(n)


# i know there will be less lines of code if q is implemented, please see commented psuedocode below for q implementation
def bfs
raise NotImplementedError
return [] unless @root
array = [@root]
i = 0
current = @root
length = 1
until (i == length - 1) && (current.left == nil) && (current.right == nil)
if current.left
array.push(current.left)
length += 1
end
if current.right
array.push(current.right)
length += 1
end
i += 1
current = array[i]
end
return array.map do |node|

Choose a reason for hiding this comment

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

Good use of map

{ key: node.key, value: node.value }
end
end

# BFS USING QUEUE
# Time Complexity: O(n)
# Space Complexity: O(2n)
# def bfs
# q = Queue.new(@root)
# array = []
# until q is_empty
# current = q.dequeue
# array.push({key: current.key, value: current.value })
# q.enqueue(current.left) if current.left
# q.enqueue(current.right) if current.right
# end
# return array
# end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
require 'minitest/autorun'
require 'minitest/reporters'

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

require_relative '../lib/tree'
34 changes: 16 additions & 18 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 "pry"

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,12 +61,12 @@
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 "returns 0 for an empty tree" do
expect(tree.height).must_equal 0
Expand All @@ -85,9 +83,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