Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kristy, Branches #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
106 changes: 83 additions & 23 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,124 @@
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:
# Time Complexity: O(n)
# Space Complexity: O(n)
def add(key, value)
raise NotImplementedError
Comment on lines +19 to -22

Choose a reason for hiding this comment

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

The space/time complexity is O(log n) if the tree is balanced and O(n) if it's not.

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

# Time Complexity:
# Space Complexity:
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

# Time Complexity: O(log n)
# Space Complexity: O(log n)
def find(key)
Comment on lines +35 to 37

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return find_helper(@root, key)
end

# Time Complexity:
# Space Complexity:
def find_helper(current, key)
return nil if current.nil?
return current.value if current.key == key
if key > current.key
return find_helper(current.right, key)
else
return find_helper(current.left, key)
end
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder
Comment on lines +51 to 53

Choose a reason for hiding this comment

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

👍

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

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

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

return list
end

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

Choose a reason for hiding this comment

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

👍

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

# Time Complexity:
# Space Complexity:
def preorder_helper(current, list)
return list if current.nil?
list << {key: current.key, value: current.value}
preorder_helper(current.left, list)
preorder_helper(current.right, list)
return list
end

# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder
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
return postorder_helper(@root, [])
end

# Time Complexity:
# Space Complexity:
def postorder_helper(current, list)
return list if current.nil?
postorder_helper(current.left, list)
postorder_helper(current.right, list)
list << {key: current.key, value: current.value}
return list
end

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

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, the time complexity is right, but the space complexity is O(log n) for a balanced tree since the call stack never gets greater than height of the tree.

raise NotImplementedError
return height_helper(@root, 0, 1)
end

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

max = count if count > max
height_helper(current.left, max, (count + 1))
height_helper(current.right, max, (count + 1))

end

# def delete(key)
# return nil if self.find(key).nil?

# end

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

# Useful for printing
def to_s
return "#{self.inorder}"
Expand Down
2 changes: 1 addition & 1 deletion test/tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
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
Expand Down