-
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
Paper - Malakhova K. #54
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 |
---|---|---|
|
@@ -8,41 +8,93 @@ def __init__(self, key, val = None): | |
self.left = None | ||
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
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(logN) | ||
# Space Complexity: O(logN) | ||
def add(self, key, value = None): | ||
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 | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
def find_helper(self, current_node, key): | ||
if current_node == None: | ||
return None | ||
elif current_node.key == key: | ||
return current_node.value | ||
elif current_node.key <= key: | ||
return self.find_helper(current_node.right, key) | ||
return self.find_helper(current_node.left, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(logN) | ||
# Space Complexity: O(logN) | ||
def find(self, key): | ||
Comment on lines
+41
to
43
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. 👍 Nice recursive solution |
||
pass | ||
if self.root == None: | ||
return None | ||
else: | ||
return self.find_helper(self.root, key) | ||
|
||
def inorder_helper(self, current, traversal_list): | ||
if current != None: | ||
self.inorder_helper(current.left, traversal_list) | ||
traversal_list.append({"key":current.key,"value":current.value}) | ||
self.inorder_helper(current.right, traversal_list) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(N) | ||
# Space Complexity: O(N) | ||
def inorder(self): | ||
Comment on lines
+55
to
57
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 | ||
traversal_list = [] | ||
self.inorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
def preorder_helper(self, current, traversal_list): | ||
if current != None: | ||
traversal_list.append({"key":current.key,"value":current.value}) | ||
self.preorder_helper(current.left, traversal_list) | ||
self.preorder_helper(current.right, traversal_list) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(N) | ||
# Space Complexity: O(N) | ||
def preorder(self): | ||
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. 👍 |
||
pass | ||
traversal_list = [] | ||
self.preorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
def postorder_helper(self, current, traversal_list): | ||
if current != None: | ||
self.postorder_helper(current.left, traversal_list) | ||
self.postorder_helper(current.right, traversal_list) | ||
traversal_list.append({"key":current.key,"value":current.value}) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(N) | ||
# Space Complexity: O(N) | ||
def postorder(self): | ||
Comment on lines
+81
to
83
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 | ||
traversal_list = [] | ||
self.postorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height_helper(self, current): | ||
if current is None: | ||
return 0 | ||
return 1+max(self.height_helper(current.left), self.height_helper(current.right)) | ||
|
||
# Time Complexity: O(logN) | ||
# Space Complexity: O(logN) | ||
def height(self): | ||
Comment on lines
+93
to
95
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 time complexity is O(n) because you visit each node. |
||
pass | ||
return self.height_helper(self.root) | ||
|
||
|
||
|
||
# # Optional Method | ||
|
@@ -51,9 +103,6 @@ def height(self): | |
def bfs(self): | ||
pass | ||
|
||
|
||
|
||
|
||
# # 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.
👍 Space/time complexity?