-
Notifications
You must be signed in to change notification settings - Fork 68
Spruce C16- Emily Colon #38
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 xml.dom import Node | ||
|
||
|
||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
if val == None: | ||
|
@@ -7,42 +10,116 @@ def __init__(self, key, val = None): | |
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
def node_height(self): | ||
if self == None: | ||
return 0 | ||
else: | ||
if self.left: | ||
h_left = self.left.node_height() | ||
else: | ||
h_left = 0 | ||
if self.right: | ||
h_right = self.right.node_height() | ||
else: | ||
h_right = 0 | ||
|
||
if (h_left > h_right): | ||
return h_left + 1 | ||
else: | ||
return h_right + 1 | ||
|
||
|
||
|
||
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): | ||
pass | ||
node = TreeNode(key, value) | ||
if self.root is None: | ||
self.root = node | ||
return | ||
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: | ||
# Space Complexity: | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: 0(1) | ||
def find(self, key): | ||
pass | ||
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: O(n) | ||
# Space Complexity: O(n) | ||
def inorder_helper(self, current, inorder_list): | ||
if current: | ||
self.inorder_helper(current.left, inorder_list) | ||
inorder_list.append({"key": current.key , "value":current.value}) | ||
self.inorder_helper(current.right, inorder_list) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
pass | ||
inorder_list = [] | ||
self.inorder_helper(self.root, inorder_list) | ||
return inorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder_helper(self, current, preorder_list): | ||
if current: | ||
preorder_list.append({"key": current.key , "value":current.value}) | ||
self.preorder_helper(current.left, preorder_list) | ||
self.preorder_helper(current.right, preorder_list) | ||
|
||
def preorder(self): | ||
pass | ||
preorder_list = [] | ||
self.preorder_helper(self.root, preorder_list) | ||
return preorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder_helper(self, current, postorder_list): | ||
if current: | ||
self.postorder_helper(current.left, postorder_list) | ||
self.postorder_helper(current.right, postorder_list) | ||
postorder_list.append({"key": current.key , "value":current.value}) | ||
def postorder(self): | ||
pass | ||
postorder_list = [] | ||
self.postorder_helper(self.root, postorder_list) | ||
return postorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(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. These are both correct assuming the tree is unbalanced, but if balanced it'd move down to O(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. Sorry, I have decided this is a nonsensical assumption to move down to O(log(n)) for time complexity as we still have to travel to every node even in a balanced tree. Likewise, as there is no additional space being used, I think the space complexity should be O(1). Forgive me for the confusion, but I was going off our instructor solution and I realized there is a mistake in their calculations too. |
||
def height(self): | ||
pass | ||
if self.root == None: | ||
return 0 | ||
return self.root.node_height() | ||
|
||
|
||
|
||
|
||
|
||
# # Optional Method | ||
|
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.
It looks like this
import
statement never got used, so you can delete it.