-
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
Leaves - Katie K #39
base: master
Are you sure you want to change the base?
Leaves - Katie K #39
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"workbench.colorCustomizations": { | ||
"activityBar.background": "#7f40bf", | ||
"activityBar.activeBorder": "#c78f58", | ||
"activityBar.foreground": "#e7e7e7", | ||
"activityBar.inactiveForeground": "#e7e7e799", | ||
"activityBarBadge.background": "#c78f58", | ||
"activityBarBadge.foreground": "#15202b", | ||
"titleBar.activeBackground": "#663399", | ||
"titleBar.inactiveBackground": "#66339999", | ||
"titleBar.activeForeground": "#e7e7e7", | ||
"titleBar.inactiveForeground": "#e7e7e799", | ||
"statusBar.background": "#663399", | ||
"statusBarItem.hoverBackground": "#7f40bf", | ||
"statusBar.foreground": "#e7e7e7" | ||
}, | ||
"peacock.color": "#639" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,165 @@ | ||
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 | ||
|
||
def inorder(list) | ||
list = left.inorder(list) unless left.nil? | ||
list << {key: key, value: value} | ||
list = right.inorder(list) unless right.nil? | ||
return list | ||
end | ||
|
||
def preorder(list) | ||
list << {key: key, value: value} | ||
list = left.preorder(list) unless left.nil? | ||
list = right.preorder(list) unless right.nil? | ||
return list | ||
end | ||
|
||
def postorder(list) | ||
list = left.postorder(list) unless left.nil? | ||
list = right.postorder(list) unless right.nil? | ||
list << {key: key, value: value} | ||
return list | ||
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_helper(current, key, value) | ||
KKennedyCodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(1) | ||
# Space Complexity: O1 | ||
def find(key) | ||
Comment on lines
+57
to
59
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 complexity constant? Really? Think about this a bit. |
||
raise NotImplementedError | ||
current = @root | ||
until current.nil? | ||
if current.key == key | ||
return current.value | ||
elsif key > current.key | ||
current = current.right | ||
else | ||
current = current.left | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: On | ||
# Space Complexity: On | ||
def inorder_helper | ||
Comment on lines
+73
to
+75
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 inorder_helper(@root, []) | ||
list = left.inorder(list) unless left.nil? | ||
list << {key: key, value: value} | ||
list = right.inorder(list) unless right.nil? | ||
return list | ||
end | ||
|
||
def inorder | ||
raise NotImplementedError | ||
list = [] | ||
return list if @root.nil? | ||
return @root.inorder(list) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: On | ||
def preorder | ||
Comment on lines
+89
to
91
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 | ||
list = [] | ||
return list if @root.nil? | ||
return @root.preorder([]) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: On | ||
# Space Complexity: On | ||
def postorder | ||
Comment on lines
+97
to
99
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 | ||
list = [] | ||
return list if @root.nil? | ||
return @root.postorder([]) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def max (num1, num2) | ||
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. I like this little helper. |
||
if num1 > num2 | ||
return num1 | ||
else | ||
return num2 | ||
end | ||
end | ||
|
||
def height_helper(current) | ||
return 0 if current.nil? | ||
return 1 + max(height_helper(current.left), height_helper(current.right)) | ||
end | ||
|
||
def height | ||
Comment on lines
+115
to
120
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. 👍 Well done |
||
raise NotImplementedError | ||
return height_helper(@root) | ||
end | ||
|
||
def delete_helper(current, key) | ||
return nil if current.nil? | ||
|
||
if current.key > key | ||
current.left = delete_helper(current.left, key) | ||
return current | ||
end | ||
|
||
if current.key < key | ||
current.right = delete_helper(current.right, key) | ||
return current | ||
end | ||
|
||
if current.left.nil? && current.right.nil? | ||
return nil | ||
end | ||
|
||
if current.left.nil? | ||
return current.right | ||
end | ||
|
||
if current.right.nil? | ||
return current.left | ||
end | ||
|
||
end | ||
|
||
def delete(key) | ||
value = self.find(key) | ||
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. Not sure what value is doing? This is a good start at the |
||
@root = delete_helper(@root, key) | ||
return value | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
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.
I like how you put these into the
TreeNode
class.