Skip to content

Commit e34a458

Browse files
committed
BST deletion
1 parent a910190 commit e34a458

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

.DS_Store

-7 KB
Binary file not shown.

Data Structure with Python/Binary_Search_Tree.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
reload(sys)
33
sys.setdefaultencoding('utf-8')
44

5+
'''
6+
这里实现的时候一个没有parent指针的BST
7+
'''
8+
59
class TreeNode(object):
610
def __init__(self, x):
711
self.val = x
@@ -77,4 +81,28 @@ def tree_maximum(self, root):
7781
root = root.right
7882
return root
7983

80-
84+
'''
85+
一个需要背下来的程序段
86+
'''
87+
88+
def delete(self, root, value):
89+
if not root:
90+
return None
91+
92+
if root.val < value:
93+
root.right = self.delete(root.right, value)
94+
elif root.val > value:
95+
root.left = self.delete(root.left, value)
96+
else:
97+
if not root.left:
98+
tmp = root.right
99+
root = None
100+
return tmp
101+
elif not root.right:
102+
tmp = root.left
103+
root = None
104+
return tmp
105+
tmp = self.tree_minimum(root.right)
106+
root.val = tmp.val
107+
root.right = self.delete(root.right, root.val)
108+
return tmp

书/.DS_Store

-4 KB
Binary file not shown.

经典算法汇总/.DS_Store

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)