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

Earth/Taylor #33

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
114 changes: 88 additions & 26 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative '../../stacks-queues/lib/queue'

class TreeNode
attr_reader :key, :value
attr_accessor :left, :right
Expand All @@ -11,52 +13,112 @@ def initialize(key, val)
end

class Tree
attr_reader :root
attr_reader :root, :size
def initialize
@root = nil
@size = 0
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(1)
def add(key, value)
Comment on lines +22 to 24

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
if @root.nil?
@root = TreeNode.new(key, value)
else
current = @root
prev = @root

while current
prev = current
if key < current.key
current = current.left
else
current = current.right
end
end
if key < prev.key
prev.left = TreeNode.new(key, value)
else
prev.right = TreeNode.new(key, value)
end
end
@size += 1
end

# Time Complexity:
# Space Complexity:
# Time Complexity: worst case: O(n), best case: O(log n)
# Space Complexity: O(1)
def find(key)
Comment on lines +48 to 50

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @root
while current
if current.key == key
return current.value
elsif key > current.key
current = current.right
else
current = current.left
end
end
return nil
end

# Time Complexity:
# Space Complexity:
def inorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder(array=[], current=@root)
Comment on lines +64 to +66

Choose a reason for hiding this comment

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

👍

return array if current == nil
inorder(array, current.left)
array << {key: current.key, value: current.value}
inorder(array, current.right)
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder(array=[],current=@root)
Comment on lines +73 to +75

Choose a reason for hiding this comment

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

👍

return array if current.nil?
array << {key: current.key, value: current.value}
preorder(array, current.left)
preorder(array, current.right)
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder(array=[], current=@root)
Comment on lines +82 to +84

Choose a reason for hiding this comment

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

👍

return array if current.nil?
postorder(array, current.left)
postorder(array, current.right)
array << {key: current.key, value: current.value}
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
# Time Complexity: O(n)
# Space Complexity: O(n)
def height(current=@root)
Comment on lines +91 to +93

Choose a reason for hiding this comment

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

👍 The space complexity is O(log n) if the tree is balanced and O(n) if it isn't.

return 0 if current.nil?
return 1 if current.right.nil? && current.left.nil?

left_count = 0
right_count = 0
left_count += 1 + height(current.left)
right_count += 1 + height(current.right)
left_count > right_count ? left_count : right_count
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def bfs
Comment on lines +105 to 107

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
array = []
return array if @root.nil?
queue = [@root]
while !queue.empty?
current = queue.shift
array << {key: current.key, value: current.value}
unless current.left.nil?
queue.push(current.left)
end
unless current.right.nil?
queue.push(current.right)
end
end
return array
end

# Useful for printing
Expand Down
22 changes: 11 additions & 11 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
it "will return 1 for a tree of height 1" do
my_tree = Tree.new

my_tree.add(100)
my_tree.add(100, "bruno")
expect(my_tree.height).must_equal 1
end

Expand All @@ -102,23 +102,23 @@
it "will report the height for unbalanced trees" do
my_tree = Tree.new

my_tree.add(100)
my_tree.add(110)
my_tree.add(120)
my_tree.add(130)
my_tree.add(140)
my_tree.add(100, "bella")
my_tree.add(110, "rocky")
Comment on lines +105 to +106

Choose a reason for hiding this comment

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

This works, but you can also make the value an optional parameter.

my_tree.add(120, "snowball")
my_tree.add(130, "mr. whiskers")
my_tree.add(140, "bebe")

expect(my_tree.height).must_equal 5

my_tree = Tree.new

my_tree = Tree.new

my_tree.add(100)
my_tree.add(90)
my_tree.add(80)
my_tree.add(70)
my_tree.add(60)
my_tree.add(100, "bruno")
my_tree.add(90, "bella")
my_tree.add(80, "arbor")
my_tree.add(70, "teal")
my_tree.add(60, "josh")

expect(my_tree.height).must_equal 5
end
Expand Down