Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update binary_tree_part_2.py#93

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
avigod wants to merge1 commit intocodebasics:master
base:master
Choose a base branch
Loading
fromavigod:patch-1
Open
Changes fromall 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 deletionsdata_structures/9_Binary_Tree_2/binary_tree_part_2.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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):
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp