-
Notifications
You must be signed in to change notification settings - Fork 68
Nourhan - Spruce - C16 #57
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?
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,3 +1,6 @@ | ||
from queue import Queue | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
|
@@ -14,42 +17,117 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(1) | ||
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. ✨ Nice inner function |
||
pass | ||
def add_helper(node, key, value = None): | ||
if key < node.key: | ||
if node.left is None: | ||
node.left = TreeNode(key, value) | ||
return | ||
else: | ||
add_helper(node.left, key, value) | ||
return | ||
if key > node.key: | ||
if node.right is None: | ||
node.right = TreeNode(key, value) | ||
return | ||
else: | ||
add_helper(node.right, key, value) | ||
return | ||
|
||
if self.root is None: | ||
self.root = TreeNode(key, value) | ||
return | ||
add_helper(self.root, key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log(n)) | ||
# Space Complexity: O(log(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. ✨ |
||
pass | ||
def find_helper(node, key): | ||
if node is None: | ||
return None | ||
elif node.key == key: | ||
return node.value | ||
elif key < node.key: | ||
return find_helper(node.left, key) | ||
elif key > node.key: | ||
return find_helper(node.right, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return find_helper(self.root, key) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(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 | ||
def inorder_helper(inorder_list, node): | ||
if node is None: | ||
return | ||
inorder_helper(inorder_list, node.left) | ||
inorder_list.append({"key": node.key, "value": node.value}) | ||
inorder_helper(inorder_list, node.right) | ||
|
||
inorder_list = [] | ||
inorder_helper(inorder_list, self.root) | ||
return inorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(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 | ||
def preorder_helper(inorder_list, node): | ||
if node is None: | ||
return | ||
preorder_list.append({"key": node.key, "value": node.value}) | ||
preorder_helper(inorder_list, node.left) | ||
preorder_helper(inorder_list, node.right) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
preorder_list = [] | ||
preorder_helper(preorder_list, self.root) | ||
return preorder_list | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(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 | ||
def postorder_helper(inorder_list, node): | ||
if node is None: | ||
return | ||
postorder_helper(inorder_list, node.left) | ||
postorder_helper(inorder_list, node.right) | ||
postorder_list.append({"key": node.key, "value": node.value}) | ||
|
||
postorder_list = [] | ||
postorder_helper(postorder_list, self.root) | ||
return postorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def height(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 | ||
def height_helper(node): | ||
if node is None: | ||
return 0 | ||
return 1 + max(height_helper(node.left), height_helper(node.right)) | ||
return 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. ✨🌟 Excellent! |
||
pass | ||
if self.root is None: | ||
return [] | ||
|
||
|
||
bfs_result = [] | ||
bfs_queue = Queue() | ||
bfs_queue.put(self.root) | ||
while not bfs_queue.empty(): | ||
node = bfs_queue.get() | ||
bfs_result.append({"key": node.key, "value": node.value}) | ||
if node.left is not None: | ||
bfs_queue.put(node.left) | ||
if node.right is not None: | ||
bfs_queue.put(node.right) | ||
|
||
return bfs_result | ||
|
||
|
||
|
||
|
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 will actually be O(log(n)) here because of the recursive call stack