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

Dominique #31

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
149 changes: 140 additions & 9 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "pry"

class TreeNode
attr_reader :key, :value
attr_accessor :left, :right
Expand All @@ -19,44 +21,173 @@ def initialize
# Time Complexity:
# Space Complexity:
def add(key, value)
Comment on lines 21 to 23

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

# iterative loop way
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
# recursive way
# @root = add_helper(@root, key, value)
end

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:
# Space Complexity:
def find(key)
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.

Time & space complexity?

raise NotImplementedError
current_node = @root
return find_helper(current_node, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(current_node, key)
if current_node.nil?
return nil
end

returned_value = find_helper(current_node.left, key)

if returned_value != nil
return returned_value
end

if current_node.key == key
return current_node.value
end

return find_helper(current_node.right, key)

end

# Time Complexity: O(n) because I went through each node once
# Space Complexity: O(n) because a stack was created
def inorder
Comment on lines +90 to 92

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

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:
# Space Complexity:
def preorder
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.

Time/space complexity?

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

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)
return list
end

# Time Complexity:
# Space Complexity:
def postorder
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.

Time/space complexity?

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

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}
return list
# if current_node.left == nil
# list << {key: current_node, value: current_node.value}
# end
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 140 to 142

Choose a reason for hiding this comment

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

Time/space complexity?

Otherwise well done

raise NotImplementedError
current_node = @root

return height_helper(current_node)
end

def height_helper(current_node)

return 0 if current_node.nil?

left_height = height_helper(current_node.left)

right_height = height_helper(current_node.right)

height_array = [left_height, right_height]

return height_array.max + 1
end

# Optional Method
# Time Complexity:
# Space Complexity:
def bfs
Comment on lines 161 to 164

Choose a reason for hiding this comment

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

Nice work on the optional.

Now what about time & space complexity?

raise NotImplementedError
q = []
list = []

return [] if @root.nil?

current_node = @root

q.push(current_node)
# put current node into queue
while q.length != 0
# put current node into list and delete from queue
# add current node's children into the queue
list.push({key: q[0].key, value: q[0].value})
q.shift

if current_node.left != nil
q.push(current_node.left)
end

if current_node.right != nil
q.push(current_node.right)
end
current_node = q[0]
end

return list
end

# Useful for printing
Expand Down
10 changes: 7 additions & 3 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative "test_helper"

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

Expand All @@ -15,14 +15,18 @@

describe "add and find" do
it "add & find values" do

tree.add(5, "Peter")
# binding.pry
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
Expand Down Expand Up @@ -84,7 +88,7 @@
expect(tree.height()).must_equal 0
end

it "will return the nuber of nodes in the longest path" do
it "will return the number 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")
Expand All @@ -98,7 +102,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