-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Lak #17
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?
Space - Lak #17
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,40 +16,94 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def add(key, value) | ||
raise NotImplementedError | ||
# raise NotImplementedError | ||
# if the root is nil set the root to be a new node with the given value and return the node. | ||
return @root = TreeNode.new(key, value) if @root.nil? | ||
current = @root | ||
while current != nil | ||
if key <= current.key | ||
if current.left.nil? | ||
current.left = TreeNode.new(key, value) | ||
return | ||
end | ||
current = current.left | ||
else | ||
if current.right.nil? | ||
current.right = TreeNode.new(key, value) | ||
return | ||
end | ||
current = current.right | ||
end | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
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. 👍 |
||
raise NotImplementedError | ||
return nil if @root.nil? | ||
current = @root | ||
while current != nil | ||
if key == current.key | ||
return current.value | ||
elsif key > current.key | ||
current = current.right | ||
else | ||
current = current.left | ||
end | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
raise NotImplementedError | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def inorder(current = @root, array = []) | ||
Comment on lines
+59
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. 👍 |
||
# traverse the left subtree | ||
# visit the current node | ||
# traverse the right subtree | ||
return array if current.nil? | ||
inorder(current.left, array) | ||
array << { key: current.key, value: current.value } | ||
inorder(current.right, array) | ||
|
||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
raise NotImplementedError | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(current = @root, array = []) | ||
Comment on lines
+73
to
+75
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. 👍 |
||
# visit the current node | ||
# traverse the left subtree | ||
# traverse the right subtree | ||
return array if current.nil? | ||
array << { key: current.key, value: current.value } | ||
preorder(current.left, array) | ||
preorder(current.right, array) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder | ||
raise NotImplementedError | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def postorder(current = @root, array = []) | ||
Comment on lines
+85
to
+88
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. 👍 |
||
# traverse the left subtree | ||
# traverse the right subtree | ||
# visit the current node | ||
return array if current.nil? | ||
postorder(current.left, array) | ||
postorder(current.right, array) | ||
array << { key: current.key, value: current.value } | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height(current = @root) | ||
Comment on lines
+98
to
+100
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. 👍 The space complexity is O(log n) assuming the tree is balanced and O(n) if it's unbalanced. |
||
# If the current node is nil return 0 | ||
# Otherwise return 1 plus the maximum of the heights of the right and left subtrees | ||
return 0 if current.nil? | ||
left = height(current.left) | ||
right = height(current.right) | ||
left >= right ? 1 + left : 1 + right | ||
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.
👍 , the time complexity is O(log n) assuming the tree is balanced.