-
Notifications
You must be signed in to change notification settings - Fork 44
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
Shirley #27
base: master
Are you sure you want to change the base?
Shirley #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,63 +2,162 @@ 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 | ||
@parent = nil | ||
end | ||
end | ||
|
||
class Tree | ||
attr_reader :root | ||
|
||
def initialize | ||
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
return @root = new_node unless @root | ||
add_node(@root, new_node) | ||
end | ||
|
||
def add_node(root, new_node) | ||
if new_node.key < root.key | ||
return root.left = new_node unless root.left | ||
add_node(root.left, new_node) | ||
elsif new_node.key > root.key | ||
return root.right = new_node unless root.right | ||
add_node(root.right, new_node) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) / O(logn) | ||
# Space Complexity: O(1) | ||
def find(key) | ||
raise NotImplementedError | ||
return find_helper(key, @root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(key, node) | ||
return nil unless node | ||
if key == node.key | ||
return node.value | ||
elsif key < node.key | ||
find_helper(key, node.left) | ||
else key > node.key | ||
find_helper(key, node.right) end | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
raise NotImplementedError | ||
array = [] | ||
inorder_helper(array, @root) | ||
end | ||
|
||
def inorder_helper(array, node) | ||
return array unless node | ||
inorder_helper(array, node.left) | ||
node_kv = {} | ||
node_kv[:key] = node.key | ||
node_kv[:value] = node.value | ||
array.push(node_kv) | ||
inorder_helper(array, node.right) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
raise NotImplementedError | ||
array = [] | ||
return preorder_helper(array, @root) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(array, node) | ||
return array unless node | ||
node_kv = {} | ||
node_kv[:key] = node.key | ||
node_kv[:value] = node.value | ||
array.push(node_kv) | ||
preorder_helper(array, node.left) | ||
preorder_helper(array, node.right) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
raise NotImplementedError | ||
array = [] | ||
postorder_helper(@root, array) | ||
return array | ||
end | ||
|
||
def postorder_helper(root, array) | ||
return unless root | ||
postorder_helper(root.left, array) | ||
postorder_helper(root.right, array) | ||
array.push({ key: root.key, value: root.value }) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
height_counter(@root) | ||
end | ||
|
||
def height_counter(node) | ||
return 0 unless node | ||
left_counter = height_counter(node.left) | ||
right_counter = height_counter(node.right) | ||
counter = left_counter > right_counter ? left_counter : right_counter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return counter += 1 | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(2n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Big-O we drop the coefficient so this should be Space complexity of O(n) |
||
|
||
# i know there will be less lines of code if q is implemented, please see commented psuedocode below for q implementation | ||
def bfs | ||
raise NotImplementedError | ||
return [] unless @root | ||
array = [@root] | ||
i = 0 | ||
current = @root | ||
length = 1 | ||
until (i == length - 1) && (current.left == nil) && (current.right == nil) | ||
if current.left | ||
array.push(current.left) | ||
length += 1 | ||
end | ||
if current.right | ||
array.push(current.right) | ||
length += 1 | ||
end | ||
i += 1 | ||
current = array[i] | ||
end | ||
return array.map do |node| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good use of map |
||
{ key: node.key, value: node.value } | ||
end | ||
end | ||
|
||
# BFS USING QUEUE | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(2n) | ||
# def bfs | ||
# q = Queue.new(@root) | ||
# array = [] | ||
# until q is_empty | ||
# current = q.dequeue | ||
# array.push({key: current.key, value: current.value }) | ||
# q.enqueue(current.left) if current.left | ||
# q.enqueue(current.right) if current.right | ||
# end | ||
# return array | ||
# end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're using a recursive
add_node
helper method, you are using the system stack and this impacts space complexity. Since you are making O(log n) recursive calls on a balanced tree this would have a space complexity of O(log n).