-
Notifications
You must be signed in to change notification settings - Fork 64
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
Rock - lin #52
base: master
Are you sure you want to change the base?
Rock - lin #52
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 |
---|---|---|
@@ -1,59 +1,117 @@ | ||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
val = key | ||
def __init__(self, key, value=None): | ||
if value == None: | ||
value = key | ||
|
||
self.key = key | ||
self.value = val | ||
self.value = value | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# Time Complexity: O(log(n))*assumes all balanced tree | ||
# Space Complexity: O(1) | ||
def add(self, key, value=None): | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def find(self, key): | ||
pass | ||
def add_helper(self, current_node, key, value): | ||
if current_node == None: | ||
return TreeNode(key, value) | ||
if key < current_node.key: | ||
current_node.left = self.add_helper(current_node.left, key, value) | ||
else: | ||
current_node.right = self.add_helper(current_node.right, key, value) | ||
return current_node | ||
|
||
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(1) | ||
def find(self, value): | ||
Comment on lines
+33
to
+35
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. 👍 Similar issues with space complexity. |
||
return self.find_helper(self.root, value) | ||
|
||
def find_helper(self, current, key): | ||
if current == None: | ||
return None | ||
elif current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
return self.find_helper(current.left, key) | ||
return self.find_helper(current.right, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
Comment on lines
+47
to
49
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 hit each node and make a list the of all the nodes in the tree the time and space complexity is O(n) |
||
pass | ||
array = [] | ||
if self.root == None: | ||
return array | ||
return self.inorder_helper(self.root, array) | ||
|
||
def inorder_helper(self, current, array): | ||
if current == None: | ||
return | ||
self.inorder_helper(current.left, array) | ||
array.append({"key": current.key, "value": current.value}) | ||
self.inorder_helper(current.right, array) | ||
return array | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(n) | ||
def preorder(self): | ||
Comment on lines
+63
to
65
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. 👍 Similar issues to time/space complexity |
||
pass | ||
array = [] | ||
if self.root == None: | ||
return array | ||
return self.preorder_helper(self.root, array) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(self, current, array): | ||
if current == None: | ||
return | ||
array.append({"key": current.key, "value": current.value}) | ||
self.preorder_helper(current.left, array) | ||
self.preorder_helper(current.right, array) | ||
return array | ||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(n) | ||
def postorder(self): | ||
Comment on lines
+79
to
81
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. 👍 Similar issues to time/space complexity |
||
pass | ||
array = [] | ||
if self.root == None: | ||
return array | ||
return self.postorder_helper(self.root, array) | ||
|
||
def postorder_helper(self, current, array): | ||
if current == None: | ||
return | ||
self.postorder_helper(current.left, array) | ||
self.postorder_helper(current.right, array) | ||
array.append({"key": current.key, "value": current.value}) | ||
return array | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
def height(self): | ||
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. 👍 However the space complexity is O(log n) due to the recursive call stack. |
||
pass | ||
if self.root == None: | ||
return 0 | ||
return self.height_helper(self.root) | ||
|
||
def height_helper(self, current): | ||
if current == None: | ||
return 0 | ||
left = self.height_helper(current.left) | ||
right = self.height_helper(current.right) | ||
return max(left, right) + 1 | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
|
||
|
||
|
||
|
||
# # Useful for printing | ||
# # Useful for printing | ||
def to_s(self): | ||
return f"{self.inorder()}" |
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.
👍 However due to the recursive call stack this is O(log n) for space complexity (if the tree is balanced and O(n) otherwise).