forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Ports - Jillianne #28
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
Open
jillirami
wants to merge
6
commits into
Ada-C11:master
Choose a base branch
from
jillirami: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
6 commits
Select commit
Hold shift + click to select a range
9764fa1
solutions to tree
5927dc6
add and find value in binary search tree
jillirami 0356b58
add and find key
jillirami 3189a8c
preorder, inorder, postorder
jillirami 0a416cd
height of tree & tests for height
jillirami ab4bef0
breadth first search
jillirami 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,51 +16,155 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) - side eliminated | ||
| # Space Complexity: O(1) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| new_node = TreeNode.new(key, value) | ||
|
|
||
| if !@root | ||
| @root = new_node | ||
| else | ||
| current = @root | ||
| until !current | ||
| if current.key < new_node.key | ||
| if current.right | ||
| current = current.right | ||
| else | ||
| return current.right = new_node | ||
| end | ||
| else | ||
| if current.left | ||
| current = current.left | ||
| else | ||
| return current.left = new_node | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) - side eliminated | ||
| # Space Complexity: O(1) | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return nil if !@root | ||
|
|
||
| current = @root | ||
|
|
||
| until !current | ||
| return current.value if current.key == key | ||
|
|
||
| if current.key < key | ||
| current = current.right | ||
| else | ||
| current = current.left | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) - all nodes visited | ||
| # Space Complexity: O(n) - array size based on node amount | ||
| def inorder | ||
| raise NotImplementedError | ||
| return [] if !@root | ||
| values = [] | ||
| inorder_key(@root, values) | ||
| end | ||
|
|
||
| def inorder_key(current_node, array) | ||
| if !current_node | ||
| return array | ||
| else | ||
| inorder_key(current_node.left, array) | ||
| array << {key: current_node.key, value: current_node.value} | ||
| inorder_key(current_node.right, array) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) - all nodes visited | ||
| # Space Complexity: O(n) - array size based on node amount | ||
| def preorder | ||
| raise NotImplementedError | ||
| return [] if !@root | ||
| values = [] | ||
| preorder_key(@root, values) | ||
| end | ||
|
|
||
| def preorder_key(current_node, array) | ||
| if !current_node | ||
| return array | ||
| else | ||
| array << {key: current_node.key, value: current_node.value} | ||
| preorder_key(current_node.left, array) | ||
| preorder_key(current_node.right, array) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) - all nodes visited | ||
| # Space Complexity: O(n) - array size based on node amount | ||
| def postorder | ||
| raise NotImplementedError | ||
| return [] if !@root | ||
| values = [] | ||
| postorder_key(@root, values) | ||
| end | ||
|
|
||
| def postorder_key(current_node, array) | ||
| if !current_node | ||
| return array | ||
| else | ||
| postorder_key(current_node.left, array) | ||
| postorder_key(current_node.right, array) | ||
| array << {key: current_node.key, value: current_node.value} | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) - possible to visit each node | ||
| # Space Complexity: O(1) | ||
| def height | ||
| raise NotImplementedError | ||
| return 0 if !@root | ||
| height_helper(@root) | ||
| end | ||
|
|
||
| def height_helper(current) | ||
| return 0 if !current | ||
| left = height_helper(current.left) | ||
| right = height_helper(current.right) | ||
|
|
||
| if left < right | ||
| return right + 1 | ||
| else | ||
| return left + 1 | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) - all nodes visited | ||
| # Space Complexity: O(n) - array size based on size of tree | ||
| def bfs | ||
| raise NotImplementedError | ||
| return [] if !@root | ||
|
|
||
| values = [{key: @root.key, value: @root.value}] | ||
| bfs_helper(@root, values) | ||
| end | ||
|
|
||
| def bfs_helper(current, array) | ||
| if current.left | ||
| array << {key: current.left.key, value: current.left.value} | ||
| end | ||
| if current.right | ||
| array << {key: current.right.key, value: current.right.value} | ||
| end | ||
| if !current.left && !current.right | ||
| return array | ||
| end | ||
| if current.left | ||
| bfs_helper(current.left, array) | ||
| end | ||
| if current.right | ||
| bfs_helper(current.right, array) | ||
| end | ||
| end | ||
|
Comment on lines
+139
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. Really nice work on this. This may help you with the Graph unit! |
||
|
|
||
| # Useful for printing | ||
| def to_s | ||
| return "#{self.inorder}" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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.
Nice iterative solution!