forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Maria W #23
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
MariaWissler
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
MariaWissler: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
Maria W #23
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,192 @@ 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 h) if balanced | ||
| # Space Complexity: O(log h) id balanced | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| new_node = TreeNode.new(key, value) | ||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| def add_helper(current, new_node) | ||
| if new_node.key <= current.key | ||
| if current.left | ||
| add_helper(current.left, new_node) | ||
| else | ||
| current.left = new_node | ||
| end | ||
| else | ||
| if current.right | ||
| add_helper(current.right, new_node) | ||
| else | ||
| current.right = new_node | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log h) if balanced | ||
| # Space Complexity: O(1) | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return nil if @root.nil? | ||
| current = @root | ||
| while !current.nil? | ||
| if key == current.key | ||
| return current.value | ||
| elsif key <= current.key | ||
| current = current.left | ||
| else | ||
| current = current.right | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n^2) | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
| raise NotImplementedError | ||
| current = root | ||
| stack = [] | ||
| output = [] | ||
| while current || !stack.empty? | ||
| while !current.nil? | ||
| stack.push(current) | ||
| current = current.left | ||
| end | ||
| current = stack.pop | ||
| new_hash = Hash.new | ||
| new_hash[:key] = current.key | ||
| new_hash[:value] = current.value | ||
| output.push(new_hash); #storing the data | ||
| current = current.right | ||
| end | ||
| return output | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) if balanced | ||
| # Space Complexity: O(h) | ||
| def preorder | ||
| raise NotImplementedError | ||
| #raise NotImplementedError | ||
| stack = [] | ||
| output = [] | ||
| current = @root | ||
| return [] if current.nil? | ||
| stack.push(current) | ||
| while !stack.empty? | ||
| current = stack.pop() | ||
| new_hash = Hash.new | ||
| new_hash[:key] = current.key | ||
| new_hash[:value] = current.value | ||
| output.push(new_hash) | ||
| if current.right | ||
| stack.push(current.right) | ||
| end | ||
| if current.left | ||
| stack.push(current.left) | ||
| end | ||
| end | ||
| return output | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log h) if balanced | ||
| # Space Complexity: O(h) | ||
| def postorder | ||
| raise NotImplementedError | ||
| #raise NotImplementedError | ||
| stack = [] | ||
| output = [] | ||
| return [] if @root.nil? | ||
| stack.push(@root) | ||
| while !stack.empty? | ||
| current = stack.pop() | ||
| new_hash = Hash.new | ||
| new_hash[:key] = current.key | ||
| new_hash[:value] = current.value | ||
| output.push(new_hash) | ||
| if current.left | ||
| stack.push(current.left) | ||
| end | ||
| if current.right | ||
| stack.push(current.right) | ||
| end | ||
| end | ||
| return output.reverse | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(h) | ||
| def height | ||
| raise NotImplementedError | ||
| #raise NotImplementedError | ||
| stack = [] | ||
| current = @root | ||
| stack.push(current) | ||
| count_left = 0 | ||
| count_right = 0 | ||
| max_count = 0 | ||
| while !stack.empty? | ||
| current = stack.pop | ||
| if current.left | ||
| count_left += 1 | ||
| stack.push(current.left) | ||
| else | ||
| if count_left > max_count | ||
| max_count = count_left | ||
| count_left = 0 | ||
| end | ||
| end | ||
| if current.right | ||
| count_right += 1 | ||
| stack.push(current.right) | ||
| else | ||
| if count_right > max_count | ||
| max_count = count_right | ||
| count_right = 0 | ||
| end | ||
| end | ||
| end | ||
| return max_count | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log h) if balanced | ||
|
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. Since you are building a Queue and sift the 1st element of the queue off at each iteration, this is O(n2) Also since you are building an array, the space complexity is O(n) |
||
| # Space Complexity: O(h) | ||
| def bfs | ||
| raise NotImplementedError | ||
| #raise NotImplementedError | ||
| return [] if @root.nil? | ||
| queue = [] | ||
| output = [] | ||
| queue.push(@root) | ||
| while !queue.empty? | ||
| current = queue[0] | ||
| new_hash = Hash.new | ||
| new_hash[:key] = current.key | ||
| new_hash[:value] = current.value | ||
| output.push(new_hash) | ||
| if current.left | ||
| queue.push(current.left) | ||
| end | ||
| if current.right | ||
| queue.push(current.right) | ||
| end | ||
| queue = queue[1..-1] | ||
| end | ||
| return output | ||
| 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.
Nice try at an iterative solution. It's easier to do recursively. This solution doesn't work.
Remember the height of a subtree is the max of the height of the left subtree compared to the height of the right subtree plus one.