-
Notifications
You must be signed in to change notification settings - Fork 43
Branches, C. Gutierrez #43
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?
Changes from all commits
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 |
|---|---|---|
| @@ -1,64 +1,177 @@ | ||
| 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(n) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
|
|
||
| current = @root | ||
| temp = TreeNode.new(key, value) | ||
|
|
||
| if @root == nil | ||
| @root = temp | ||
| return | ||
|
|
||
| else | ||
| add_helper(@root, temp) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| def add_helper(current_node, temp) | ||
| if current_node == nil | ||
| return temp | ||
| end | ||
|
|
||
| if temp.key < current_node.key | ||
| current_node.left = add_helper(current_node.left, temp) | ||
| else | ||
| current_node.right = add_helper(current_node.right, temp) | ||
| end | ||
| return current_node | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(n) | ||
| def find(key) | ||
|
Comment on lines
+48
to
50
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. Similar to add with space complexity. |
||
| raise NotImplementedError | ||
| current = @root | ||
| if current == nil | ||
| return nil | ||
| elsif current.key == key | ||
| return current.value | ||
| else | ||
| find_helper(current, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| def find_helper (current, key) | ||
| if current.key == key | ||
| return current.value | ||
| elsif current.key > key && current.left != nil | ||
| find_helper(current.left, key) | ||
| elsif current.key > key | ||
| return nil | ||
| elsif current.key < key && current.right != nil | ||
| find_helper(current.right, key) | ||
| elsif current.key < key | ||
| return nil | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
|
Comment on lines
+75
to
77
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. 👍 |
||
| raise NotImplementedError | ||
| if @root == nil | ||
| return [] | ||
| end | ||
| inorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| def inorder_helper(current_node, list) | ||
|
|
||
| if current_node == nil | ||
| return list | ||
| end | ||
|
|
||
| inorder_helper(current_node.left, list) | ||
| list.push({key: current_node.key, value: current_node.value}) | ||
| inorder_helper(current_node.right, list) | ||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder | ||
|
Comment on lines
+96
to
98
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. 👍 |
||
| raise NotImplementedError | ||
| if @root == nil | ||
| return [] | ||
| else | ||
| return preorder_helper(@root, []) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| def preorder_helper(current_node, list) | ||
| if current_node == nil | ||
| return list | ||
| end | ||
|
|
||
| list.push({key: current_node.key, value: current_node.value}) | ||
| preorder_helper(current_node.left, list) | ||
| preorder_helper(current_node.right, list) | ||
|
|
||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
|
Comment on lines
+118
to
120
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. 👍 |
||
| raise NotImplementedError | ||
| if @root == nil | ||
| return [] | ||
| else | ||
| return postorder_helper(@root, []) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| def postorder_helper(current_node, list) | ||
| if current_node == nil | ||
| return list | ||
| end | ||
|
|
||
| postorder_helper(current_node.left, list) | ||
| postorder_helper(current_node.right, list) | ||
| list.push({key: current_node.key, value: current_node.value}) | ||
|
|
||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def height | ||
|
Comment on lines
+140
to
142
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're visiting each node so the time complexity is O(n), but because the call stack doesn't go deeper than the height of the tree the space complexity is O(log n) for a balanced tree and O(n) for an unbalanced one. |
||
| raise NotImplementedError | ||
| if @root == nil | ||
| return 0 | ||
| elsif @root.left == nil && @root.right == nil | ||
| return 1 | ||
| end | ||
|
|
||
| return height_helper(@root) | ||
|
|
||
| end | ||
|
|
||
|
|
||
| def height_helper(current_node) | ||
|
|
||
| if current_node == nil | ||
| return 0 | ||
| end | ||
|
|
||
| left = height_helper(current_node.left) | ||
| right = height_helper(current_node.right) | ||
| if left > right | ||
| return left + 1 | ||
| else | ||
| return right + 1 | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| # Useful for printing | ||
| def to_s | ||
| return "#{self.inorder}" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
|
|
||
| require "minitest" | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
| require "minitest/skip_dsl" | ||
|
|
||
|
|
||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| require_relative "../lib/tree" | ||
| require "minitest" | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
| require "minitest/skip_dsl" | ||
|
|
||
|
|
||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| require_relative "../lib/tree" | ||
|
|
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.
Space complexity
n? Why?Otherwise the method works.