-
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
Finished all but the bfs part #45
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,5 +1,5 @@ | ||
class TreeNode: | ||
def __init__(self, key, val = None): | ||
def __init__(self, key, val=None): | ||
if val == None: | ||
val = key | ||
|
||
|
@@ -14,35 +14,108 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def add(self, key, value = None): | ||
pass | ||
def add_helper(self, current_node, key, value): | ||
if current_node == None: | ||
return TreeNode(key, value) | ||
if current_node.key > 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 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) | ||
def add(self, key, value=None): | ||
if self.root == None: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
Comment on lines
+34
to
36
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 | ||
current = self.root | ||
while current: | ||
if current.key == key: | ||
return current.value | ||
elif key < current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
return None | ||
|
||
def inorder_helper(self, root, elements): | ||
if root == None: | ||
return | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
self.inorder_helper(root.left, elements) | ||
elements.append({"key": root.key, "value": root.value}) | ||
self.inorder_helper(root.right, elements) | ||
return | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
Comment on lines
+56
to
58
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 == None: | ||
return [] | ||
|
||
elements = [] | ||
self.inorder_helper(self.root, elements) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return elements | ||
|
||
def preorder_helper(self, root, elements): | ||
if root == None: | ||
return | ||
|
||
elements.append({"key": root.key, "value": root.value}) | ||
self.preorder_helper(root.left, elements) | ||
self.preorder_helper(root.right, elements) | ||
return elements | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def preorder(self): | ||
Comment on lines
+76
to
78
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 == None: | ||
return [] | ||
|
||
elements = [] | ||
self.preorder_helper(self.root, elements) | ||
return elements | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder_helper(self, root, elements): | ||
if root == None: | ||
return | ||
|
||
self.postorder_helper(root.left, elements) | ||
self.postorder_helper(root.right, elements) | ||
elements.append({"key": root.key, "value": root.value}) | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def postorder(self): | ||
Comment on lines
+94
to
96
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 == None: | ||
return [] | ||
elements = [] | ||
self.postorder_helper(self.root, elements) | ||
return elements | ||
|
||
def height_helper(self, node): | ||
if not node: | ||
return 0 | ||
|
||
left = self.height_helper(node.left) | ||
right = self.height_helper(node.right) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
return max(left, right) + 1 | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(log n) | ||
def height(self): | ||
Comment on lines
+112
to
114
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 == None: | ||
return 0 | ||
|
||
return self.height_helper(self.root) | ||
|
||
|
||
# # Optional Method | ||
|
@@ -51,8 +124,6 @@ def height(self): | |
def bfs(self): | ||
pass | ||
|
||
|
||
|
||
|
||
# # 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.
👍