Skip to content
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
113 changes: 95 additions & 18 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from xml.dom import Node

Choose a reason for hiding this comment

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

It looks like this import statement never got used, so you can delete it.



class TreeNode:
def __init__(self, key, val = None):
if val == None:
Expand All @@ -7,42 +10,116 @@ def __init__(self, key, val = None):
self.value = val
self.left = None
self.right = None

def node_height(self):
if self == None:
return 0
else:
if self.left:
h_left = self.left.node_height()
else:
h_left = 0
if self.right:
h_right = self.right.node_height()
else:
h_right = 0

if (h_left > h_right):
return h_left + 1
else:
return h_right + 1



class Tree:
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 self.root is None:
self.root = node
return
current = self.root
while current:
if current.key > key:
if current.left is None:
current.left = node
return
else:
current = current.left
else:
if current.right is None:
current.right = node
return
else:
current = current.right

# Time Complexity:
# Space Complexity:

# Time Complexity: O(log n)
# Space Complexity: 0(1)
def find(self, key):
pass
current = self.root
while current != None:
if current.key == key:
return current.value
elif key < current.key:
current = current.left
else:
current = current.right
return None



# Time Complexity: O(n)
# Space Complexity: O(n)
def inorder_helper(self, current, inorder_list):
if current:
self.inorder_helper(current.left, inorder_list)
inorder_list.append({"key": current.key , "value":current.value})
self.inorder_helper(current.right, inorder_list)

# Time Complexity:
# Space Complexity:
def inorder(self):
pass
inorder_list = []
self.inorder_helper(self.root, inorder_list)
return inorder_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def preorder_helper(self, current, preorder_list):
if current:
preorder_list.append({"key": current.key , "value":current.value})
self.preorder_helper(current.left, preorder_list)
self.preorder_helper(current.right, preorder_list)

def preorder(self):
pass
preorder_list = []
self.preorder_helper(self.root, preorder_list)
return preorder_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)
def postorder_helper(self, current, postorder_list):
if current:
self.postorder_helper(current.left, postorder_list)
self.postorder_helper(current.right, postorder_list)
postorder_list.append({"key": current.key , "value":current.value})
def postorder(self):
pass
postorder_list = []
self.postorder_helper(self.root, postorder_list)
return postorder_list

# Time Complexity:
# Space Complexity:
# Time Complexity: O(n)
# Space Complexity: O(n)

Choose a reason for hiding this comment

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

These are both correct assuming the tree is unbalanced, but if balanced it'd move down to O(log(n))

Choose a reason for hiding this comment

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

Sorry, I have decided this is a nonsensical assumption to move down to O(log(n)) for time complexity as we still have to travel to every node even in a balanced tree. Likewise, as there is no additional space being used, I think the space complexity should be O(1).

Forgive me for the confusion, but I was going off our instructor solution and I realized there is a mistake in their calculations too.

def height(self):
pass
if self.root == None:
return 0
return self.root.node_height()





# # Optional Method
Expand Down