-
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
Dominique #31
base: master
Are you sure you want to change the base?
Dominique #31
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require "pry" | ||
|
||
class TreeNode | ||
attr_reader :key, :value | ||
attr_accessor :left, :right | ||
|
@@ -19,44 +21,173 @@ def initialize | |
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
|
||
# iterative loop way | ||
if @root.nil? | ||
@root = TreeNode.new(key, value) | ||
else | ||
current = @root | ||
while true | ||
if key < current.key | ||
|
||
if !current.left.nil? | ||
current = current.left | ||
else | ||
current.left = TreeNode.new(key, value) | ||
return | ||
end | ||
else | ||
if !current.right.nil? | ||
current = current.right | ||
else | ||
current.right = TreeNode.new(key, value) | ||
return | ||
end | ||
end | ||
end | ||
end | ||
# recursive way | ||
# @root = add_helper(@root, key, value) | ||
end | ||
|
||
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 | ||
end | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
Comment on lines
64
to
66
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. Time & space complexity? |
||
raise NotImplementedError | ||
current_node = @root | ||
return find_helper(current_node, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current_node, key) | ||
if current_node.nil? | ||
return nil | ||
end | ||
|
||
returned_value = find_helper(current_node.left, key) | ||
|
||
if returned_value != nil | ||
return returned_value | ||
end | ||
|
||
if current_node.key == key | ||
return current_node.value | ||
end | ||
|
||
return find_helper(current_node.right, key) | ||
|
||
end | ||
|
||
# Time Complexity: O(n) because I went through each node once | ||
# Space Complexity: O(n) because a stack was created | ||
def inorder | ||
Comment on lines
+90
to
92
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. 👍 |
||
raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
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: | ||
# Space Complexity: | ||
def preorder | ||
Comment on lines
105
to
107
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. Time/space complexity? |
||
raise NotImplementedError | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
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: | ||
# Space Complexity: | ||
def postorder | ||
Comment on lines
122
to
124
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. Time/space complexity? |
||
raise NotImplementedError | ||
return postorder_helper(@root, []) | ||
end | ||
|
||
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 | ||
# if current_node.left == nil | ||
# list << {key: current_node, value: current_node.value} | ||
# end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
Comment on lines
140
to
142
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. Time/space complexity? Otherwise well done |
||
raise NotImplementedError | ||
current_node = @root | ||
|
||
return height_helper(current_node) | ||
end | ||
|
||
def height_helper(current_node) | ||
|
||
return 0 if current_node.nil? | ||
|
||
left_height = height_helper(current_node.left) | ||
|
||
right_height = height_helper(current_node.right) | ||
|
||
height_array = [left_height, right_height] | ||
|
||
return height_array.max + 1 | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
Comment on lines
161
to
164
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. Nice work on the optional. Now what about time & space complexity? |
||
raise NotImplementedError | ||
q = [] | ||
list = [] | ||
|
||
return [] if @root.nil? | ||
|
||
current_node = @root | ||
|
||
q.push(current_node) | ||
# put current node into queue | ||
while q.length != 0 | ||
# put current node into list and delete from queue | ||
# add current node's children into the queue | ||
list.push({key: q[0].key, value: q[0].value}) | ||
q.shift | ||
|
||
if current_node.left != nil | ||
q.push(current_node.left) | ||
end | ||
|
||
if current_node.right != nil | ||
q.push(current_node.right) | ||
end | ||
current_node = q[0] | ||
end | ||
|
||
return list | ||
end | ||
|
||
# Useful for printing | ||
|
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.
Time & space complexity?