forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Ports - Sopheary #16
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
sophearychiv
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
sophearychiv: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
Ports - Sopheary #16
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 |
|---|---|---|
|
|
@@ -16,47 +16,149 @@ 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? | ||
| @root = new_node | ||
| return | ||
| end | ||
|
|
||
| add_helper(@root, new_node) | ||
| end | ||
|
|
||
| def add_helper(tree_node, new_node) | ||
| if tree_node.nil? | ||
| tree_node = new_node | ||
| end | ||
|
|
||
| return tree_node if tree_node.key == new_node.key | ||
|
|
||
| if new_node.key < tree_node.key | ||
| if tree_node.left.nil? | ||
| tree_node.left = new_node | ||
| else | ||
| add_helper(tree_node.left, new_node) | ||
| end | ||
| end | ||
|
|
||
| if new_node.key > tree_node.key | ||
| if tree_node.right.nil? | ||
| tree_node.right = new_node | ||
| else | ||
| add_helper(tree_node.right, new_node) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return nil if @root.nil? | ||
| return find_helper(@root, key) | ||
| end | ||
|
|
||
| def find_helper(tree_node, key) | ||
| return nil if tree_node.nil? | ||
| return tree_node.value if tree_node.key == key | ||
| return find_helper(tree_node.left, key) if key < tree_node.key | ||
| return find_helper(tree_node.right, key) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) where n is the number of nodes | ||
| # Space Complexity: O(n) where n is the number of nodes | ||
| def inorder | ||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return inorder_helper(@root) | ||
| end | ||
|
|
||
| def inorder_helper(tree_node, array = []) | ||
| return array if tree_node.nil? | ||
| if tree_node | ||
| inorder_helper(tree_node.left, array) | ||
| array << {key: tree_node.key, value: tree_node.value} | ||
| inorder_helper(tree_node.right, array) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) where n is the number of nodes | ||
| # Space Complexity: O(n) where n is the number of nodes | ||
| def preorder | ||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return preorder_helper(@root) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(root) | ||
| return if root.nil? | ||
| array = [] | ||
| array << root | ||
| nodeStack = [] | ||
|
|
||
| while array.length > 0 | ||
| node = array.pop | ||
| nodeStack << {key: node.key, value: node.value} | ||
| if node.right != nil | ||
| array << node.right | ||
| end | ||
|
|
||
| if node.left != nil | ||
| array << node.left | ||
| end | ||
| end | ||
| return nodeStack | ||
| end | ||
|
|
||
| # Time Complexity: O(n) where n is the number of nodes | ||
| # Space Complexity: O(n) where n is the length of the tree | ||
| def postorder | ||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| return postorder_helper(@root) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder_helper(tree_node, array = []) | ||
| return array if tree_node.nil? | ||
|
|
||
| if tree_node | ||
| postorder_helper(tree_node.left, array) | ||
| postorder_helper(tree_node.right, array) | ||
| array << {key: tree_node.key, value: tree_node.value} | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(n) where n is the number of nodes | ||
| # Space Complexity: O(1) | ||
| def height | ||
| raise NotImplementedError | ||
| return 0 if @root.nil? | ||
| return height_helper(@root) | ||
| end | ||
|
|
||
| def height_helper(tree_node) | ||
| return 0 if tree_node.nil? | ||
| left_height = height_helper(tree_node.left) | ||
| right_height = height_helper(tree_node.right) | ||
| if left_height > right_height | ||
| return left_height + 1 | ||
| else | ||
| return right_height + 1 | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) where n is the number of nodes | ||
| # Space Complexity: O(n) where is is the length of the tree | ||
| def bfs | ||
|
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. Niiice! |
||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| queue = [] | ||
| output = [] | ||
| queue << @root | ||
| while (!queue.empty?) | ||
| current = queue.shift | ||
| if(current.left) then queue.push(current.left) end | ||
| if(current.right) then queue.push(current.right) end | ||
| output.push({key: current.key, value: current.value}) | ||
| 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.
Actually since you're doing a recursive solution the system stack will be of size O(log n) if the tree is balanced and O(n) otherwise.
Also your time complexity will be the same.