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

Update binary_tree_part_2.py #93

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
17 changes: 10 additions & 7 deletions data_structures/9_Binary_Tree_2/binary_tree_part_2.py
Original file line number Diff line number Diff line change
@@ -49,24 +49,27 @@ def in_order_traversal(self):
return elements

def delete(self, val):
if val < self.data:
#traverse till you find the node to delete
if val < self.data: #then go left
if self.left:
self.left = self.left.delete(val)
elif val > self.data:
elif val > self.data: #then go right
if self.right:
self.right = self.right.delete(val)
else:
if self.left is None and self.right is None:
else:#found node to delete i.e. val = self.data
if self.left is None and self.right is None: #case of deleting leaf node
return None
elif self.left is None:
elif self.left is None: #case of deleting node with one child
return self.right
elif self.right is None:
elif self.right is None: #case of deleting node with one child
return self.left

#case of deleting node with two children
min_val = self.right.find_min()
self.data = min_val
self.right = self.right.delete(min_val)


#if not leaf, then you have to return self
return self

def find_max(self):