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 else
parent.right ← nodeToRemove.right parent.right ← nodeToRemove.right
end if end if
else if nodeToRemove.left = ø and nodeToRemove.right = ø else if nodeToRemove.left != ø and nodeToRemove.right != ø
if nodeToRemove.value < parent.value next ← nodeToRemove.right
parent.left ← nodeToRemove.left while next.left != ø
else next ← next.left
parent.right ← nodeToRemove.left end while
end if if next != nodeToRemove.right
remove(next.value)
nodeToRemove.value ← next.value
else
nodeToRemove.value ← next.value
nodeToRemove.right ← nodeToRemove.right.right
end if
else else
largestValue ← nodeToRemove.left if nodeToRemove.left = ø
while largestValue.right = ø next ← nodeToRemove.right
largestValue ← largestValue.right else
end while next ← nodeToRemove.left
findParent(largestValue.value).right ← ø end if
nodeToRemove.value ← largestValue.value 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 end if
count ← count - 1 count ← count - 1
return true return true