-
Notifications
You must be signed in to change notification settings - Fork 68
Lety Palomo - Pine C-16 #60
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,9 @@ | ||
from lib2to3.pytree import Node | ||
|
||
from requests import post | ||
from toml import TomlPreserveCommentDecoder | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
|
@@ -8,52 +14,160 @@ 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(self, key, value = None): | ||
pass | ||
node = TreeNode(key, value) | ||
if self.root == None: | ||
self.root = node | ||
return | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
current = self.root | ||
|
||
while current: | ||
if current.key > key: | ||
if current.left is None: | ||
current.left = node | ||
return | ||
else: | ||
current = current.left | ||
else: | ||
if current.right is None: | ||
current.right = node | ||
return | ||
else: | ||
current = current.right | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
|
||
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. ✨ Nice iterative solution |
||
pass | ||
|
||
if self.root == None: | ||
return None | ||
|
||
node = self.root | ||
|
||
while node != None: | ||
if (node.key == key): | ||
return node.value | ||
if node.key < key: | ||
node = node.right | ||
else: | ||
node = node.left | ||
|
||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
64
to
65
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 and space complexity? |
||
|
||
def inorder_helper_norecursion(self, current_node, trav_list): | ||
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. 🌟😎 Really cool you did it with both |
||
#Initialize a holding place | ||
stack = [] | ||
|
||
# Loop until all the nodes are visited | ||
while current_node or stack: | ||
# If current node ,then push it in the stack and assign current to left node | ||
if current_node: | ||
stack.append(current_node) | ||
current_node = current_node.left | ||
# If current is none, pop the value and append it to the node visited | ||
else: | ||
current_node = stack.pop() | ||
trav_list.append({'key':current_node.key, 'value':current_node.value}) | ||
# Now get the right node | ||
current_node = current_node.right | ||
return trav_list | ||
|
||
def inorder_helper(self, current_node, trav_list): | ||
if current_node == None: | ||
return | ||
if current_node.left: | ||
trav_list = self.inorder_helper(current_node.left, trav_list) | ||
# visit root node | ||
trav_list.append({'key':current_node.key, 'value':current_node.value}) | ||
# visit right Tree | ||
if current_node.right: | ||
trav_list = self.inorder_helper(current_node.right, trav_list) | ||
return trav_list | ||
|
||
def inorder(self): | ||
pass | ||
trav_list = [] | ||
#now call the helper to append to this array away from the fray of traversal | ||
self.inorder_helper(self.root, trav_list) | ||
return trav_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
103
to
104
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 and space complexity? |
||
|
||
def preorder_helper(self, current_node, trav_list): | ||
if current_node == None: | ||
return [] | ||
# visit root node | ||
trav_list.append({'key':current_node.key, 'value':current_node.value}) | ||
|
||
# visit right Tree | ||
if current_node.left: | ||
trav_list = self.preorder_helper(current_node.left, trav_list) | ||
|
||
if current_node.right: | ||
trav_list = self.preorder_helper(current_node.right, trav_list) | ||
|
||
return trav_list | ||
|
||
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 | ||
trav_list = [] | ||
self.preorder_helper(self.root, trav_list) | ||
return trav_list | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Space Complexity: | ||
Comment on lines
127
to
+128
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 and space complexity? |
||
def postorder_helper(self, current_node, trav_list): | ||
if current_node == None: | ||
return [] | ||
|
||
# visit right Tree | ||
if current_node.left: | ||
trav_list = self.postorder_helper(current_node.left, trav_list) | ||
|
||
if current_node.right: | ||
trav_list = self.postorder_helper(current_node.right, trav_list) | ||
|
||
# visit root node | ||
trav_list.append({'key':current_node.key, 'value':current_node.value}) | ||
|
||
|
||
return trav_list | ||
|
||
|
||
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 | ||
trav_list = [] | ||
self.postorder_helper(self.root, trav_list) | ||
return trav_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
152
to
153
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 and space complexity? |
||
def height(self): | ||
pass | ||
def height_helper(self, current_node): | ||
if current_node is not 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): | ||
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. ✨ |
||
return self.height_helper(self.root) | ||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
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.
⏱🪐 Time and space complexity?