-
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 - Priscille #51
base: master
Are you sure you want to change the base?
Rock - Priscille #51
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 |
---|---|---|
|
@@ -7,53 +7,116 @@ def __init__(self, key, val = None): | |
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
def dict(self): | ||
return {"key": self.key, "value": self.value} | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def add(self, key, value = None): | ||
Comment on lines
+20
to
22
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 | ||
tree = TreeNode(key, value) | ||
|
||
if self.root == None: | ||
self.root = tree | ||
else: | ||
current = self.root | ||
|
||
while current != None: | ||
if key == current.key: | ||
return None | ||
if key < current.key: | ||
if current.left == None: | ||
current.left = tree | ||
current = current.left | ||
elif key > current.key: | ||
if current.right == None: | ||
current.right = tree | ||
current = current.right | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+42
to
44
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: | ||
return None | ||
current = self.root | ||
while current != None: | ||
if key == current.key: | ||
return current.value | ||
elif key > current.key: | ||
current = current.right | ||
else: | ||
current = current.left | ||
return None | ||
|
||
def inorder_helper(self, current, traversal_list): | ||
if current == None: | ||
return | ||
|
||
self.inorder_helper(current.left, traversal_list) | ||
traversal_list.append(current.dict()) | ||
self.inorder_helper(current.right, traversal_list) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def inorder(self): | ||
Comment on lines
+65
to
67
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 since you are building a list of all tree node values the space complexity is O(n) |
||
pass | ||
traversal_list = [] | ||
self.inorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
def preorder_helper(self, current, traversal_list): | ||
if current == None: | ||
return | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
traversal_list.append(current.dict()) | ||
self.preorder_helper(current.left, traversal_list) | ||
self.preorder_helper(current.right, traversal_list) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def preorder(self): | ||
Comment on lines
+80
to
82
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 since you are building a list of all tree node values the space complexity is O(n) |
||
pass | ||
traversal_list = [] | ||
self.preorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(self, current, traversal_list): | ||
if current == None: | ||
return | ||
|
||
self.postorder_helper(current.left, traversal_list) | ||
self.postorder_helper(current.right, traversal_list) | ||
traversal_list.append(current.dict()) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def postorder(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 since you are building a list of all tree node values the space complexity is O(n) |
||
pass | ||
traversal_list = [] | ||
self.postorder_helper(self.root, traversal_list) | ||
return traversal_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height_helper(self, current): | ||
if current == None: | ||
return 0 | ||
left_height = self.height_helper(current.left) | ||
right_height = self.height_helper(current.right) | ||
return 1 +max(left_height, right_height) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def height(self): | ||
Comment on lines
+109
to
111
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(h) where h is the height of the tree. |
||
pass | ||
return self.height_helper(self.root) | ||
|
||
|
||
# # 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.
👍 Useful!