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

Leaves - Samantha #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
120 changes: 91 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,126 @@
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
attr_reader :root
def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(1)
def add_helper(current_node, key, value)

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's not.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

return TreeNode.new(key, value) if current_node.nil?

if key < current_node.key
current_node.left = add_helper(current_node.left, key, value)
else
current_node.right = add_helper(current_node.right, key, value)
end

return current_node
end

def add(key, value)
raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(1)
def find_helper(current_node, key)

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's not.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

return nil if current_node.nil?

if key > current_node.key
current_node.right = find_helper(current_node.right, key)
elsif key < current_node.key
current_node.left = find_helper(current_node.left, key)
else
return current_node.value
end
end

def find(key)
raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

def inorder_helper(current_node, list)
return list if current_node.nil?

inorder_helper(current_node.left, list)
list << {key: current_node.key, value: current_node.value}

inorder_helper(current_node.right, list)
return list
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return pre_order_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

def pre_order_helper(current_node, list)
return list if current_node.nil?

list << {key: current_node.key, value: current_node.value}
pre_order_helper(current_node.left, list)
pre_order_helper(current_node.right, list)
return list
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return post_order_helper(@root, [])
end

# Time Complexity:
# Space Complexity:

def post_order_helper(current_node, list)
return list if current_node.nil?

post_order_helper(current_node.left, list)
post_order_helper(current_node.right, list)
list << {key: current_node.key, value: current_node.value}
return list
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def height_helper(current_node)

Choose a reason for hiding this comment

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

The space complexity here depends on the recursive calls which depend on the height of the tree. So the space complexity is O(log n) for balanced trees and O(n) for unbalanced ones.

Otherwise the method is quite well done! 😃

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

return 0 if current_node.nil?

if height_helper(current_node.right) > height_helper(current_node.left)
return height_helper(current_node.right) + 1
else
return height_helper(current_node.left) + 1
end
end

def height
raise NotImplementedError
return height_helper(@root)
end

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

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
196 changes: 98 additions & 98 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,121 @@

describe Tree do
let (:tree) {Tree.new}

let (:tree_with_nodes) {
tree.add(5, "Peter")
tree.add(3, "Paul")
tree.add(1, "Mary")
tree.add(10, "Karla")
tree.add(15, "Ada")
tree.add(25, "Kari")
tree
}

describe "add and find" do
it "add & find values" do
tree.add(5, "Peter")
tree.add(3, "Paul")
tree.add(1, "Mary")
tree.add(10, "Karla")
expect(tree.find(5)).must_equal "Peter"

tree.add(15, "Ada")
tree.add(25, "Kari")
tree
}

describe "add and find" do
it "add & find values" do
tree.add(5, "Peter")
expect(tree.find(5)).must_equal "Peter"

tree.add(15, "Ada")
expect(tree.find(15)).must_equal "Ada"

tree.add(3, "Paul")
expect(tree.find(3)).must_equal "Paul"
end

it "can't find anything when the tree is empty" do
expect(tree.find(50)).must_be_nil
end
expect(tree.find(15)).must_equal "Ada"

tree.add(3, "Paul")
expect(tree.find(3)).must_equal "Paul"
end

describe "inorder" do
it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
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"}]
end

it "can't find anything when the tree is empty" do
expect(tree.find(50)).must_be_nil
end
end

describe "inorder" do
it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
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"}]
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"}]
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"}]
end
end

describe "postorder" do
it "will give an empty array for an empty tree" do
expect(tree.postorder).must_equal []
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"}]
end
describe "postorder" do
it "will give an empty array for an empty tree" do
expect(tree.postorder).must_equal []
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"}]
end
end

describe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end
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"}]
end
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"}]
end
describe "height" do
it "will return 0 if tree is empty" do
expect(tree.height()).must_equal 0
end

describe "height" do
it "will return 0 if tree is empty" do
expect(tree.height()).must_equal 0
end
it "will return the nuber of nodes in the longest path" do
expect(tree_with_nodes.height).must_equal 4
tree_with_nodes.add(60, "sam")
tree_with_nodes.add(58, "penny")
tree_with_nodes.add(65, "sam")
expect(tree_with_nodes.height).must_equal 6
end

it "will give the correct height of a binary search tree" do
tree_with_nodes.add(30, "Tatiana")
expect(tree_with_nodes.height).must_equal 5
end
end

it "will return the nuber of nodes in the longest path" do
expect(tree_with_nodes.height).must_equal 4
tree_with_nodes.add(60, "sam")
tree_with_nodes.add(58, "penny")
tree_with_nodes.add(65, "sam")
expect(tree_with_nodes.height).must_equal 6
end
xdescribe "delete" do
it "can delete a note in the tree" do
# Arrange & Assert
expect(tree_with_nodes.find(15)).must_equal "Ada"

# Act
tree_with_nodes.delete(15)

it "will give the correct height of a binary search tree" do
tree_with_nodes.add(30, "Tatiana")
expect(tree_with_nodes.height).must_equal 5
end
# Assert
expect(tree_with_nodes.find(15)).must_be_nil
end

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

# Act
tree_with_nodes.delete(15)

# Assert
expect(tree_with_nodes.find(15)).must_be_nil
end

it "will return nil if the node is not in the tree when it's deleted" do
# Arrange & Act
answer = tree_with_nodes.delete(47)

# Assert
expect(answer).must_be_nil
expect(tree_with_nodes.find(47)).must_be_nil
end

it "will return nil if the node is not in the tree when it's deleted" do
# Arrange & Act
answer = tree_with_nodes.delete(47)

# Assert
expect(answer).must_be_nil
expect(tree_with_nodes.find(47)).must_be_nil
end
end
end