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
122 changes: 102 additions & 20 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,127 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn) if the tree is balanced (best case), O(n) if tree is unbalanced (worst case)

Choose a reason for hiding this comment

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

👍

# Space Complexity: O(1)
def add(key, value)

Choose a reason for hiding this comment

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

This method could use a bit of a refactor.

raise NotImplementedError
if !@root
new_node = TreeNode.new(key, value)
@root = new_node
return
end

current = @root
new_node = TreeNode.new(key, value)

until !current
if new_node.key <= current.key # GO LEFT
if !current.left
current.left = new_node
return
else
current = current.left
end
else # new_node.value > current.value # GO RIGHT
if !current.right
current.right = new_node
return
else
current = current.right
end
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logn) where n is the length of the array if the tree is balanced. O(n) if not.
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if !@root
current = @root

until current == nil
return current.value if key == current.key
key < current.key ? current = current.left : current = current.right
end

end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n-squared)
# Space Complexity: O(n) where n is the height of the recursion stack
def inorder
raise NotImplementedError
@output = []
current = @root

return @output if !@root

def analyzeInorder(current)

Choose a reason for hiding this comment

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

An inner method? I suggest either putting this method in TreeNode or making it a private helper method. Output doesn't need to be an instance variable.

analyzeInorder(current.left) if current.left
@output << {:key => current.key, :value => current.value}
analyzeInorder(current.right) if current.right
end

analyzeInorder(current)

return @output
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n-squared)

Choose a reason for hiding this comment

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

You hit each node just once, so I would say O(n) instead.

# Space Complexity: O(n) where n is the height of the recursion stack
def preorder
raise NotImplementedError
@output = []
current = @root

return @output if !@root

def analyzePreorder(current)
@output << {:key => current.key, :value => current.value}
analyzePreorder(current.left) if current.left
analyzePreorder(current.right) if current.right
end

analyzePreorder(current)

return @output
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n-squared)
# Space Complexity: O(n) where n is the height of the recursion stack
def postorder
raise NotImplementedError
@output = []
current = @root

return @output if !@root

def analyzePostorder(current)
analyzePostorder(current.left) if current.left
analyzePostorder(current.right) if current.right
@output << {:key => current.key, :value => current.value}
end

analyzePostorder(current)

return @output
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) if tree is balanced, O(n) if unbalanced
# Space Complexity: O(1)
def height
raise NotImplementedError
return 0 if !@root

def findMaxHeight(node)
return 0 if !node

height_left = findMaxHeight(node.left)
height_right = findMaxHeight(node.right)

return height_left > height_right ? (height_left + 1) : (height_right + 1)
end

findMaxHeight(@root)

Choose a reason for hiding this comment

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

Just for readability adding a return here would be good.

end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 24 additions & 11 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/skip_dsl'
require_relative 'test_helper'


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

describe Tree do
Expand All @@ -10,7 +11,9 @@
tree.add(5, "Peter")
tree.add(3, "Paul")
tree.add(1, "Mary")
# tree.add(4, "Elle")
tree.add(10, "Karla")
# tree.add(9, "Brad")
tree.add(15, "Ada")
tree.add(25, "Kari")
tree
Expand Down Expand Up @@ -38,8 +41,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"},
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
Expand All @@ -51,8 +54,8 @@
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"},
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 +66,31 @@
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"},
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 "breadth first search" do
describe "height" do
it "returns 0 for an empty tree" do
expect(tree.height).must_equal 0
end

it "returns correct height of tree" do
expect(tree_with_nodes.height).must_equal 4
end
end

xdescribe "breadth first search" do # optional
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"},
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