forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 68
completed binary tree task #40
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
Eatmine
wants to merge
1
commit into
Ada-C16:master
Choose a base branch
from
Eatmine: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
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 |
---|---|---|
|
@@ -14,35 +14,156 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(1) | ||
|
||
def add_helper(self, current_root, new_node): | ||
if current_root is None: | ||
return new_node | ||
|
||
if new_node.key < current_root.key: | ||
current_root.left = self.add_helper(current_root.left, new_node) | ||
|
||
else: | ||
current_root.right = self.add_helper(current_root.right, new_node) | ||
return current_root | ||
|
||
def add(self, key, value = None): | ||
pass | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
if self.root is None: | ||
self.root = TreeNode(key,value) | ||
return self.root | ||
|
||
new_node =TreeNode(key,value) | ||
self.add_helper(self.root, new_node) | ||
|
||
|
||
# Time Complexity: O(logn) | ||
# Space Complexity: O(1) | ||
|
||
def find(self, key): | ||
pass | ||
if self.root is None: | ||
return None | ||
current = self.root | ||
|
||
while current: | ||
if key < current.key: | ||
# go left | ||
|
||
current = current.left # traversing left | ||
elif key > current.key: | ||
|
||
current = current.right # traversing right. key is the integer and value string | ||
|
||
else: | ||
return current.value | ||
return None | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def inorder_helper(self, current_node,inorder_list): | ||
|
||
if current_node is not None: | ||
|
||
# traversing left | ||
self.inorder_helper(current_node.left, inorder_list) | ||
# add current node to preorder_list | ||
inorder_list.append({"key": current_node.key, "value": current_node.value}) | ||
#traversing right | ||
self.inorder_helper(current_node.right, inorder_list) | ||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def inorder(self): | ||
pass | ||
inorder_list = [] | ||
if self.root is None: | ||
return inorder_list | ||
|
||
|
||
self.inorder_helper(self.root, inorder_list) | ||
|
||
return inorder_list | ||
# inorder = self.root | ||
# inorder(self.root.left) | ||
|
||
# inorder(self.root.right) | ||
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. Looks like there's some commented code here. It's not a big deal in this case, but usually as a style thing, I recommend getting rid of commented code just to keep the code cleaner in general. |
||
|
||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
def preorder_helper(self,current_node, preorder_list): | ||
|
||
if current_node is not None: | ||
|
||
#append current node to preorder_list | ||
preorder_list.append({"key": current_node.key, "value": current_node.value}) | ||
|
||
#traverse left | ||
self.preorder_helper(current_node.left, preorder_list) | ||
|
||
|
||
#traverse right | ||
self.preorder_helper(current_node.right, preorder_list) | ||
|
||
|
||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def preorder(self): | ||
pass | ||
preorder_list = [] | ||
if self.root is None: | ||
return preorder_list | ||
|
||
self.preorder_helper(self.root, preorder_list) | ||
|
||
return preorder_list | ||
|
||
|
||
#Time Complexity: O(n) | ||
#Space Complexity: O(n) | ||
|
||
def postorder_helper(self, current_node, postorder_list): | ||
|
||
if current_node is not None: | ||
|
||
# traversing left | ||
self.postorder_helper(current_node.left, postorder_list) | ||
|
||
#traversing right | ||
self.postorder_helper(current_node.right, postorder_list) | ||
|
||
postorder_list.append({"key": current_node.key, "value": current_node.value}) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def postorder(self): | ||
pass | ||
postorder_list = [] | ||
if self.root is None: | ||
return postorder_list | ||
|
||
#call helper method | ||
self.postorder_helper(self.root, postorder_list) | ||
|
||
return postorder_list | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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) | ||
|
||
else: | ||
return 0 | ||
|
||
return (max(height_left,height_right) +1) | ||
|
||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
def height(self): | ||
pass | ||
return self.height_helper(self.root) | ||
|
||
|
||
|
||
|
||
# # Optional Method | ||
|
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.
This implementation of add_helper works, but definitely does a bit of extra work in setting nodes. Essentially, every node on the path to where the new node ends up will have either its left or right node "updated", though most of these will be updated to the exact same node. The only one that gets a meaningful update is whenever the bottom of the tree is reached and gets its left/right node updated to the new node. These updates happen in lines 25 and 28.
This probably will not be too bad of a performance hit, but there is a way that you can do this and just update only the node you need.
But this overall fine enough!