Skip to content

Commit a073c62

Browse files
authored
Fix bug
The current code would fail when remove the root element in a tree where right subtree only have right child. # 17 # 0 20 # -5 5 25 Current code would produce a result with duplicate elements # 20 # 0 20 # -5 5 25 Trick is to move one the assignment to root value to the bottom
1 parent 916ef1a commit a073c62

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Trees/bst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def remove(self, data):
131131
delNodeParent = delNode
132132
delNode = delNode.leftChild
133133

134-
self.root.value = delNode.value
135134
if delNode.rightChild:
136135
if delNodeParent.value > delNode.value:
137136
delNodeParent.leftChild = delNode.rightChild
@@ -142,6 +141,7 @@ def remove(self, data):
142141
delNodeParent.leftChild = None
143142
else:
144143
delNodeParent.rightChild = None
144+
self.root.value = delNode.value
145145

146146
return True
147147

@@ -233,4 +233,4 @@ def main():
233233
print(bst.remove(10))
234234
bst.preorder()
235235

236-
main()
236+
main()

0 commit comments

Comments
 (0)