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
148 changes: 131 additions & 17 deletions binary_search_tree/tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from lib2to3.pytree import Node

from requests import post
from toml import TomlPreserveCommentDecoder


class TreeNode:
def __init__(self, key, val = None):
if val == None:
Expand All @@ -8,52 +14,160 @@ 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(self, key, value = None):

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

pass
node = TreeNode(key, value)
if self.root == None:
self.root = node
return

# Time Complexity:
# Space Complexity:
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: O(log n)
# Space Complexity: O(1)

def find(self, key):

Choose a reason for hiding this comment

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

✨ Nice iterative solution

pass

if self.root == None:
return None

node = self.root

while node != None:
if (node.key == key):
return node.value
if node.key < key:
node = node.right
else:
node = node.left




# Time Complexity:
# Space Complexity:
Comment on lines 64 to 65

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?


def inorder_helper_norecursion(self, current_node, trav_list):

Choose a reason for hiding this comment

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

🌟😎 Really cool you did it with both

#Initialize a holding place
stack = []

# Loop until all the nodes are visited
while current_node or stack:
# If current node ,then push it in the stack and assign current to left node
if current_node:
stack.append(current_node)
current_node = current_node.left
# If current is none, pop the value and append it to the node visited
else:
current_node = stack.pop()
trav_list.append({'key':current_node.key, 'value':current_node.value})
# Now get the right node
current_node = current_node.right
return trav_list

def inorder_helper(self, current_node, trav_list):
if current_node == None:
return
if current_node.left:
trav_list = self.inorder_helper(current_node.left, trav_list)
# visit root node
trav_list.append({'key':current_node.key, 'value':current_node.value})
# visit right Tree
if current_node.right:
trav_list = self.inorder_helper(current_node.right, trav_list)
return trav_list

def inorder(self):
pass
trav_list = []
#now call the helper to append to this array away from the fray of traversal
self.inorder_helper(self.root, trav_list)
return trav_list

# Time Complexity:
# Space Complexity:
Comment on lines 103 to 104

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?


def preorder_helper(self, current_node, trav_list):
if current_node == None:
return []
# visit root node
trav_list.append({'key':current_node.key, 'value':current_node.value})

# visit right Tree
if current_node.left:
trav_list = self.preorder_helper(current_node.left, trav_list)

if current_node.right:
trav_list = self.preorder_helper(current_node.right, trav_list)

return trav_list

def preorder(self):

Choose a reason for hiding this comment

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

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


# Time Complexity:
# Space Complexity:
# Space Complexity:
Comment on lines 127 to +128

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def postorder_helper(self, current_node, trav_list):
if current_node == None:
return []

# visit right Tree
if current_node.left:
trav_list = self.postorder_helper(current_node.left, trav_list)

if current_node.right:
trav_list = self.postorder_helper(current_node.right, trav_list)

# visit root node
trav_list.append({'key':current_node.key, 'value':current_node.value})


return trav_list


def postorder(self):

Choose a reason for hiding this comment

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

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

# Time Complexity:
# Space Complexity:
Comment on lines 152 to 153

Choose a reason for hiding this comment

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

⏱🪐 Time and space complexity?

def height(self):
pass
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)
return (max(height_left, height_right)+1)
else:
return 0

def height (self):

Choose a reason for hiding this comment

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

return self.height_helper(self.root)

# # Optional Method
# # Time Complexity:
# # Space Complexity:
def bfs(self):
pass




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