fix: correct the wrong code of Deletion (#263)

This commit is contained in:
RequireSun 2018-12-11 12:00:22 +08:00 committed by Oleksii Trekhleb
parent e4ba378767
commit 3844e9f1da

View File

@ -103,19 +103,31 @@ remove(value)
else
parent.right ← nodeToRemove.right
end if
else if nodeToRemove.left = ø and nodeToRemove.right = ø
if nodeToRemove.value < parent.value
parent.left ← nodeToRemove.left
else if nodeToRemove.left != ø and nodeToRemove.right != ø
next ← nodeToRemove.right
while next.left != ø
next ← next.left
end while
if next != nodeToRemove.right
remove(next.value)
nodeToRemove.value ← next.value
else
parent.right ← nodeToRemove.left
nodeToRemove.value ← next.value
nodeToRemove.right ← nodeToRemove.right.right
end if
else
largestValue ← nodeToRemove.left
while largestValue.right = ø
largestValue ← largestValue.right
end while
findParent(largestValue.value).right ← ø
nodeToRemove.value ← largestValue.value
if nodeToRemove.left = ø
next ← nodeToRemove.right
else
next ← nodeToRemove.left
end if
if root = nodeToRemove
root = next
else if parent.left = nodeToRemove
parent.left = next
else if parent.right = nodeToRemove
parent.right = next
end if
end if
count ← count - 1
return true