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
91 changes: 85 additions & 6 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,121 @@ class Tree
attr_reader :root
def initialize
@root = nil
@nodes = []
end

def add_helper(current, key)
if key <= current.key
return current if current.left.nil?
add_helper(current.left, key)
else
return current if current.right.nil?
add_helper(current.right, key)
end
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
if @root == nil
@root = TreeNode.new(key, value)
return
end

node = add_helper(@root, key)
if key <= node.key
node.left = TreeNode.new(key, value)
else
node.right = TreeNode.new(key, value)
end
Comment on lines +33 to +43

Choose a reason for hiding this comment

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

This doesn't actually work, you only can construct a 3-node tree your way.

Consider:

Suggested change
if @root == nil
@root = TreeNode.new(key, value)
return
end
node = add_helper(@root, key)
if key <= node.key
node.left = TreeNode.new(key, value)
else
node.right = TreeNode.new(key, value)
end
@root = add_helper(@root, key, value)
end
def add_helper(current, key, value)
return TreeNode.new(key, value) if current.nil?
if key <= current.key
current.left = add_helper(current.left, key, value)
else
current.right = add_helper(current.right, key, value)
end
return current
end

end

def find_helper(current, key)
if key <= current.key
return current.value if current.key == key
find_helper(current.left, key)
elsif key > current.key
return current.value if current.key == key

Choose a reason for hiding this comment

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

This can't be true.

Suggested change
return current.value if current.key == key

find_helper(current.right, key)
end
end

# Time Complexity:
# Space Complexity:
def find(key)
raise NotImplementedError
return nil if @root.nil?
return @root.value if @root.key == key

current = @root
find_key = find_helper(current, key)

return find_key
end

# inorder helper
def traverse_inorder(tree)
traverse_inorder(tree.left) if tree.left
@nodes.push({key: tree.key, value: tree.value})
traverse_inorder(tree.right) if tree.right
end

# Time Complexity:
# Space Complexity:
# left, root, right
def inorder
Comment on lines 75 to 78

Choose a reason for hiding this comment

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

👍 Time/space complexities?

raise NotImplementedError
return [] if @root.nil?
traverse_inorder(@root)
return @nodes
end

# preorder helper
def traverse_preorder(tree)
@nodes.push({key: tree.key, value: tree.value})
traverse_preorder(tree.left) if tree.left
traverse_preorder(tree.right) if tree.right
end

# Time Complexity:
# Space Complexity:
def preorder
Comment on lines 91 to 93

Choose a reason for hiding this comment

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

👍 Time/space complexities?

raise NotImplementedError
return [] if @root.nil?
traverse_preorder(@root)
return @nodes
end

# postorder helper
def traverse_postorder(tree)
traverse_postorder(tree.left) if tree.left
traverse_postorder(tree.right) if tree.right
@nodes.push({key: tree.key, value: tree.value})
end

# Time Complexity:
# Space Complexity:
def postorder
Comment on lines 106 to 108

Choose a reason for hiding this comment

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

👍 Time/space complexities?

raise NotImplementedError
return [] if @root.nil?
traverse_postorder(@root)
return @nodes
end

def height_helper(tree, height)
return height_helper(tree.left, height) if tree.left
return height_helper(tree.right, height) if tree.right
return height + 1
end

# Time Complexity:
# Space Complexity:
def height
Comment on lines 120 to 122

Choose a reason for hiding this comment

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

👍 Time/space complexities?

raise NotImplementedError
return 0 if @root.nil?
return 1 if (@root.left.nil? && @root.right.nil?)

left_height = 1
height_helper(@root.left, 1) if @root.left
right_height = 1
height_helper(@root.right, 1) if @root.right

return left_height >= right_height ? left_height : right_height
end

# Optional Method
Expand Down
19 changes: 19 additions & 0 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@

tree.add(3, "Paul")
expect(tree.find(3)).must_equal "Paul"

tree.add(4, "Mary")
expect(tree.find(4)).must_equal "Mary"

tree.add(10, "Karla")
expect(tree.find(10)).must_equal "Karla"

tree.add(1, "Jenny")
expect(tree.find(1)).must_equal "Jenny"

end

it "can't find anything when the tree is empty" do
Expand All @@ -49,6 +59,15 @@
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"}]
expect(balanced_tree.inorder).must_equal [
{:key=>1, :value=>"Mary"},
{:key=>3, :value=>"Paul"},
{:key=>4, :value=>"Kate"},
{:key=>5, :value=>"Peter"},
{:key=>8, :value=>"Ada"},
{:key=>10, :value=>"Karla"},
{:key=>25, :value=>"Kari"}
]
end
end

Expand Down