-
Notifications
You must be signed in to change notification settings - Fork 44
Ports - Elle #9
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
base: master
Are you sure you want to change the base?
Ports - Elle #9
Changes from all commits
cc7a3f6
7bdae56
50c6db6
e44e3df
66256e3
d2ff7b9
1c0c6a9
a50f83c
e8dc921
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,45 +16,127 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(logn) if the tree is balanced (best case), O(n) if tree is unbalanced (worst case) | ||
| # Space Complexity: O(1) | ||
| def add(key, value) | ||
|
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. This method could use a bit of a refactor. |
||
| raise NotImplementedError | ||
| if !@root | ||
| new_node = TreeNode.new(key, value) | ||
| @root = new_node | ||
| return | ||
| end | ||
|
|
||
| current = @root | ||
| new_node = TreeNode.new(key, value) | ||
|
|
||
| until !current | ||
| if new_node.key <= current.key # GO LEFT | ||
| if !current.left | ||
| current.left = new_node | ||
| return | ||
| else | ||
| current = current.left | ||
| end | ||
| else # new_node.value > current.value # GO RIGHT | ||
| if !current.right | ||
| current.right = new_node | ||
| return | ||
| else | ||
| current = current.right | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(logn) where n is the length of the array if the tree is balanced. O(n) if not. | ||
| # Space Complexity: O(1) | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return nil if !@root | ||
| current = @root | ||
|
|
||
| until current == nil | ||
| return current.value if key == current.key | ||
| key < current.key ? current = current.left : current = current.right | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n-squared) | ||
| # Space Complexity: O(n) where n is the height of the recursion stack | ||
| def inorder | ||
| raise NotImplementedError | ||
| @output = [] | ||
| current = @root | ||
|
|
||
| return @output if !@root | ||
|
|
||
| def analyzeInorder(current) | ||
|
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. An inner method? I suggest either putting this method in |
||
| analyzeInorder(current.left) if current.left | ||
| @output << {:key => current.key, :value => current.value} | ||
| analyzeInorder(current.right) if current.right | ||
| end | ||
|
|
||
| analyzeInorder(current) | ||
|
|
||
| return @output | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n-squared) | ||
|
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. You hit each node just once, so I would say O(n) instead. |
||
| # Space Complexity: O(n) where n is the height of the recursion stack | ||
| def preorder | ||
| raise NotImplementedError | ||
| @output = [] | ||
| current = @root | ||
|
|
||
| return @output if !@root | ||
|
|
||
| def analyzePreorder(current) | ||
| @output << {:key => current.key, :value => current.value} | ||
| analyzePreorder(current.left) if current.left | ||
| analyzePreorder(current.right) if current.right | ||
| end | ||
|
|
||
| analyzePreorder(current) | ||
|
|
||
| return @output | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n-squared) | ||
| # Space Complexity: O(n) where n is the height of the recursion stack | ||
| def postorder | ||
| raise NotImplementedError | ||
| @output = [] | ||
| current = @root | ||
|
|
||
| return @output if !@root | ||
|
|
||
| def analyzePostorder(current) | ||
| analyzePostorder(current.left) if current.left | ||
| analyzePostorder(current.right) if current.right | ||
| @output << {:key => current.key, :value => current.value} | ||
| end | ||
|
|
||
| analyzePostorder(current) | ||
|
|
||
| return @output | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) if tree is balanced, O(n) if unbalanced | ||
| # Space Complexity: O(1) | ||
| def height | ||
| raise NotImplementedError | ||
| return 0 if !@root | ||
|
|
||
| def findMaxHeight(node) | ||
| return 0 if !node | ||
|
|
||
| height_left = findMaxHeight(node.left) | ||
| height_right = findMaxHeight(node.right) | ||
|
|
||
| return height_left > height_right ? (height_left + 1) : (height_right + 1) | ||
| end | ||
|
|
||
| findMaxHeight(@root) | ||
|
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. Just for readability adding a return here would be good. |
||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
👍