-
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
Brittany- Binary Search Tree #59
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,59 +1,145 @@ | ||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
def __init__(self, key, val=None): | ||
if val == None: | ||
val = key | ||
|
||
self.key = key | ||
self.value = val | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
def add(self, key, value=None): | ||
if key < self.key: | ||
if self.left is None: | ||
self.left = TreeNode(key, value) | ||
return self.left | ||
return self.left.add(key, value) | ||
else: | ||
if self.right is None: | ||
self.right = TreeNode(key, value) | ||
return self.right | ||
return self.right.add(key, value) | ||
|
||
def inorder(self, 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. 👍 |
||
if self.left is not None: | ||
self.left.inorder(list) | ||
|
||
list.append({ | ||
"key": self.key, | ||
"value": self.value | ||
}) | ||
|
||
if self.right is not None: | ||
self.right.inorder(list) | ||
|
||
def preorder(self, 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. 👍 |
||
list.append({ | ||
"key": self.key, | ||
"value": self.value | ||
}) | ||
|
||
if self.left is not None: | ||
self.left.preorder(list) | ||
|
||
if self.right is not None: | ||
self.right.preorder(list) | ||
|
||
def postorder(self, 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. 👍 |
||
if self.left is not None: | ||
self.left.postorder(list) | ||
|
||
if self.right is not None: | ||
self.right.postorder(list) | ||
|
||
list.append({ | ||
"key": self.key, | ||
"value": self.value | ||
}) | ||
|
||
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. 👍 |
||
if self.left is None: | ||
leftHeight = 0 | ||
else: | ||
leftHeight = self.left.height() | ||
|
||
if self.right is None: | ||
rightHeight = 0 | ||
else: | ||
rightHeight = self.right.height() | ||
|
||
return 1 + max(leftHeight, rightHeight) | ||
|
||
|
||
class Tree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value=None): | ||
Comment on lines
+77
to
+79
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/space Complexity? |
||
if self.root is None: | ||
self.root = TreeNode(key, value) | ||
return self.root | ||
else: | ||
return self.root.add(key, value) | ||
|
||
def find_helper(self, current, key): | ||
if current == None: | ||
return None | ||
elif current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
return self.find_helper(current.left, key) | ||
|
||
return self.find_helper(current.right, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: o(1) | ||
# Space Complexity:o(n) | ||
def find(self, key): | ||
Comment on lines
+96
to
98
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 time/space complexity are both O(n) if the tree is unbalanced and O(log n) if it's balanced. |
||
pass | ||
return self.find_helper(self.root, key) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: o(1) | ||
# Space Complexity: o(1) | ||
def inorder(self): | ||
Comment on lines
+101
to
103
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/space Complexity are both O(n) |
||
pass | ||
list = [] | ||
if self.root is not None: | ||
self.root.inorder(list) | ||
return list | ||
|
||
# Time Complexity: o(n) | ||
# Space Complexity:0(n) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
Comment on lines
+109
to
112
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/space Complexity are both O(n) |
||
pass | ||
list = [] | ||
if self.root is not None: | ||
self.root.preorder(list) | ||
return list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: 0(log n) | ||
# Space Complexity:o(1) | ||
def postorder(self): | ||
Comment on lines
+118
to
120
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/space Complexity are both O(n) |
||
pass | ||
list = [] | ||
if self.root is not None: | ||
self.root.postorder(list) | ||
return list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: o(n) | ||
# Space Complexity:0(n) | ||
def height(self): | ||
Comment on lines
+126
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. 👍 |
||
pass | ||
if self.root is None: | ||
return 0 | ||
return self.root.height() | ||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# # 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.
👍 Nice recursive solution