Skip to content

Leah - Space #27

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

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
185 changes: 150 additions & 35 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,179 @@
class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

def initialize(key, val)
def initialize(key, value)
@key = key
@value = val
@value = value
@left = nil
@right = nil
end
end
end

class Tree
attr_reader :root
def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: Olog(n) - only need to look at half the tree
# Space Complexity: O(1)
def add(key, value)
Comment on lines +19 to 21

Choose a reason for hiding this comment

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

👍 Since you're doing this recursively, the space complexity is O(log n) if the tree is balanced and O(n) if it is unbalanced. Same for time complexities.

raise NotImplementedError
new_node = TreeNode.new(key, value)

if @root.nil?
@root = new_node
else
add_helper(@root, new_node)
end
end

# Time Complexity:
# Space Complexity:

def add_helper(current, new_node)
return new_node if current.nil?

if new_node.key <= current.key
current.left = add_helper(current.left, new_node)
else
current.right = add_helper(current.right, new_node)
end

return current
end

# Time Complexity: Olog(n) - only need to look at half the tree
# Space Complexity: O(1)
def find(key)
Comment on lines +43 to 45

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return nil if @root.nil?
return @root.value if @root.key == key

current = @root
while current != nil
if key == current.key
return current.value
elsif key < current.key
current = current.left
elsif key > current.key
current = current.right
end
end
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n) - n is the number of nodes in the tree
# Space Complexity: O(n) - my thought is that since we're making a list of nodes to return, we'll have that many nodes stored in the list, but since the stack doesn't ever hold something that's also already in the list, there would never be more than n nodes stored at one time..? Let me know if that's on the right track.
# left, root, right (me btwn my children)
def inorder
Comment on lines +61 to 64

Choose a reason for hiding this comment

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

Nice work doing this with loops and a stack. I guess you REALLY don't like recursion.

raise NotImplementedError
return [] if @root.nil?
list = []
stack = []
current = @root

# while current node exists OR the stack is not empty
while !current.nil? || !stack.empty?

# while current node exists
while !current.nil?
# add it to the stack
stack << current
# current becomes node to its left and if it is not nil, this loop will continue
current = current.left
end

# pop off the last node that was added
current = stack.pop
# add it to the list
list << {key: current.key, value: current.value}
# traverse to the right
current = current.right
end

return list
end

# Time Complexity:
# Space Complexity:
def preorder
raise NotImplementedError

# Time Complexity: O(n) - n is the number of nodes in the tree
# Space Complexity: O(n) - same assumption as inorder
# root, left, right (me before my children)
def preorder
Comment on lines +92 to +95

Choose a reason for hiding this comment

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

👍

return [] if @root.nil?
list = []
stack = []
current = @root

stack.push(current)
while !stack.empty?
current = stack.pop
list << {key: current.key, value: current.value}
stack << current.right if current.right
stack << current.left if current.left
end

return list
end

# Time Complexity:
# Space Complexity:
def postorder
raise NotImplementedError

# Time Complexity: O(n) - n is the number of nodes in the tree
# Space Complexity: O(n) - same assumption as inorder
# left, right, root (me after my children)
def postorder
Comment on lines +112 to +115

Choose a reason for hiding this comment

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

👍

return [] if @root.nil?

list = []
s1 = []
s2 = []
current = @root

s1 << current
while !s1.empty?
current = s1.pop
s2 << current
s1 << current.left if current.left
s1 << current.right if current.right
end

while !s2.empty?
current = s2.pop
list << {key: current.key, value: current.value}
end

return list
end

# Time Complexity:
# Space Complexity:
# Time Complexity: Olog(n) in the best case (e.g. if the tree is balanced), O(n) in the worst case (the tree is unbalanced and essentially a linked list)
# Space Complexity: O(1)
def height
Comment on lines +139 to 141

Choose a reason for hiding this comment

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

👍 , Since you are doing this recursively the space complexity is O(log n) if the tree is balanced and O(n) otherwise.

raise NotImplementedError
return 0 if @root.nil?
return height_helper(@root)
end


def height_helper(current)
if current.nil?
return 0
else
return 1 + [height_helper(current.left), height_helper(current.right)].max
end
end


# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - n is number of nodes
# Space Complexity: O(n) - queue will hold at most the entire tree
def bfs
Comment on lines +156 to 158

Choose a reason for hiding this comment

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

👍 , nice work!

raise NotImplementedError
end
return [] if @root.nil?

queue = []
list = []

queue << @root
current = @root
while !queue.empty?
current = queue[0]
queue << current.left if !current.left.nil?
queue << current.right if !current.right.nil?
node_to_add = queue.shift
list << {key: node_to_add.key, value: node_to_add.value}
end

return list
end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
16 changes: 8 additions & 8 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,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 All @@ -62,8 +62,8 @@

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

Expand All @@ -74,8 +74,8 @@

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

Expand All @@ -86,8 +86,8 @@

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 Down