-
Notifications
You must be signed in to change notification settings - Fork 44
Charlotte:Space:Tree-Practice #34
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?
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ gem 'minitest' | |
gem 'minitest-spec' | ||
gem 'minitest-reporters' | ||
gem "pry" | ||
gem 'pry-nav' | ||
gem 'minitest-skip' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,40 +16,101 @@ def initialize | |
@root = nil | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def add_recursive(node, key, value) | ||
Comment on lines
+19
to
+21
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 TreeNode.new(key, value) if node.nil? | ||
if key < node.key | ||
node.left = add_recursive(node.left, key, value) | ||
else | ||
node.right = add_recursive(node.right, key, value) | ||
end | ||
return node | ||
end | ||
|
||
def add(key, value) | ||
raise NotImplementedError | ||
@root = add_recursive(@root, key, value) | ||
end | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find_recursive(node, key) | ||
Comment on lines
+35
to
+37
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 node.value if node.key == key | ||
|
||
if key <= node.key | ||
node.left = find_recursive(node.left, key) | ||
else | ||
node.right = find_recursive(node.right, key) | ||
end | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(key) | ||
raise NotImplementedError | ||
current = @root | ||
return nil if !current | ||
|
||
return find_recursive(root, key) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder_recursive(node, list) | ||
Comment on lines
+54
to
+56
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 list if node.nil? | ||
|
||
inorder_recursive(node.left, list) | ||
list << {key: node.key, value: node.value} | ||
inorder_recursive(node.right, list) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder | ||
raise NotImplementedError | ||
return inorder_recursive(@root, []) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder_recursive(node, list) | ||
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. 👍 |
||
return list if node.nil? | ||
|
||
list << {key: node.key, value: node.value} | ||
preorder_recursive(node.left, list) | ||
preorder_recursive(node.right, list) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder | ||
raise NotImplementedError | ||
return preorder_recursive(@root, []) | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder_recursive(node, list) | ||
Comment on lines
+82
to
+84
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 list if node.nil? | ||
|
||
postorder_recursive(node.left, list) | ||
postorder_recursive(node.right, list) | ||
list << {key: node.key, value: node.value} | ||
end | ||
def postorder | ||
raise NotImplementedError | ||
return postorder_recursive(@root, []) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n | ||
def height_recursive(node) | ||
Comment on lines
+95
to
+97
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 right if the tree is unbalanced, if it's balanced, the space complexity is O(log n) |
||
return 0 if node.nil? | ||
|
||
left = height_recursive(node.left) | ||
right = height_recursive(node.right) | ||
|
||
if left > right | ||
height = left | ||
else | ||
height = right | ||
end | ||
height += 1 | ||
return height | ||
end | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height | ||
raise NotImplementedError | ||
return height_recursive(@root) | ||
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.
Interesting gem, what does it do?