-
Notifications
You must be signed in to change notification settings - Fork 68
Ainur-Pine C16 #46
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?
Ainur-Pine C16 #46
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -14,44 +14,119 @@ class Tree: | |||||||
def __init__(self): | ||||||||
self.root = None | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
# Time Complexity: O(log N) or O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
|
||||||||
def insert_node_helper(self, current_node, key, value): | ||||||||
if current_node == None: | ||||||||
return TreeNode(key, value) | ||||||||
|
||||||||
if key < current_node.key: | ||||||||
current_node.left = self.insert_node_helper(current_node.left, key, value) | ||||||||
else: | ||||||||
current_node.right = self.insert_node_helper(current_node.right, key, value) | ||||||||
return current_node | ||||||||
|
||||||||
def add(self, key, value = None): | ||||||||
pass | ||||||||
# BST is empty | ||||||||
if self.root is None: | ||||||||
self.root = TreeNode(key, value) | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
# BST is not empty | ||||||||
self.insert_node_helper(self.root, key, value) | ||||||||
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. Without the
Suggested change
|
||||||||
|
||||||||
|
||||||||
# Time Complexity: O(log N) or O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
def find(self, key): | ||||||||
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. ✨ However space complexity would be O(1) here since you did this iteratively and aren't creating any additional data structures whose size is proportional to the size of the tree. |
||||||||
pass | ||||||||
if self.root == None: | ||||||||
return None | ||||||||
current = self.root | ||||||||
while current != None: | ||||||||
if current.key == key: | ||||||||
return current.value | ||||||||
elif current.key < key: | ||||||||
current = current.right | ||||||||
else: | ||||||||
current = current.left | ||||||||
return None | ||||||||
|
||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
def inorder_helper(self, current, result): | ||||||||
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 current is None: | ||||||||
return result | ||||||||
else: | ||||||||
self.inorder_helper(current.left, result) | ||||||||
result.append({'key': current.key, 'value': current.value}) | ||||||||
self.inorder_helper(current.right, result) | ||||||||
return result | ||||||||
|
||||||||
def inorder(self): | ||||||||
pass | ||||||||
result = [] | ||||||||
# result_value = self.inorder_helper(self.root, result) | ||||||||
return self.inorder_helper(self.root, result) | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
def preorder_helper(self, current, result): | ||||||||
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 current: | ||||||||
result.append({"key": current.key, "value": current.value}) | ||||||||
self.preorder_helper(current.left, result) | ||||||||
self.preorder_helper(current.right, result) | ||||||||
return result | ||||||||
def preorder(self): | ||||||||
pass | ||||||||
result = [] | ||||||||
# self.preorder_helper(self.root, result) | ||||||||
return self.preorder_helper(self.root, result) | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
|
||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(n) | ||||||||
def postorder_helper(self, current, result): | ||||||||
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 current: | ||||||||
self.postorder_helper(current.left, result) | ||||||||
self.postorder_helper(current.right, result) | ||||||||
result.append({"key": current.key, "value": current.value}) | ||||||||
def postorder(self): | ||||||||
pass | ||||||||
result = [] | ||||||||
self.postorder_helper(self.root, result) | ||||||||
return result | ||||||||
|
||||||||
# Time Complexity: | ||||||||
# Space Complexity: | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def height_helper(self, current): | ||||||||
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. Because of the recursive call stack, space complexity would also be O(n) here |
||||||||
if current == None: | ||||||||
return 0 | ||||||||
if current: | ||||||||
height_left = self.height_helper(current.left) | ||||||||
height_right = self.height_helper(current.right) | ||||||||
return max(height_left, height_right)+1 | ||||||||
def height(self): | ||||||||
pass | ||||||||
|
||||||||
return self.height_helper(self.root) | ||||||||
|
||||||||
|
||||||||
# # Optional Method | ||||||||
# # Time Complexity: | ||||||||
# # Space Complexity: | ||||||||
# # Time Complexity: O(n) | ||||||||
# # Space Complexity: O(n) | ||||||||
|
||||||||
def bfs(self): | ||||||||
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. ✨ |
||||||||
pass | ||||||||
result = [] | ||||||||
|
||||||||
|
||||||||
if self.root == None: | ||||||||
return result | ||||||||
|
||||||||
queue = [self.root] | ||||||||
while queue: | ||||||||
current = queue.pop(0) | ||||||||
result.append({"key": current.key, "value": current.value}) | ||||||||
if current.left: | ||||||||
queue.append(current.left) | ||||||||
if current.right: | ||||||||
queue.append(current.right) | ||||||||
return result | ||||||||
|
||||||||
|
||||||||
# # Useful for printing | ||||||||
|
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.
Space complexity would be same as time complexity based on the recursive call stack: O(log n) for balanced trees and O(n) for unbalanced trees.