-
Notifications
You must be signed in to change notification settings - Fork 44
Earth - Kal #36
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?
Earth - Kal #36
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 |
|---|---|---|
|
|
@@ -2,12 +2,12 @@ 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 | ||
|
|
@@ -16,47 +16,127 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| # Time Complexity: O(log n) if balanced, otherwise O(n) where n is number of nodes | ||
| # Space Complexity: O(n), n is height | ||
| def add(key, value=nil) | ||
| new_node = TreeNode.new(key, value) | ||
|
|
||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def add_helper(curr_node, new_node) | ||
| return new_node if curr_node.nil? | ||
|
|
||
| if new_node.key <= curr_node.key | ||
| curr_node.left = add_helper(curr_node.left, new_node) | ||
| else | ||
| curr_node.right = add_helper(curr_node.right, new_node) | ||
| end | ||
|
|
||
| return curr_node | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) if balanced | ||
| # Space Complexity: O(n) | ||
| def find(key) | ||
|
Comment on lines
+43
to
45
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. 👍 I assume space complexity is related to the height. |
||
| raise NotImplementedError | ||
| find_helper(@root, key) #starts at root | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def find_helper(current, key) | ||
| return if current.nil? | ||
| if key == current.key | ||
| return current.value | ||
| elsif key < current.key | ||
| find_helper(current.left, key) | ||
| else | ||
| find_helper(current.right, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(n), n is number of nodes | ||
| # Space Complexity: O(n) | ||
| def inorder | ||
|
Comment on lines
+60
to
62
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 inorder_helper(@root, []) | ||
| end | ||
|
|
||
| def inorder_helper(current, list) | ||
| return list if current.nil? | ||
| inorder_helper(current.left, list) | ||
| list << {key: current.key, value: current.value} | ||
| inorder_helper(current.right, list) | ||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def preorder | ||
|
Comment on lines
+74
to
76
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 preorder_helper(@root, []) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(current, list) | ||
| return list if current.nil? | ||
| list << {key: current.key, value: current.value} | ||
| preorder_helper(current.left, list) | ||
| preorder_helper(current.right, list) | ||
| end | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def postorder | ||
|
Comment on lines
+87
to
89
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 | ||
|
|
||
| def postorder_helper(current, list) | ||
| return list if current.nil? | ||
| postorder_helper(current.left, list) | ||
| postorder_helper(current.right, list) | ||
| list << {key: current.key, value: current.value} | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height | ||
| raise NotImplementedError | ||
| def height(current = @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. 👍 Time/space complexity? |
||
| return 0 if current.nil? | ||
| return 1 if current.right.nil? && current.left.nil? | ||
|
|
||
| left_count = 0 | ||
| right_count = 0 | ||
| left_count += 1 + height(current.left) | ||
| right_count += 1 + height(current.right) | ||
| left_count > right_count ? left_count : right_count | ||
| end | ||
|
|
||
| def height_helper(node, side, count) | ||
| return count if node.nil? | ||
|
|
||
| count += 1 | ||
| if side == 'left' | ||
| height_helper(node.left, 'left', count) | ||
| else | ||
| height_helper(node.right, 'right', count) | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
|
Comment on lines
125
to
127
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. Yeah BFS! Time/space complexity |
||
| raise NotImplementedError | ||
| return [] if @root.nil? | ||
| queue = [] | ||
| list = [] | ||
| queue.push(@root) | ||
| while queue.length > 0 | ||
| current = queue[0] | ||
| list << {key: current.key, value: current.value} | ||
| queue.push(current.left) if current.left | ||
| queue.push(current.right) if current.right | ||
| queue.shift | ||
| end | ||
| return list | ||
| end | ||
|
|
||
| # Useful for printing | ||
|
|
||
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.
👍 Yeah recursion! You threw me with the O(n) space complexity until I saw the note on height.