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

Branches - Erika #45

Open
wants to merge 9 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
Empty file added lib/pract.rb
Empty file.
160 changes: 132 additions & 28 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,87 @@ def initialize(key, val)
@left = nil
@right = nil
end

def add(key, value)

Choose a reason for hiding this comment

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

Nice adding these methods to the TreeNode class.


if key <= @key
return @left = TreeNode.new(key, value) if @left.nil?
@left.add(key, value)
else
return @right = TreeNode.new(key, value) if @right.nil?
@right.add(key, value)
end

end

def find(key)

return @value if @key == key

if key > @key
return nil if @right.nil?
@right.find(key)
else
return nil if @left.nil?
@left.find(key)
end

end

def inorder

inorder_array = []

inorder_array += @left.inorder unless @left.nil?
inorder_array << { key: @key, value: @value }
inorder_array += @right.inorder unless @right.nil?

return inorder_array

end

def preorder

preorder_array = []

preorder_array << { key: @key, value: @value }
preorder_array += @left.preorder unless left.nil?
preorder_array += @right.preorder unless right.nil?


return preorder_array

end

def postorder

postorder_array = []

postorder_array += @left.postorder unless left.nil?
postorder_array += @right.postorder unless right.nil?
postorder_array << { key: @key, value: @value }

return postorder_array

end

def height

return 1 if @left.nil? && @right.nil?

if @left.nil?
@right.height + 1
elsif @right.nil?
@left.height + 1
else
if @left.height >= @right.height
@left.height + 1
else
@right.height + 1
end
end

end
end

class Tree
Expand All @@ -16,51 +97,74 @@ def initialize
@root = nil
end

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

Choose a reason for hiding this comment

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

👍 , time complexity is right if the tree is balanced.

raise NotImplementedError

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

return @root.add(key, value)

end

# Time Complexity:
# Space Complexity:

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

Choose a reason for hiding this comment

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

👍 , time complexity is right if the tree is balanced.

raise NotImplementedError

return nil if @root.nil?
return @root.find(key)

end

# Time Complexity:
# Space Complexity:
# # Time Complexity: O(n)
# # Space Complexity: O(n)
def inorder
Comment on lines +122 to 124

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

return [] if @root.nil?
return @root.inorder

end


# Time Complexity:
# Space Complexity:
# # Time Complexity: O(n)
# # Space Complexity: O(n)
def preorder
Comment on lines +132 to 134

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

return [] if @root.nil?
return @root.preorder

end

# Time Complexity:
# Space Complexity:
# # Time Complexity: O(n)
# # Space Complexity: O(n)
def postorder
Comment on lines +141 to 143

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

return [] if @root.nil?
return @root.postorder

end

# Time Complexity:
# Space Complexity:
# # Time Complexity:
# # Space Complexity:
def height
Comment on lines +150 to 152

Choose a reason for hiding this comment

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

👍 , time & space complexity?

raise NotImplementedError
end

return 0 if @root.nil?
return @root.height

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end

# Useful for printing
def to_s
return "#{self.inorder}"
end
# # Optional Method
# # Time Complexity:
# # Space Complexity:
# def bfs
# raise NotImplementedError
# end

# # Useful for printing
# def to_s
# return "#{self.inorder}"
# end
end
14 changes: 8 additions & 6 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'pry'

require_relative "test_helper"

describe Tree do
Expand Down Expand Up @@ -37,8 +39,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 Down Expand Up @@ -67,15 +69,15 @@
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 @@ -98,7 +100,7 @@
end
end

describe "delete" do
xdescribe "delete" do
it "can delete a note in the tree" do
# Arrange & Assert
expect(tree_with_nodes.find(15)).must_equal "Ada"
Expand Down