forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
Sarah #4
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
sjscotton
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
sjscotton: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
Sarah #4
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,59 +2,131 @@ 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) is the best case which is a balanced tree, where n is the number of nodes. Worst case would be O(n) if each nod was more than the previous, or all less than the previous, which would just be a linked list. | ||
| # Space Complexity:O(1) only constant sized variables are used | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| node = TreeNode.new(key, value) | ||
| if @root.nil? | ||
| @root = node | ||
| return | ||
| end | ||
| current = @root | ||
| until current == nil | ||
| if current.key > node.key | ||
| if current.left == nil | ||
| current.left = node | ||
| return | ||
| else | ||
| current = current.left | ||
| end | ||
| else | ||
| if current.right == nil | ||
| current.right = node | ||
| return | ||
| else | ||
| current = current.right | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) is the best case which is a balanced tree, where n is the number of nodes. Worst case would be O(n) if each nod was more than the previous, or all less than the previous, which would just be a linked list. | ||
| # Space Complexity:O(1) only constant sized variables are used | ||
| def find(key) | ||
| raise NotImplementedError | ||
| current = @root | ||
| until current == nil | ||
| if current.key > key | ||
| current = current.left | ||
| elsif current.key < key | ||
| current = current.right | ||
| else | ||
| return current.value | ||
| end | ||
| end | ||
| return nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) where n is the number of nodes. The inorder recurssive method is called once for each node, and the time complexity for each frame is constant. | ||
| # Space Complexity: Worst Case would be O(n^2) for a completely unbalanced tree, becuase you could have n stack frames and n arrays. Best case for a balances tree would be O(log n)^2 because you would have log n stack frames at once and log n arrays at once. | ||
| def inorder | ||
| raise NotImplementedError | ||
| node = @root | ||
| return inorder_recurssive(node, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_recurssive(curr_node, inorder_array) | ||
| return inorder_array if curr_node == nil | ||
|
|
||
| inorder_array = inorder_recurssive(curr_node.left, inorder_array) | ||
| inorder_array.append({ :key => curr_node.key, :value => curr_node.value }) | ||
| inorder_array = inorder_recurssive(curr_node.right, inorder_array) | ||
| return inorder_array | ||
| end | ||
|
|
||
| # Time Complexity: O(n) where n is the number of nodes. The inorder recurssive method is called once for each node, and the time complexity for each frame is constant. | ||
| # Space Complexity: Worst Case would be O(n^2) for a completely unbalanced tree, becuase you could have n stack frames and n arrays. Best case for a balances tree would be O(log n)^2 because you would have log n stack frames at once and log n arrays at once. | ||
| def preorder | ||
| raise NotImplementedError | ||
| node = @root | ||
| return preorder_recurssive(node, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_recurssive(curr_node, preorder_array) | ||
| return preorder_array if curr_node == nil | ||
| preorder_array.append({ :key => curr_node.key, :value => curr_node.value }) | ||
| preorder_array = preorder_recurssive(curr_node.left, preorder_array) | ||
| preorder_array = preorder_recurssive(curr_node.right, preorder_array) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) where n is the number of nodes. The inorder recurssive method is called once for each node, and the time complexity for each frame is constant. | ||
| # Space Complexity: Worst Case would be O(n^2) for a completely unbalanced tree, becuase you could have n stack frames and n arrays. Best case for a balances tree would be O(log n)^2 because you would have log n stack frames at once and log n arrays at once. | ||
| def postorder | ||
| raise NotImplementedError | ||
| node = @root | ||
| return postorder_recurssive(node, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder_recurssive(curr_node, postorder_array) | ||
| return postorder_array if curr_node == nil | ||
| postorder_array = postorder_recurssive(curr_node.left, postorder_array) | ||
| postorder_array = postorder_recurssive(curr_node.right, postorder_array) | ||
| postorder_array.append({ :key => curr_node.key, :value => curr_node.value }) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) where n is the number of nodes. The inorder recurssive method is called once for each node, and the time complexity for each frame is constant. | ||
| # Space Complexity: O(n) is the worst case because you would have n stack frames, where n is the number of nodes. Best case on a balamced tree would be O(log n) because you would have at most the height number of stack frames, which is log n. | ||
| def height | ||
| raise NotImplementedError | ||
| return 0 if @root == nil | ||
| node = @root | ||
| return height_recurssive(node, 1, 1) | ||
| end | ||
|
|
||
| def height_recurssive(curr_node, curr_height, max_height) | ||
|
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. Nice tail recursive method. |
||
| return max_height if curr_node == nil | ||
|
|
||
| max_height = curr_height if curr_height > max_height | ||
| max_height = height_recurssive(curr_node.left, curr_height += 1, max_height) | ||
| curr_height -= 1 | ||
| max_height = height_recurssive(curr_node.right, curr_height += 1, max_height) | ||
|
|
||
| return max_height | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| 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.
O(n2)??? You are building an array of each key-value pair, and have the system stack being called on each node, but they're not nested. Remember arrays are passed by reference.