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

Brittany- Binary Search Tree #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
136 changes: 111 additions & 25 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,145 @@
class TreeNode:
def __init__(self, key, val = None):
def __init__(self, key, val=None):
if val == None:
val = key

self.key = key
self.value = val
self.left = None
self.right = None


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.

👍 Nice recursive solution

if key < self.key:
if self.left is None:
self.left = TreeNode(key, value)
return self.left
return self.left.add(key, value)
else:
if self.right is None:
self.right = TreeNode(key, value)
return self.right
return self.right.add(key, value)

def inorder(self, list):

Choose a reason for hiding this comment

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

👍

if self.left is not None:
self.left.inorder(list)

list.append({
"key": self.key,
"value": self.value
})

if self.right is not None:
self.right.inorder(list)

def preorder(self, list):

Choose a reason for hiding this comment

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

👍

list.append({
"key": self.key,
"value": self.value
})

if self.left is not None:
self.left.preorder(list)

if self.right is not None:
self.right.preorder(list)

def postorder(self, list):

Choose a reason for hiding this comment

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

👍

if self.left is not None:
self.left.postorder(list)

if self.right is not None:
self.right.postorder(list)

list.append({
"key": self.key,
"value": self.value
})

def height(self):

Choose a reason for hiding this comment

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

👍

if self.left is None:
leftHeight = 0
else:
leftHeight = self.left.height()

if self.right is None:
rightHeight = 0
else:
rightHeight = self.right.height()

return 1 + max(leftHeight, rightHeight)


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

# Time Complexity:
# Space Complexity:
def add(self, key, value = None):
pass
# Time Complexity:
# Space Complexity:
def add(self, key, value=None):
Comment on lines +77 to +79

Choose a reason for hiding this comment

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

👍 Time/space Complexity?

if self.root is None:
self.root = TreeNode(key, value)
return self.root
else:
return self.root.add(key, value)

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

return self.find_helper(current.right, key)

# Time Complexity:
# Space Complexity:
# Time Complexity: o(1)
# Space Complexity:o(n)
def find(self, key):
Comment on lines +96 to 98

Choose a reason for hiding this comment

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

👍 However the time/space complexity are both O(n) if the tree is unbalanced and O(log n) if it's balanced.

pass
return self.find_helper(self.root, key)

# Time Complexity:
# Space Complexity:
# Time Complexity: o(1)
# Space Complexity: o(1)
def inorder(self):
Comment on lines +101 to 103

Choose a reason for hiding this comment

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

👍 Time/space Complexity are both O(n)

pass
list = []
if self.root is not None:
self.root.inorder(list)
return list

# Time Complexity: o(n)
# Space Complexity:0(n)

# Time Complexity:
# Space Complexity:
def preorder(self):
Comment on lines +109 to 112

Choose a reason for hiding this comment

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

👍 Time/space Complexity are both O(n)

pass
list = []
if self.root is not None:
self.root.preorder(list)
return list

# Time Complexity:
# Space Complexity:
# Time Complexity: 0(log n)
# Space Complexity:o(1)
def postorder(self):
Comment on lines +118 to 120

Choose a reason for hiding this comment

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

👍 Time/space Complexity are both O(n)

pass
list = []
if self.root is not None:
self.root.postorder(list)
return list

# Time Complexity:
# Space Complexity:
# Time Complexity: o(n)
# Space Complexity:0(n)
def height(self):
Comment on lines +126 to 128

Choose a reason for hiding this comment

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

👍

pass
if self.root is None:
return 0
return self.root.height()


# # Optional Method
# # Time Complexity:
# # Space Complexity:
# # Time Complexity:
# # Space Complexity:

def bfs(self):
pass




# # Useful for printing

def to_s(self):
return f"{self.inorder()}"
Binary file modified tests/__pycache__/__init__.cpython-39.pyc
Binary file not shown.