-
Notifications
You must be signed in to change notification settings - Fork 44
Diana - Space #35
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?
Diana - Space #35
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,54 +2,105 @@ class TreeNode | |
attr_reader :key, :value | ||
attr_accessor :left, :right | ||
|
||
def initialize(key, val) | ||
@key = key | ||
@value = val | ||
@left = nil | ||
@right = nil | ||
end | ||
end | ||
|
||
class Tree | ||
attr_reader :root | ||
def initialize | ||
@root = nil | ||
def initialize(key, val) | ||
@key = key | ||
@value = val | ||
@left = nil | ||
@right = nil | ||
end | ||
end | ||
|
||
class Tree | ||
attr_reader :root | ||
def initialize | ||
@root = nil | ||
end | ||
|
||
# Time Complexity: worst case unbalanced-O(n), bal-O(log n) | ||
# Space Complexity: worst case-O(n), bal-O(log n) | ||
def add(key, value) | ||
if @root.nil? | ||
@root = TreeNode.new(key, value) | ||
else | ||
self.add_helper(@root, key, value) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(key, value) | ||
raise NotImplementedError | ||
end | ||
def add_helper(node, new_key, new_value) | ||
if new_key <= node.key | ||
node.left.nil? ? node.left = TreeNode.new(new_key, new_value) : add_helper(node.left, new_key, new_value) | ||
else | ||
node.right.nil? ? node.right = TreeNode.new(new_key, new_value) : add_helper(node.right, new_key, new_value) | ||
end | ||
end | ||
|
||
# Time Complexity: unbal-O(n), bal-O(log n) | ||
# Space Complexity: unbal-O(n), bal-O(log n) | ||
def find(key, node=@root) | ||
Comment on lines
+37
to
+39
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 nil if @root.nil? | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
raise NotImplementedError | ||
if key < node.key | ||
find(key, node.left) | ||
elsif key > node.key | ||
find(key, node.right) | ||
else | ||
return node.value | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder | ||
Comment on lines
+52
to
54
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 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(node, array) | ||
return array if node.nil? | ||
|
||
inorder_helper(node.left, array) | ||
array << {:key=>node.key, :value=>node.value} | ||
inorder_helper(node.right, array) | ||
|
||
return array | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder | ||
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. 👍 |
||
raise NotImplementedError | ||
return preorder_helper(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(node, array) | ||
return [] if node.nil? | ||
|
||
array << {:key=>node.key, :value=>node.value} | ||
preorder_helper(node.left, array) | ||
preorder_helper(node.right, array) | ||
|
||
return array | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder | ||
Comment on lines
+84
to
86
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: | ||
def height | ||
raise NotImplementedError | ||
def postorder_helper(node, array) | ||
return [] if node.nil? | ||
|
||
postorder_helper(node.left, array) | ||
postorder_helper(node.right, array) | ||
array << {:key=>node.key, :value=>node.value} | ||
|
||
return array | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def height(node=@root) | ||
Comment on lines
+100
to
+102
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. 👍 , very compact! |
||
node.nil? ? 0 : (1 + [height(node.right), height(node.left)].max) | ||
end | ||
|
||
# Optional Method | ||
|
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.
👍