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
Angela #6
Open
AngelaOh
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
AngelaOh: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
Angela #6
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -2,61 +2,142 @@ 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) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data | ||
# Space Complexity: O(1) | ||
def add(key, value) | ||
raise NotImplementedError | ||
new_node = TreeNode.new(key, value) | ||
@root = new_node if @root.nil? | ||
current = @root | ||
add_helper(current, new_node) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add_helper(current, new_node) | ||
if current.nil? | ||
return current = new_node | ||
elsif new_node.key < current.key | ||
current.left = add_helper(current.left, new_node) | ||
elsif new_node.key > current.key | ||
current.right = add_helper(current.right, new_node) | ||
end | ||
return current | ||
end | ||
|
||
# Time Complexity: O(logn) because one side of the tree can be eliminated each time the new key is evaluated against the existing nodes data | ||
# Space Complexity: O(1) | ||
def find(key) | ||
raise NotImplementedError | ||
current = @root | ||
return nil if current.nil? | ||
return current.value if current.key == key | ||
find_helper(current, key) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find_helper(current, key) | ||
if key == current.key | ||
return current.value | ||
elsif key < current.key | ||
current.left = find_helper(current.left, key) | ||
else | ||
current.right = find_helper(current.right, key) | ||
end | ||
end | ||
|
||
# Time Complexity: O(n) because each node will need to be visited and added to the final array | ||
# Space Complexity: O(n) because the final array will contain the size of the tree | ||
# left, root, right | ||
def inorder | ||
raise NotImplementedError | ||
array = [] | ||
return array if @root.nil? | ||
inorder_helper(array, @root) | ||
return array | ||
end | ||
|
||
def inorder_helper(array, node) | ||
return if node.nil? | ||
inorder_helper(array, node.left) | ||
hash = { key: node.key, value: node.value } | ||
array.push(hash) | ||
inorder_helper(array, node.right) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) because each node will need to be visited and added to the final array | ||
# Space Complexity: O(n) because the final array will contain the size of the tree | ||
# root, left, right | ||
def preorder | ||
raise NotImplementedError | ||
return [] if @root.nil? | ||
array = [] | ||
preorder_helper(@root, array) | ||
return array | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(node, array) | ||
return if node.nil? | ||
array.push({ key: node.key, value: node.value }) | ||
preorder_helper(node.left, array) | ||
preorder_helper(node.right, array) | ||
end | ||
|
||
# Time Complexity: O(n) because each node will need to be visited and added to the final array | ||
# Space Complexity: O(n) because the final array will contain the size of the tree | ||
# left, right, root | ||
def postorder | ||
raise NotImplementedError | ||
array = [] | ||
return array if @root.nil? | ||
postorder_helper(array, @root) | ||
return array | ||
end | ||
|
||
def postorder_helper(array, node) | ||
return if node.nil? | ||
postorder_helper(array, node.left) | ||
postorder_helper(array, node.right) | ||
# hash = { key: node.key, value: node.value } | ||
array.push({ key: node.key, value: node.value }) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) because worst case each node will need to be visited to be counted | ||
# Space Complexity: O(1) | ||
def height | ||
raise NotImplementedError | ||
return 0 if @root.nil? | ||
node = @root | ||
return height_helper(@root) | ||
end | ||
|
||
def height_helper(node) | ||
return 0 if node.nil? | ||
left = height_helper(node.left) | ||
right = height_helper(node.right) | ||
|
||
if left >= right | ||
return left + 1 | ||
else | ||
return right + 1 | ||
end | ||
end | ||
|
||
# Optional Method | ||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: | ||
# Space Complexity: | ||
def bfs | ||
raise NotImplementedError | ||
array = [] | ||
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 it's a start! |
||
return array if @root.nil? | ||
end | ||
|
||
def bfs_helper(node, level, array) | ||
end | ||
|
||
# Useful for printing | ||
|
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
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.
It will be O(log n) if the tree is balanced but O(n) if the tree is totally unbalanced.