forked from AdaGold/tree-practice
-
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 - Samantha #28
Open
iamsammysam
wants to merge
3
commits into
Ada-C12:master
Choose a base branch
from
iamsammysam:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,152 @@ | ||
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(logn) | ||
# Space Complexity: O(logn) if tree is balanced and O(n) if it's not balanced. | ||
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 | ||
|
||
return current_node | ||
end | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_helper(@root, key, value) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(logn) if tree is balanced and O(n) if it's not balanced. | ||
def find_helper(current_node, key) | ||
return nil if current_node.nil? | ||
|
||
if key > current_node.key | ||
current_node.right = find_helper(current_node.right, key) | ||
elsif key < current_node.key | ||
current_node.left = find_helper(current_node.left, key) | ||
else | ||
return current_node.value | ||
end | ||
end | ||
|
||
def find(key) | ||
raise NotImplementedError | ||
return find_helper(@root, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
raise NotImplementedError | ||
return inorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
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: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
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 pre_order_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def pre_order_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
list << {key: current_node.key, value: current_node.value} | ||
pre_order_helper(current_node.left, list) | ||
pre_order_helper(current_node.right, list) | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
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 post_order_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def post_order_helper(current_node, list) | ||
return list if current_node.nil? | ||
|
||
post_order_helper(current_node.left, list) | ||
post_order_helper(current_node.right, list) | ||
list << {key: current_node.key, value: current_node.value} | ||
return list | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(logn) if tree is balanced and O(n) if it's not balanced. | ||
def height_helper(current_node) | ||
return 0 if current_node.nil? | ||
|
||
if height_helper(current_node.right) > height_helper(current_node.left) | ||
return height_helper(current_node.right) + 1 | ||
else | ||
return height_helper(current_node.left) + 1 | ||
end | ||
end | ||
|
||
def height | ||
raise NotImplementedError | ||
return height_helper(@root) | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def bfs | ||
raise NotImplementedError | ||
list = [] | ||
return list if @root.nil? | ||
queue = [@root] | ||
|
||
until queue.empty? | ||
current = queue.shift | ||
queue.push(current.left) unless current.left.nil? | ||
queue.push(current.right) unless current.right.nil? | ||
|
||
list << {key: current.key, value: current.value} | ||
end | ||
return list | ||
end | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_largest(current) | ||
current = current_root | ||
|
||
while current.right | ||
current = current.right | ||
end | ||
return current | ||
end | ||
|
||
# def delete(key) | ||
# raise NotImplementedError | ||
# end | ||
|
||
# Useful for printing | ||
def to_s | ||
return "#{self.inorder}" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍