-
Notifications
You must be signed in to change notification settings - Fork 44
Margaret #22
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?
Margaret #22
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,59 +2,168 @@ 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 | ||
|
|
||
| def inorder(array) | ||
| #left, root, right | ||
| if @left != nil | ||
| @left.inorder(array) | ||
| end | ||
|
|
||
| array << {key: @key, value: @value} | ||
|
|
||
| if @right != nil | ||
| @right.inorder(array) | ||
| end | ||
| end | ||
|
|
||
| def preorder(array) | ||
| #Root, Left, Right | ||
| array << {key: @key, value: @value} | ||
|
|
||
| if @left != nil | ||
| @left.preorder(array) | ||
| end | ||
|
|
||
| if @right != nil | ||
| @right.preorder(array) | ||
| end | ||
| end | ||
|
|
||
| def postorder(array) | ||
| #left, right, root | ||
|
|
||
| if @left != nil | ||
| @left.postorder(array) | ||
| end | ||
|
|
||
| if @right != nil | ||
| @right.postorder(array) | ||
| end | ||
|
|
||
| array << {key: @key, value: @value} | ||
| end | ||
| end | ||
|
|
||
| class Tree | ||
| attr_reader :root | ||
|
|
||
| def initialize | ||
| @root = nil | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) if it is balanced, O(n) if it is not | ||
| # Space Complexity: Constant | ||
| def add(key, value) | ||
| raise NotImplementedError | ||
| 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 current == nil | ||
| return current = new_node | ||
| end | ||
|
|
||
| if new_node.key <= current.key | ||
| current.left = add_helper(current.left, new_node) | ||
| else | ||
| current.right = add_helper(current.right, new_node) | ||
| end | ||
| return current | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(log n) if it is balanced, O(n) if it is not balanced | ||
| # Space Complexity: constant | ||
| def find(key) | ||
| raise NotImplementedError | ||
| current = @root | ||
|
|
||
| if current == nil | ||
| return nil | ||
| end | ||
|
|
||
| while current != nil | ||
| if key == current.key | ||
| return current.value | ||
| elsif key <= current.key | ||
| current = current.left | ||
| else | ||
| current = current.right | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space 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. Since you are building an array in this example, it would be O(n) Same for all the traversals |
||
| def inorder | ||
| raise NotImplementedError | ||
| list = [] | ||
| if @root != nil | ||
| @root.inorder(list) | ||
| end | ||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity:O(n) | ||
| # Space Complexity:O(log n) | ||
| def preorder | ||
| raise NotImplementedError | ||
| #Root, Left, Right | ||
| list = [] | ||
|
|
||
| if @root == nil #done | ||
| return [] | ||
| else | ||
| @root.preorder(list) | ||
| end | ||
| return list | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: O(n) | ||
| # Space Complexity:O(log n) | ||
| def postorder | ||
| raise NotImplementedError | ||
| #left, right, root | ||
| list = [] | ||
|
|
||
| if @root == nil | ||
| return [] | ||
| else | ||
| @root.postorder(list) | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height | ||
| raise NotImplementedError | ||
| # Time Complexity:O(n) | ||
| # Space Complexity:O(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. Space complexity would be O(n) if the tree is widely unbalanced, but O(log n) if it is balanced. |
||
| def height(current = @root) | ||
| return 0 if current == nil | ||
| heightLeft = 0 | ||
| heightRight = 0 | ||
|
|
||
| if current.left != nil | ||
| heightLeft = height(current.left) | ||
| end | ||
|
|
||
| if current.right != nil | ||
| heightRight = height(current.right) | ||
| end | ||
|
|
||
| if heightLeft < heightRight | ||
| return heightRight + 1 | ||
| else | ||
| return heightLeft + 1 | ||
| end | ||
| end | ||
|
|
||
| # Optional Method | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| # Time Complexity: | ||
| # Space Complexity: | ||
| def bfs | ||
| raise NotImplementedError | ||
| 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 since
add_helperis recursive the space complexity will be O(log n) if the tree is balanced and O(n) if it's not due to the function call stack.