-
Notifications
You must be signed in to change notification settings - Fork 68
C16 - Gabe K #42
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?
C16 - Gabe K #42
Changes from all commits
aeac65b
24b49f9
101fa78
baa7ac6
65dd359
89f5f96
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,7 @@ | ||
from multiprocessing.dummy import current_process | ||
from typing import ItemsView | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
|
@@ -9,50 +13,144 @@ def __init__(self, key, val = None): | |
self.right = None | ||
|
||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(log n) | ||
# Space Complexity: 0(log n) | ||
def add_helper(self, current_node, key, value): | ||
# helper takes advantage of recursive | ||
# if node is empty, return new node with key and 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 | ||
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. This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 28 and 30. This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need. But this overall fine enough! |
||
|
||
def add(self, key, value = None): | ||
pass | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(log n) | ||
# Space Complexity: 0(log n) | ||
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. Luckily since you did an iterative approach for this, we do not take the hit of the space that will be added to the stack if you had done this recursively. As such, the space complexity is O(1). 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. Ignore my comment on how there is space complexity savings for a recursive solution. Both should be O(1) in my opinion because they are not using additional space. I was going off the instructor solution and then realized that I think they made a mistake. |
||
def find(self, key): | ||
pass | ||
if self.root == None: | ||
return None | ||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif current.key <key: | ||
current = current.right | ||
else: | ||
current = current.left | ||
return None | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(n) | ||
# Space Complexity: 0(n) | ||
def inorder_helper(self, current_node, items): | ||
if current_node != None: | ||
self.inorder_helper(current_node.left, items) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.inorder_helper(current_node.right, items) | ||
|
||
def inorder(self): | ||
pass | ||
#list of items to be returned --> depth-first traversal sorted key values in an ascending order, Left,Current,Right | ||
items = [] | ||
self.inorder_helper(self.root, items) | ||
return items | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(n) | ||
# Space Complexity: 0(n) | ||
def preorder_helper(self, current_node, items): | ||
if current_node == None: | ||
return items | ||
else: | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.preorder_helper(current_node.left, items) | ||
self.preorder_helper(current_node.right, items) | ||
return items | ||
|
||
def preorder(self): | ||
pass | ||
#list of items to be returned --> depth-first traversal sorted current, left, right | ||
items = [] | ||
if self.root: | ||
self.preorder_helper(self.root, items) | ||
return items | ||
|
||
|
||
# Time Complexity: 0(n) | ||
# Space Complexity: 0(n) | ||
def postorder_helper(self, current_node, items): | ||
if current_node: | ||
self.postorder_helper(current_node.left, items) | ||
self.postorder_helper(current_node.right, items) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
return items | ||
|
||
def postorder(self): | ||
#list of items to be returned --> depth-first traversal sorted left, right, current | ||
items = [] | ||
if self.root: | ||
self.postorder_helper(self.root, items) | ||
return items | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(n) | ||
# Space Complexity: 0(n) | ||
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. This is true assuming an unbalanced tree, but if we assume a balanced tree, then we only need to recurse down O(log(n)) times along each branch of the tree, so we can use that for the time and space complexity calculations. 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. When I gave this comment, I was working off the instructor solution, but I now think this is wrong. The time complexity is O(n) regardless because we have to travel to every node, and O(1) regardless because no additional space is allocated. Sorry for any confusion. |
||
def height_helper(self, current_node): | ||
# uses stack and recursion for each node in tree | ||
# plus one for the root node / the current node | ||
if current_node != None: | ||
height_left = self.height_helper(current_node.left) | ||
height_right = self.height_helper(current_node.right) | ||
return (max(height_left, height_right) + 1) | ||
|
||
else: | ||
return 0 | ||
|
||
def height(self): | ||
pass | ||
return self.height_helper(self.root) | ||
|
||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
def bfs(self): | ||
pass | ||
# # Time Complexity: O(n) | ||
# # Space Complexity: O(n) | ||
|
||
def bfs_helper(self, current_node, sub_list): | ||
if current_node.left: | ||
sub_list.append(current_node.left) | ||
if current_node.right: | ||
sub_list.append(current_node.right) | ||
|
||
|
||
|
||
def bfs(self): | ||
# uses queues to create sublists for each level, pops off elements from queue from left to right until sublist is empty to creates list, then adds children of each element to queue as popping off, repeats, | ||
# recursion for each level | ||
items = [] | ||
sub_list = [] | ||
|
||
if not self.root: | ||
return items | ||
|
||
items.append({"key": self.root.key, "value": self.root.value}) | ||
self.bfs_helper(self.root, sub_list) | ||
|
||
while sub_list: | ||
current_node = sub_list.pop(0) | ||
items.append({"key": current_node.key, "value": current_node.value}) | ||
self.bfs_helper(current_node, sub_list) | ||
return items | ||
|
||
|
||
# # 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.
Look like these
import
statements never got used. I'm guessing they got added automatically by autocomplete, but you can delete them regardless.