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
111 changes: 89 additions & 22 deletions lib/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,130 @@ class TreeNode
attr_reader :key, :value
attr_accessor :left, :right

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

class Tree
attr_reader :root

def initialize
@root = nil
end

# Time Complexity:
# Space Complexity:
def add_helper(current_node, key, value)
return TreeNode.new(key, value) if current_node.nil?

if key < current_node.key
current_node.left = add_helper(current_node.left, key, value)
else
current_node.right = add_helper(current_node.right, key, value)
end

return current_node
end

# Time Complexity: O(h) where h is the max height of the binary search tree.
# In a best case scenario, time complexity would be O(logn)
# Space Complexity: O(1)
def add(key, value)
Comment on lines +32 to 35
Copy link
Copy Markdown

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(log n) if the tree is balanced and O(n) if it's not.

raise NotImplementedError
@root = add_helper(@root, key, value)
end

# Time Complexity:
# Space Complexity:
def find_helper(current_node, key)
return nil if current_node.nil?
return current_node.value if current_node.key == key

if key < current_node.key
find_helper(current_node.left, key)
else
find_helper(current_node.right, key)
end
end

# Time Complexity: If tree is balanced, it would be O(logn) n being the size of the tree
# In a worst case scenario, the time complexity would be O(n)
# Space Complexity: O(1)
def find(key)
Comment on lines +50 to 53
Copy link
Copy Markdown

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(log n) if the tree is balanced and O(n) if it's not.

raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
def inorder_helper(current_node, list)
return list if current_node.nil?

inorder_helper(current_node.left, list)
list << { key: current_node.key, value: current_node.value }
inorder_helper(current_node.right, list)
return list
end

# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def inorder
Comment on lines +66 to 68
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

raise NotImplementedError
return inorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def preorder_helper(current_node, list)
return list if current_node.nil?

list << { key: current_node.key, value: current_node.value }
preorder_helper(current_node.left, list)
preorder_helper(current_node.right, list)
return list
end

# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def preorder
Comment on lines +81 to 83
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

raise NotImplementedError
return preorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current_node, list)
return list if current_node.nil?

postorder_helper(current_node.left, list)
postorder_helper(current_node.right, list)
list << { key: current_node.key, value: current_node.value }
return list
end

# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(h) h is the height of the tree
def postorder
Comment on lines +96 to 98
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're visiting each node and creating a list containing all node values... the time/space complexity is O(n).

raise NotImplementedError
return postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def height_helper(current_node, max, count)
return max if current_node.nil?

if count > max
max = count
end

height_helper(current_node.left, max, count + 1)
height_helper(current_node.right, max, count + 1)
end
Comment on lines +102 to +111
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ This method isn't working since it's always returning the right subtree's height.


# Time Complexity: O(h) h is the height of the tree
# Space Complexity: O(n)
def height
raise NotImplementedError
return height_helper(@root, 0, 1)
end

# Optional Method
# Time Complexity:
# Space Complexity:
# Time Complexity:
# Space Complexity:
def bfs
raise NotImplementedError
end

def delete(key)
end

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
41 changes: 20 additions & 21 deletions test/tree_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative "test_helper"

describe Tree do
let (:tree) {Tree.new}
let (:tree) { Tree.new }

let (:tree_with_nodes) {
tree.add(5, "Peter")
Expand All @@ -13,7 +13,7 @@
tree
}

describe "add and find" do
describe "add and find" do
it "add & find values" do
tree.add(5, "Peter")
expect(tree.find(5)).must_equal "Peter"
Expand All @@ -36,22 +36,21 @@
end

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


describe "preorder" do
it "will give an empty array for an empty tree" do
expect(tree.preorder).must_equal []
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"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
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 @@ -61,26 +60,26 @@
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"},
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
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"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
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
describe "height" do
it "will return 0 if tree is empty" do

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

Expand All @@ -91,14 +90,14 @@
tree_with_nodes.add(65, "sam")
expect(tree_with_nodes.height).must_equal 6
end

it "will give the correct height of a binary search tree" do
tree_with_nodes.add(30, "Tatiana")
expect(tree_with_nodes.height).must_equal 5
end
end

describe "delete" do
xdescribe "delete" do
it "can delete a note in the tree" do
# Arrange & Assert
expect(tree_with_nodes.find(15)).must_equal "Ada"
Expand Down