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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
Gemfile.lock
Gemfile
121 changes: 100 additions & 21 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,124 @@ def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add(key, value)
raise NotImplementedError
# Time Complexity: O(log n)
# Space Complexity: O(log n)
def add(key, value = nil)
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.

👍 Good use of default parameters. The space/time complexities are O(n) if the tree is unbalanced and O(log n) if the tree is balanced.

# raise NotImplementedError
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)
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:
# Space Complexity:
# Time Complexity: O(log n)
# Space Complexity: O(l)
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.

👍 The space/time complexities are O(n) if the tree is unbalanced and O(log n) if the tree is balanced. I'm uncertain what O(l) is?

raise NotImplementedError
return @root if @root.nil?

current = @root

until current.nil?
if current.key == key
return current.value
elsif key <= current.key
current = current.left
else
current = current.right
end
end

return current
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n) going through each node
# Space Complexity: O(n) creating an array
def inorder
Comment on lines +63 to 65

Choose a reason for hiding this comment

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

👍

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

return inorder_helper(@root, [])
end

def inorder_helper(current, values)
return values if current.nil?

inorder_helper(current.left, values)
values << { key: current.key, value: current.value }
inorder_helper(current.right, values)
end

# Time Complexity:
# Space Complexity:

# Time Complexity: O(n)
# Space Complexity:O(n)
def preorder
Comment on lines +81 to 83

Choose a reason for hiding this comment

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

👍

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

return preorder_helper(@root, [])
end

def preorder_helper(current, values)
return values if current.nil?

values << { key: current.key, value: current.value}
preorder_helper(current.left, values)
preorder_helper(current.right, values)

return values
end

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
Comment on lines +100 to 102

Choose a reason for hiding this comment

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

👍

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

return postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, values)
return values if current.nil?

postorder_helper(current.left, values)
postorder_helper(current.right, values)
values << { key: current.key, value: current.value}

return values
end

# Time Complexity:O(n)
# Space Complexity:O(n)
def height
Comment on lines +119 to 121

Choose a reason for hiding this comment

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

The space complexity is O(n) if the tree is unbalanced and O(log n) if the tree is balanced.

raise NotImplementedError
# raise NotImplementedError
height_helper(@root, 0)
end

def height_helper(current, count)
return count if current.nil?

left = height_helper(current.left, count + 1)
right = height_helper(current.right, count + 1)
return [left, right].max
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end
Expand Down
24 changes: 11 additions & 13 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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"},
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"}]
end
end
Expand All @@ -51,8 +51,8 @@
end

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"},
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"}]
end
end
Expand All @@ -63,20 +63,20 @@
end

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"},
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"}]
end
end

describe "breadth first search" do
xdescribe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end

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"},
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"}]
end
end
Expand All @@ -91,12 +91,12 @@
it "will return 1 for a tree of height 1" do
my_tree = Tree.new

my_tree.add(100)
my_tree.add(my_tree, 100)
expect(my_tree.height).must_equal 1
end

it "will report the height for a balanced tree" do
expect(tree_with_nodes.height).must_equal 3
expect(tree_with_nodes.height).must_equal 4
end

it "will report the height for unbalanced trees" do
Expand All @@ -112,8 +112,6 @@

my_tree = Tree.new

my_tree = Tree.new

my_tree.add(100)
my_tree.add(90)
my_tree.add(80)
Expand Down