-
Notifications
You must be signed in to change notification settings - Fork 68
C16 - Spruce - Vange #51
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 |
---|---|---|
|
@@ -14,35 +14,110 @@ class Tree: | |
def __init__(self): | ||
self.root = None | ||
|
||
def add_helper(self, current_node, key, value ): | ||
if current_node == None: | ||
return TreeNode(key, value) | ||
if key <= current_node.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: | ||
def add(self, key, value = None): | ||
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: | ||
self.root = TreeNode(key, value) | ||
else: | ||
self.add_helper(self.root, key, value) | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
34
to
35
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input? |
||
def find(self, key): | ||
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 None | ||
current = self.root | ||
while current != None: | ||
if current.key == key: | ||
return current.value | ||
elif current.key >= key: | ||
current = current.left | ||
else: | ||
current = current.right | ||
return None | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
# In-Order: Left, Current, Right | ||
def inorder_helper(self, root, inorder_list): | ||
if root: | ||
# Traverse left | ||
self.inorder_helper(root.left, inorder_list) | ||
# Traverse root | ||
# print(str(root.val) + "->", end='') | ||
inorder_list.append({"key" : root.key, "value" : root.value}) | ||
# Traverse right | ||
self.inorder_helper(root.right, inorder_list) | ||
return inorder_list | ||
|
||
# # Time Complexity: | ||
# # Space Complexity: | ||
Comment on lines
+61
to
+62
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input? |
||
def inorder(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. 👍 |
||
pass | ||
inorder_list = [] | ||
if self.root == None: | ||
return inorder_list | ||
else: | ||
return self.inorder_helper(self.root, inorder_list) | ||
|
||
def preorder_helper(self, root, preorder_list): | ||
if root: | ||
preorder_list.append({"key" : root.key, "value" : root.value}) | ||
self.preorder_helper(root.left, preorder_list) | ||
self.preorder_helper(root.right, preorder_list) | ||
return preorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
77
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input? |
||
def preorder(self): | ||
pass | ||
preorder_list = [] | ||
if self.root == None: | ||
return preorder_list | ||
else: | ||
return self.preorder_helper(self.root, preorder_list) | ||
|
||
def postorder_helper(self, root, postorder_list): | ||
if root: | ||
self.postorder_helper(root.left, postorder_list) | ||
self.postorder_helper(root.right, postorder_list) | ||
postorder_list.append({"key" : root.key, "value" : root.value}) | ||
return postorder_list | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
93
to
94
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input? |
||
def postorder(self): | ||
pass | ||
postorder_list = [] | ||
if self.root == None: | ||
return postorder_list | ||
else: | ||
return self.postorder_helper(self.root, postorder_list) | ||
|
||
|
||
def height_helper(self, root): | ||
if root == None: | ||
return 0 | ||
lefth= self.height_helper(root.left) | ||
righth = self.height_helper(root.right) | ||
|
||
if lefth > righth: | ||
return lefth + 1 | ||
else: | ||
return righth + 1 | ||
|
||
# Time Complexity: | ||
# Space Complexity: | ||
Comment on lines
114
to
115
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input? |
||
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. 👍 |
||
pass | ||
if self.root == None: | ||
return 0 | ||
else: | ||
return self.height_helper(self.root) | ||
|
||
|
||
# # Optional Method | ||
|
@@ -51,8 +126,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.
What do you think the space and time complexity of this is? Is the time complexity dependent on the size of the binary search tree? Is there any new variables being created that grow larger depending upon the input?