-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Hannah T #38
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
base: master
Are you sure you want to change the base?
Time - Hannah T #38
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,62 +1,141 @@ | ||||||
require "pry" | ||||||
|
||||||
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(Log n) | ||||||
# Space Complexity: O(1) | ||||||
def add(key, value) | ||||||
raise NotImplementedError | ||||||
new_node = TreeNode.new(key, value) | ||||||
if !@root | ||||||
@root = new_node | ||||||
return | ||||||
end | ||||||
current = @root | ||||||
while current != nil | ||||||
if key < current.key | ||||||
if current.left | ||||||
current = current.left | ||||||
else | ||||||
current.left = new_node | ||||||
return | ||||||
end | ||||||
else | ||||||
if current.right | ||||||
current = current.right | ||||||
else | ||||||
current.right = new_node | ||||||
return | ||||||
end | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: O(Log n) | ||||||
# Space Complexity: O(1) | ||||||
def find(key) | ||||||
Comment on lines
+50
to
52
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 | ||||||
current = @root | ||||||
while current != nil | ||||||
if key == current.key | ||||||
return current.value | ||||||
elsif key < current.key | ||||||
current = current.left | ||||||
else | ||||||
current = current.right | ||||||
end | ||||||
end | ||||||
return nil | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: O(Log n) | ||||||
# Space Complexity: O(n) | ||||||
def inorder | ||||||
Comment on lines
+66
to
68
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. 👍 , interesting choice to make an inner function. That's a very JavaScripty thing to do here. |
||||||
raise NotImplementedError | ||||||
ary = [] | ||||||
|
||||||
def innerfunc(node, ary) | ||||||
return if node.nil? | ||||||
innerfunc(node.left, ary) | ||||||
ary << { :key => node.key, :value => node.value } | ||||||
innerfunc(node.right, ary) | ||||||
end | ||||||
|
||||||
innerfunc(@root, ary) | ||||||
return ary | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: O(Log n) | ||||||
# Space Complexity: O(n) | ||||||
def preorder | ||||||
Comment on lines
+82
to
84
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 | ||||||
ary = [] | ||||||
|
||||||
def innerfunc(node, ary) | ||||||
return if node.nil? | ||||||
ary << { :key => node.key, :value => node.value } | ||||||
innerfunc(node.left, ary) | ||||||
innerfunc(node.right, ary) | ||||||
end | ||||||
|
||||||
innerfunc(@root, ary) | ||||||
return ary | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: O(Log n) | ||||||
# Space Complexity: O(n) | ||||||
|
||||||
def postorder | ||||||
Comment on lines
+98
to
101
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 | ||||||
ary = [] | ||||||
def innerfunc(node, ary) | ||||||
return if node.nil? | ||||||
innerfunc(node.left, ary) | ||||||
innerfunc(node.right, ary) | ||||||
ary << { :key => node.key, :value => node.value } | ||||||
end | ||||||
|
||||||
innerfunc(@root, ary) | ||||||
return ary | ||||||
end | ||||||
|
||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def height | ||||||
Comment on lines
+114
to
116
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 and space complexity? |
||||||
raise NotImplementedError | ||||||
def innerfunc(node) | ||||||
return 0 if node.nil? | ||||||
return 1 if !node.left && !node.right | ||||||
return [innerfunc(node.left) + 1, innerfunc(node.right) + 1].max | ||||||
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. Very slightly more compact
Suggested change
|
||||||
end | ||||||
|
||||||
return innerfunc(@root) | ||||||
end | ||||||
|
||||||
# Optional Method | ||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
# Time Complexity: | ||||||
# Space Complexity: | ||||||
def bfs | ||||||
Comment on lines
+127
to
129
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 with BFS. Time and space complexity? |
||||||
raise NotImplementedError | ||||||
q = [@root] | ||||||
ary = [] | ||||||
while !q.empty? | ||||||
current = q.pop | ||||||
q << current.left if current.left | ||||||
q << current.right if current.right | ||||||
ary << { :key => current.key, :value => current.value } | ||||||
end | ||||||
return ary | ||||||
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.
👍