mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
fix: correct the wrong code of Deletion
(#263)
This commit is contained in:
parent
e4ba378767
commit
3844e9f1da
@ -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
|
||||
parent.right ← nodeToRemove.left
|
||||
end if
|
||||
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
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user