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
154 changes: 130 additions & 24 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,47 +16,153 @@ 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
Copy link
Copy Markdown

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

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:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +51 to 53
Copy link
Copy Markdown

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

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:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
# left-root-right
def inorder
Comment on lines +71 to 74
Copy link
Copy Markdown

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

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:
# Space Complexity:

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

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
Copy link
Copy Markdown

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

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:
# Space Complexity:
# Time Complexity: O(n)^2
# Space Complexity: O(n)
def height
Comment on lines +124 to 126
Copy link
Copy Markdown

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:
# Time Complexity: O(n) Does shift add time? O(n)^2?
# Space Complexity: O(n)
def bfs
raise NotImplementedError
return [] if @root.nil?

to_be_chkd = [@root]
output = []

while to_be_chkd.length > 0
current = to_be_chkd.shift
# argument
output.push({:key=>current.key, :value=>current.value})
if current.left != nil
to_be_chkd.push(current.left)
end
if current.right != nil
to_be_chkd.push(current.right)
end
end
return output
end

# Useful for printing
Expand Down
15 changes: 8 additions & 7 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,8 +65,8 @@

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

Expand All @@ -76,8 +77,8 @@

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