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_Ga-Young #48

Open
wants to merge 4 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Tree Exercise

This project is due **Monday September 2nd, 2019**

In this exercise you will implement, in Ruby, several Tree methods.

- `add(value)` - This method adds a value to the Binary Search Tree
Expand Down
164 changes: 135 additions & 29 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,170 @@
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:

# a recursive helper method for adding a node to a binary search tree
# def add_helper(current_node, key, value)
# 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
# end

# Time Complexity: O(log n) where n is number of nodes ?
# Space Complexity: O(n) where n is number of nodes ?
def add(key, value)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

Since you're not add n nodes to the tree the space complexity is O(1).

raise NotImplementedError

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

while true

if key <= current.key

if !current.left.nil?
current = current.left
else
current.left = TreeNode.new(key, value)
return
end

else

if !current.right.nil?
current = current.right
else
current.right = TreeNode.new(key, value)
return
end
end
end
end

# if using the recursive helper method instead
# @root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) where n is number of nodes ?
# Space Complexity: O(1) or is this N/A?
def find(key)
Comment on lines +66 to 68

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?
return nil
else
current = @root
while true
if key == current.key
return current.value
elsif key < current.key
current = current.left
else
current = current.right
end
end
end
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) where n is number of nodes
# Space Complexity: O(n) where n is number of nodes
def inorder
Comment on lines +96 to 98

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 preorder_helper(current_node, list)
return list if current_node.nil?

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

# Time Complexity: O(n) where n is number of nodes
# Space Complexity: O(n) where n is number of nodes
def preorder
Comment on lines +111 to 113

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:


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

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

# Time Complexity: O(n) where n is number of nodes
# Space Complexity: O(n) where n is number of nodes
def postorder
Comment on lines +126 to 128

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:



def height_helper(current)
return 0 if current.nil?

left_height = height_helper(current.left)
right_height = height_helper(current.right)

return [left_height, right_height].max + 1
end

# Time Complexity: O(n) where n is number of nodes
# Space Complexity: O(1)
def height
Comment on lines +143 to 145

Choose a reason for hiding this comment

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

Since height_helper is recursive, this is O(log n) for space complexity. Since you'll have log n recursive calls on the stack at any one time.

raise NotImplementedError
return height_helper(@root)
end

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

# list = []
# return list if @root.nil?
# queue = [@root]

# until queue.empty
# current = queue.shift
# queue.push(current.left) unless current.left.nil?
# queue.push(current.right) unless current.right.nil?

# list << { key: current.key, value: current.value }
# end
end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
109 changes: 60 additions & 49 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

let (:tree_with_nodes) {
tree.add(5, "Peter")
tree.add(3, "Paul")
Expand All @@ -15,69 +15,80 @@
tree.add(25, "Kari")
tree
}

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

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
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"}]
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
end

describe "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
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
end

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

# describe "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
end