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
148 changes: 126 additions & 22 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) - side eliminated
# Space Complexity: O(1)
def add(key, value)

Choose a reason for hiding this comment

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

Nice iterative solution!

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

if !@root
@root = new_node
else
current = @root
until !current
if current.key < new_node.key
if current.right
current = current.right
else
return current.right = new_node
end
else
if current.left
current = current.left
else
return current.left = new_node
end
end
end
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(log n) - side eliminated
# Space Complexity: O(1)
def find(key)
raise NotImplementedError
return nil if !@root

current = @root

until !current
return current.value if current.key == key

if current.key < key
current = current.right
else
current = current.left
end
end
return nil
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - all nodes visited
# Space Complexity: O(n) - array size based on node amount
def inorder
raise NotImplementedError
return [] if !@root
values = []
inorder_key(@root, values)
end

def inorder_key(current_node, array)
if !current_node
return array
else
inorder_key(current_node.left, array)
array << {key: current_node.key, value: current_node.value}
inorder_key(current_node.right, array)
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - all nodes visited
# Space Complexity: O(n) - array size based on node amount
def preorder
raise NotImplementedError
return [] if !@root
values = []
preorder_key(@root, values)
end

def preorder_key(current_node, array)
if !current_node
return array
else
array << {key: current_node.key, value: current_node.value}
preorder_key(current_node.left, array)
preorder_key(current_node.right, array)
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - all nodes visited
# Space Complexity: O(n) - array size based on node amount
def postorder
raise NotImplementedError
return [] if !@root
values = []
postorder_key(@root, values)
end

def postorder_key(current_node, array)
if !current_node
return array
else
postorder_key(current_node.left, array)
postorder_key(current_node.right, array)
array << {key: current_node.key, value: current_node.value}
end
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - possible to visit each node
# Space Complexity: O(1)
def height
raise NotImplementedError
return 0 if !@root
height_helper(@root)
end

def height_helper(current)
return 0 if !current
left = height_helper(current.left)
right = height_helper(current.right)

if left < right
return right + 1
else
return left + 1
end
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) - all nodes visited
# Space Complexity: O(n) - array size based on size of tree
def bfs
raise NotImplementedError
return [] if !@root

values = [{key: @root.key, value: @root.value}]
bfs_helper(@root, values)
end

def bfs_helper(current, array)
if current.left
array << {key: current.left.key, value: current.left.value}
end
if current.right
array << {key: current.right.key, value: current.right.value}
end
if !current.left && !current.right
return array
end
if current.left
bfs_helper(current.left, array)
end
if current.right
bfs_helper(current.right, array)
end
end
Comment on lines +139 to 164

Choose a reason for hiding this comment

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

Really nice work on this. This may help you with the Graph unit!


# Useful for printing
def to_s
return "#{self.inorder}"
end
end
end
13 changes: 12 additions & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
end

describe "inorder" do

it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
end
Expand All @@ -44,8 +45,8 @@
end
end


describe "preorder" do

it "will give an empty array for an empty tree" do
expect(tree.preorder).must_equal []
end
Expand All @@ -69,6 +70,16 @@
end
end

describe "height" do
it "will give height of 0 if empty tree" do
expect(tree.height).must_equal 0
end

it "will return height value for non-empty 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 []
Expand Down