forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 68
Laurel S. - Cedar #41
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
Open
paperbackwriter2
wants to merge
4
commits into
Ada-C16:master
Choose a base branch
from
paperbackwriter2:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e906da
Adds find() and add() methods for bst
paperbackwriter2 8420081
Adds BFS method
paperbackwriter2 58f7a32
Adds methods for preorder, inorder, postorder traversals
paperbackwriter2 6d052e7
Adds space and time complexity analysis
paperbackwriter2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,45 +11,142 @@ def __init__(self, key, val = None): | |
|
||
|
||
class Tree: | ||
# # ITERATIVE | ||
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 not self.root: | ||
self.root = node | ||
return self.root | ||
|
||
current = self.root | ||
previous = None | ||
while current: | ||
previous = current | ||
if key <= current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
if key <= previous.key: | ||
previous.left = node | ||
else: | ||
previous.right = node | ||
|
||
# RECURSIVE | ||
# Time Complexity: | ||
# Space Complexity: | ||
# def add(self, key, value = None): | ||
# # base case | ||
# if not current.left and not current.right: | ||
|
||
|
||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
def find(self, key): | ||
pass | ||
if not self.root: | ||
return None | ||
if self.root.key == key: | ||
return self.root.value | ||
current = self.root | ||
while current: | ||
if current.key == key: | ||
return current.value | ||
if key <= current.key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder_helper(self, root, nodes): | ||
if root: | ||
self.inorder_helper(root.left, nodes) | ||
nodes.append({'key': root.key, 'value': root.value}) | ||
self.inorder_helper(root.right, nodes) | ||
|
||
# Time Complexity: O(n) while n = number of nodes in BST | ||
# Space Complexity: O(n) | ||
def inorder(self): | ||
pass | ||
# left, root, right | ||
if not self.root: | ||
return [] | ||
nodes = [] | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
self.inorder_helper(self.root, nodes) | ||
return nodes | ||
|
||
|
||
def preorder_helper(self, current, nodes): | ||
if current: | ||
nodes.append({'key': current.key, 'value': current.value}) | ||
self.preorder_helper(current.left, nodes) | ||
self.preorder_helper(current.right, nodes) | ||
return | ||
|
||
# Time Complexity: O(n) n = number of nodes in BST | ||
# Space Complexity: O(n) | ||
def preorder(self): | ||
pass | ||
# root -> left -> right | ||
if not self.root: | ||
return [] | ||
nodes_list = [] | ||
self.preorder_helper(self.root, nodes_list) | ||
return nodes_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def postorder_helper(self, current, nodes_list): | ||
if current: | ||
self.postorder_helper(current.left, nodes_list) | ||
self.postorder_helper(current.right, nodes_list) | ||
nodes_list.append({'key': current.key, 'value': current.value}) | ||
|
||
# Time Complexity: O(n) n = number of nodes in BST | ||
# Space Complexity: O(n) | ||
def postorder(self): | ||
pass | ||
# left -> right -> root | ||
if not self.root: | ||
return [] | ||
nodes_list = [] | ||
self.postorder_helper(self.root, nodes_list) | ||
return nodes_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
|
||
def height_helper(self, root): | ||
if not root: | ||
return 0 | ||
left_height = self.height_helper(root.left) | ||
right_height = self.height_helper(root.right) | ||
return 1 + max(left_height, right_height) | ||
|
||
|
||
# Time Complexity: O(n) and n = # of nodes in BST | ||
# Space Complexity: O(n) ? not sure on this one | ||
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. Good news, neither are the instructors, because as I was doing this grading I realized our instructor solution was wrong on these calculations! In this case as there is no additional space allocated, I would consider this O(1). |
||
def height(self): | ||
pass | ||
if not self.root: | ||
return 0 | ||
return self.height_helper(self.root) | ||
|
||
|
||
# # Optional Method | ||
# # Time Complexity: | ||
# # Space Complexity: | ||
# Optional Method | ||
# Time Complexity: O(n) where n = number of nodes in BST | ||
# Space Complexity: O(n) | ||
def bfs(self): | ||
pass | ||
if not self.root: | ||
return [] | ||
queue = [ self.root ] | ||
values = [] | ||
while queue: | ||
current = queue.pop(0) | ||
# {'key': 5, 'value': 'Peter'} | ||
values.append({'key': current.key, 'value': current.value}) | ||
if current.left: | ||
queue.append(current.left) | ||
if current.right: | ||
queue.append(current.right) | ||
return values | ||
|
||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not a big deal, but generally I recommend removing unfinished but uncommented code like this to just keep the source clean as a style thing.