-
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
Scissors - Araceli #56
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
|
||
|
||
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 | ||
|
||
|
@@ -14,45 +16,148 @@ 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): | ||
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
+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. 👍 Time complexity is O(n) if the tree is unbalanced and O(log n) if it is balanced. |
||
pass | ||
|
||
if self.root == None: | ||
return None | ||
|
||
current = self.root | ||
|
||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
|
||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def inorder(self): | ||
Comment on lines
+60
to
62
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(n) because you're building a list of all the nodes. |
||
pass | ||
tree_nodes = [] | ||
|
||
if self.root == None: | ||
return tree_nodes | ||
else: | ||
return self.inorder_helper(self.root, tree_nodes) | ||
|
||
def inorder_helper(self, current, tree_nodes): | ||
if current == None: | ||
return None | ||
|
||
self.inorder_helper(current.left, tree_nodes) | ||
|
||
tree_nodes.append( | ||
{ | ||
"key": current.key, | ||
"value": current.value | ||
} | ||
) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
self.inorder_helper(current.right, tree_nodes) | ||
|
||
return tree_nodes | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def preorder(self): | ||
Comment on lines
+87
to
89
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(n) because you're building a list of all the nodes. |
||
pass | ||
tree_nodes = [] | ||
|
||
if self.root == None: | ||
return tree_nodes | ||
else: | ||
return self.preorder_helper(self.root, tree_nodes) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder_helper(self, current, tree_nodes): | ||
if current == None: | ||
return None | ||
|
||
tree_nodes.append( | ||
{ | ||
"key": current.key, | ||
"value": current.value | ||
} | ||
) | ||
|
||
self.preorder_helper(current.left, tree_nodes) | ||
|
||
self.preorder_helper(current.right, tree_nodes) | ||
|
||
return tree_nodes | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def postorder(self): | ||
Comment on lines
+114
to
116
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(n) because you're building a list of all the nodes. |
||
pass | ||
tree_nodes = [] | ||
|
||
|
||
return self.postorder_helper(self.root, tree_nodes) | ||
|
||
def postorder_helper(self, current, tree_nodes): | ||
if current == None: | ||
return tree_nodes | ||
|
||
self.postorder_helper(current.left, tree_nodes) | ||
|
||
self.postorder_helper(current.right, tree_nodes) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
tree_nodes.append( | ||
{ | ||
"key": current.key, | ||
"value": current.value | ||
} | ||
) | ||
|
||
return tree_nodes | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def height(self): | ||
Comment on lines
+139
to
141
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 recursion the space complexity is O(h) where h is the height of the tree (which will be log n if the tree is balanced and it can approach n if the tree is very unbalanced). |
||
pass | ||
|
||
return self.height_helper(self.root) | ||
|
||
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 max(left_height, right_height) + 1 | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
|
||
|
||
|
||
|
||
# # Useful for printing | ||
def to_s(self): | ||
|
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.
👍