Skip to content
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
142 changes: 123 additions & 19 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,155 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: O(1)
def add(key, value)
raise NotImplementedError
puts ""
new_node = TreeNode.new(key, value)
if @root.nil?
@root = new_node
else
add_helper(@root, new_node)
end
end

def add_helper(current, new_node)
if new_node.key <= current.key
if current.left.nil?
current.left = new_node
else
add_helper(current.left, new_node)
end
else
if current.right.nil?
current.right = new_node
else
add_helper(current.right, new_node)
end
end
end
# Time Complexity: O(log n)
# Space Complexity: O(1)
def find_helper(current,key)
return nil if current.nil?
if key == current.key
return current.value
end
if key < current.key
return find_helper(current.left, key)
end
if key > current.key
return find_helper(current.right, key)
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
return find_helper(@root,key)
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n)

Choose a reason for hiding this comment

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

Actually the time complexity is O(n) because you visit every node.

# Space Complexity: O(n) - an new array is created to store the in-order nodes

def inorder
raise NotImplementedError
inorder_array = []
return inorder_array if @root == nil
current = @root
return inorder_helper(current, inorder_array)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current, inorder_array)
if current == nil
return inorder_array
else
inorder_helper(current.left, inorder_array)
inorder_array.push({:key => current.key, :value => current.value})
inorder_helper(current.right, inorder_array)
end
end

# Time Complexity: O(log n)

Choose a reason for hiding this comment

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

O(n)

# Space Complexity: O(n)
def preorder
raise NotImplementedError
array = []
return array if @root == nil
current = @root
return preorder_helper(current, array)
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current, array)
if current == nil
return array
else
array.push({:key => current.key, :value => current.value})
preorder_helper(current.left, array)
preorder_helper(current.right, array)
end
end

# Time Complexity: O(log n)

Choose a reason for hiding this comment

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

O(n)

# Space Complexity: O(n)
def postorder
raise NotImplementedError
array = []
return array if @root == nil
current = @root
return postorder_helper(current, array)
end

def postorder_helper(current, array)
if current == nil
return array
else
postorder_helper(current.left, array)
postorder_helper(current.right, array)
array.push({:key => current.key, :value => current.value})
end
end

# Time Complexity:
# Space Complexity:
def height
raise NotImplementedError
current = @root
height = 1
return 0 if @root.nil?
return height_helper(current,height)
end

def height_helper(current, height)

Choose a reason for hiding this comment

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

Nice example of tail recursion

if current.nil?
return height
else
height += 1
height_helper(current.left,height)
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def bfs
raise NotImplementedError
queue = Queue.new
if @root.nil?
return []
end
array = []
queue.push(@root)
while !queue.empty?
node = queue.pop

Choose a reason for hiding this comment

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

If you're doing pushes and pops, this isn't a Queue it's a stack. Thus This isn't going to be doing BFS

array.push({:key => node.key, :value => node.value})
if !node.left.nil?
queue.push(node.left)
end
if !node.right.nil?
queue.push(node.right)
end
end
return array
end

# Useful for printing
def to_s
return "#{self.inorder}"
end
end

4 changes: 2 additions & 2 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let (:tree_with_nodes) {
tree.add(5, "Peter")
tree.add(3, "Paul")
tree.add(1, "Mary")
tree.add(1, "Mary")
tree.add(10, "Karla")
tree.add(15, "Ada")
tree.add(25, "Kari")
Expand Down Expand Up @@ -39,7 +39,7 @@
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=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
Expand Down