-
Notifications
You must be signed in to change notification settings - Fork 43
Leaves - Hallie Johnson #24
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,155 @@ | ||
| 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: | ||
|
|
||
| def add_helper(current_node, key, value) | ||
| return TreeNode.new(key,value) if current_node.nil? | ||
|
|
||
| if key < current_node.key | ||
| current_node.left = add_helper(current_node.left, key, value) | ||
| else | ||
| current_node.right = add_helper(current_node.right, key, value) | ||
| end | ||
|
|
||
| return current_node | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(log n) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| new_node = TreeNode.new(key,value) | ||
|
|
||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, key, value) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: Best: O(log n), Worst: O(n) | ||
| # Space Complexity: O(n) | ||
|
Comment on lines
+43
to
+44
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. Both space/time complexity are O(log n) for a balanced tree and O(n) for an unbalanced tree. |
||
| def find_helper(current_node, key) | ||
| return if current_node.nil? | ||
|
|
||
| if key < current_node.key | ||
| current_node.left = find_helper(current_node.left, key) | ||
| elsif key > current_node.key | ||
| current_node.right = find_helper(current_node.right, key) | ||
| else | ||
| return current_node.value | ||
| end | ||
|
|
||
| end | ||
|
|
||
| def find(key) | ||
| raise NotImplementedError | ||
| return if @root.nil? | ||
|
|
||
| if @root == key | ||
| return @root.value | ||
| else | ||
| find_helper(@root, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def inorder_helper(current_node, list) | ||
|
Comment on lines
+68
to
+70
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. 👍 |
||
| return list if current_node.nil? | ||
| # left side | ||
| inorder_helper(current_node.left, list) | ||
| # middle node | ||
| list << { key: current_node.key, value: current_node.value } | ||
| # right side | ||
| inorder_helper(current_node.right, list) | ||
| return list | ||
| end | ||
|
|
||
| def inorder | ||
| raise NotImplementedError | ||
| return inorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder_helper(current_node, list) | ||
|
Comment on lines
+85
to
+87
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. 👍 |
||
| return list if current_node.nil? | ||
| # middle node | ||
| list << { key: current_node.key, value: current_node.value } | ||
| # left side | ||
| preorder_helper(current_node.left, list) | ||
| # right side | ||
| preorder_helper(current_node.right, list) | ||
|
|
||
| return list | ||
| end | ||
|
|
||
| def preorder | ||
| raise NotImplementedError | ||
| return preorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder_helper(current_node, list) | ||
| return list if current_node.nil? | ||
| # left side | ||
| postorder_helper(current_node.left, list) | ||
| # right side | ||
| postorder_helper(current_node.right, list) | ||
| # middle node | ||
| list << { key: current_node.key, value: current_node.value } | ||
|
|
||
| return list | ||
| end | ||
|
|
||
| def postorder | ||
|
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 | ||
| return postorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity:O(n) | ||
| def height_helper(current_node) | ||
|
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 work, the space complexity however is O(log n) for a balanced tree and O(n) for an unbalanced tree. |
||
| if current_node.nil? | ||
| return 0 | ||
| else | ||
| left = height_helper(current_node.left) | ||
| right = height_helper(current_node.right) | ||
|
|
||
| if (left > right) | ||
| return left + 1 | ||
| else | ||
| return right + 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def height | ||
| raise NotImplementedError | ||
| if @root.nil? | ||
| return 0 | ||
| else | ||
| height_helper(@root) | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| # Useful for printing | ||
| def to_s | ||
| return "#{self.inorder}" | ||
|
|
||
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.
👍