Skip to content
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

Paper - Malakhova K. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 72 additions & 23 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,93 @@ def __init__(self, key, val = None):
self.left = None
self.right = None



class Tree:
def __init__(self):
self.root = None

# Time Complexity:
# Space Complexity:
def add_helper(self, current_node, key, value):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Space/time complexity?

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: O(logN)
# Space Complexity: O(logN)
def add(self, key, value = None):

Choose a reason for hiding this comment

The 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)

def find_helper(self, current_node, key):
if current_node == None:
return None
elif current_node.key == key:
return current_node.value
elif current_node.key <= key:
return self.find_helper(current_node.right, key)
return self.find_helper(current_node.left, key)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(logN)
# Space Complexity: O(logN)
def find(self, key):
Comment on lines +41 to 43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice recursive solution

pass
if self.root == None:
return None
else:
return self.find_helper(self.root, key)

def inorder_helper(self, current, traversal_list):
if current != None:
self.inorder_helper(current.left, traversal_list)
traversal_list.append({"key":current.key,"value":current.value})
self.inorder_helper(current.right, traversal_list)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(N)
# Space Complexity: O(N)
def inorder(self):
Comment on lines +55 to 57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
traversal_list = []
self.inorder_helper(self.root, traversal_list)
return traversal_list

def preorder_helper(self, current, traversal_list):
if current != None:
traversal_list.append({"key":current.key,"value":current.value})
self.preorder_helper(current.left, traversal_list)
self.preorder_helper(current.right, traversal_list)

# Time Complexity:
# Space Complexity:
# Time Complexity: O(N)
# Space Complexity: O(N)
def preorder(self):
Comment on lines +68 to 70

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
traversal_list = []
self.preorder_helper(self.root, traversal_list)
return traversal_list

def postorder_helper(self, current, traversal_list):
if current != None:
self.postorder_helper(current.left, traversal_list)
self.postorder_helper(current.right, traversal_list)
traversal_list.append({"key":current.key,"value":current.value})

# Time Complexity:
# Space Complexity:
# Time Complexity: O(N)
# Space Complexity: O(N)
def postorder(self):
Comment on lines +81 to 83

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
traversal_list = []
self.postorder_helper(self.root, traversal_list)
return traversal_list

# Time Complexity:
# Space Complexity:
def height_helper(self, current):
if current is None:
return 0
return 1+max(self.height_helper(current.left), self.height_helper(current.right))

# Time Complexity: O(logN)
# Space Complexity: O(logN)
def height(self):
Comment on lines +93 to 95

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 However the time complexity is O(n) because you visit each node.

pass
return self.height_helper(self.root)



# # Optional Method
Expand All @@ -51,9 +103,6 @@ def height(self):
def bfs(self):
pass




# # Useful for printing
def to_s(self):
return f"{self.inorder()}"