forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Sockets - Karla #24
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
kguadron
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
kguadron: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
Sockets - Karla #24
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 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 |
|---|---|---|
|
|
@@ -2,61 +2,157 @@ 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.nil? | ||
| return @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| def add_helper(current, new_node) | ||
| if current.nil? | ||
| return new_node | ||
| elsif new_node.key <= current.key | ||
| current.left = add_helper(current.left, new_node) | ||
| else | ||
| current.right = add_helper(current.right, new_node) | ||
| end | ||
| return current | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log(n)) | ||
| # Space Complexity: O(1) | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return nil if @root.nil? | ||
|
|
||
| current = @root | ||
| return find_helper(current, key) | ||
| end | ||
|
|
||
| def find_helper(root, key_to_find) | ||
| if root.nil? | ||
| return nil | ||
| elsif root.key == key_to_find | ||
| return root.value | ||
| elsif key_to_find < root.key | ||
| root.left = find_helper(root.left, key_to_find) | ||
| elsif key_to_find > root.key | ||
| root.right = find_helper(root.right, key_to_find) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
| raise NotImplementedError | ||
| inorder_array = [] | ||
| current = @root | ||
|
|
||
| return inorder_array if current.nil? | ||
|
|
||
| return inorder_helper(current, inorder_array) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_helper(current, tree_array) | ||
| return tree_array if current.nil? | ||
|
|
||
| inorder_helper(current.left, tree_array) | ||
| tree_array.push({ :key => current.key, | ||
| :value => current.value }) | ||
| inorder_helper(current.right, tree_array) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder | ||
| raise NotImplementedError | ||
| preorder_array = [] | ||
| current = @root | ||
|
|
||
| return preorder_array if current.nil? | ||
|
|
||
| return preorder_helper(current, preorder_array) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(current, tree_array) | ||
| return tree_array if current.nil? | ||
|
|
||
| tree_array.push({ :key => current.key, | ||
| :value => current.value }) | ||
| preorder_helper(current.left, tree_array) | ||
| preorder_helper(current.right, tree_array) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
| raise NotImplementedError | ||
| postorder_array = [] | ||
| current = @root | ||
|
|
||
| return postorder_array if current.nil? | ||
|
|
||
| return postorder_helper(current, postorder_array) | ||
| end | ||
|
|
||
| def postorder_helper(current, tree_array) | ||
| return tree_array if current.nil? | ||
|
|
||
| postorder_helper(current.left, tree_array) | ||
| postorder_helper(current.right, tree_array) | ||
| tree_array.push({ :key => current.key, | ||
| :value => current.value }) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity:O(1) | ||
| def height | ||
| raise NotImplementedError | ||
| current = @root | ||
|
|
||
| return 0 if current.nil? | ||
|
|
||
| height_helper(current) | ||
| end | ||
|
|
||
| def height_helper(current) | ||
| return 0 if current.nil? | ||
|
|
||
| right = height_helper(current.right) | ||
| left = height_helper(current.left) | ||
|
|
||
| if right > left | ||
| return right + 1 | ||
| else | ||
| return left + 1 | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| bfs_array = [] | ||
| current = @root | ||
| return bfs_array if current.nil? | ||
|
|
||
| # will come back to after queue exercises | ||
|
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. No problem |
||
| end | ||
|
|
||
| # Useful for printing | ||
|
|
||
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.
Since you are using a recursive
add_helperyou actually have O(log n) space complexity. Remember recursion does involve the system stack.This also applies to your other recursive methods.