-
Notifications
You must be signed in to change notification settings - Fork 44
Ports - Ngoc #29
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 - Ngoc #29
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 |
|---|---|---|
|
|
@@ -16,51 +16,155 @@ def initialize | |
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
|
|
||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| puts "" | ||
| new_node = TreeNode.new(key, value) | ||
| if @root.nil? | ||
| @root = new_node | ||
| else | ||
| add_helper(@root, new_node) | ||
| end | ||
| end | ||
|
|
||
| def add_helper(current, new_node) | ||
| if new_node.key <= current.key | ||
| if current.left.nil? | ||
| current.left = new_node | ||
| else | ||
| add_helper(current.left, new_node) | ||
| end | ||
| else | ||
| if current.right.nil? | ||
| current.right = new_node | ||
| else | ||
| add_helper(current.right, new_node) | ||
| end | ||
| end | ||
| end | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(1) | ||
| def find_helper(current,key) | ||
| return nil if current.nil? | ||
| if key == current.key | ||
| return current.value | ||
| end | ||
| if key < current.key | ||
| return find_helper(current.left, key) | ||
| end | ||
| if key > current.key | ||
| return find_helper(current.right, key) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def find(key) | ||
| raise NotImplementedError | ||
| return find_helper(@root,key) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) | ||
| # Space Complexity: O(n) - an new array is created to store the in-order nodes | ||
|
|
||
| def inorder | ||
| raise NotImplementedError | ||
| inorder_array = [] | ||
| return inorder_array if @root == nil | ||
| current = @root | ||
| return inorder_helper(current, inorder_array) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def inorder_helper(current, inorder_array) | ||
| if current == nil | ||
| return inorder_array | ||
| else | ||
| inorder_helper(current.left, inorder_array) | ||
| inorder_array.push({:key => current.key, :value => current.value}) | ||
| inorder_helper(current.right, inorder_array) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) | ||
|
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. O(n) |
||
| # Space Complexity: O(n) | ||
| def preorder | ||
| raise NotImplementedError | ||
| array = [] | ||
| return array if @root == nil | ||
| current = @root | ||
| return preorder_helper(current, array) | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder_helper(current, array) | ||
| if current == nil | ||
| return array | ||
| else | ||
| array.push({:key => current.key, :value => current.value}) | ||
| preorder_helper(current.left, array) | ||
| preorder_helper(current.right, array) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: O(log n) | ||
|
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. O(n) |
||
| # Space Complexity: O(n) | ||
| def postorder | ||
| raise NotImplementedError | ||
| array = [] | ||
| return array if @root == nil | ||
| current = @root | ||
| return postorder_helper(current, array) | ||
| end | ||
|
|
||
| def postorder_helper(current, array) | ||
| if current == nil | ||
| return array | ||
| else | ||
| postorder_helper(current.left, array) | ||
| postorder_helper(current.right, array) | ||
| array.push({:key => current.key, :value => current.value}) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height | ||
| raise NotImplementedError | ||
| current = @root | ||
| height = 1 | ||
| return 0 if @root.nil? | ||
| return height_helper(current,height) | ||
| end | ||
|
|
||
| def height_helper(current, 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 example of tail recursion |
||
| if current.nil? | ||
| return height | ||
| else | ||
| height += 1 | ||
| height_helper(current.left,height) | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def bfs | ||
| raise NotImplementedError | ||
| queue = Queue.new | ||
| if @root.nil? | ||
| return [] | ||
| end | ||
| array = [] | ||
| queue.push(@root) | ||
| while !queue.empty? | ||
| node = queue.pop | ||
|
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. If you're doing pushes and pops, this isn't a Queue it's a stack. Thus This isn't going to be doing BFS |
||
| array.push({:key => node.key, :value => node.value}) | ||
| if !node.left.nil? | ||
| queue.push(node.left) | ||
| end | ||
| if !node.right.nil? | ||
| queue.push(node.right) | ||
| end | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| # Useful for printing | ||
| def to_s | ||
| return "#{self.inorder}" | ||
| end | ||
| end | ||
|
|
||
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.
Actually the time complexity is O(n) because you visit every node.