mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +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
|
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 != ø
|
||||||
|
next ← next.left
|
||||||
|
end while
|
||||||
|
if next != nodeToRemove.right
|
||||||
|
remove(next.value)
|
||||||
|
nodeToRemove.value ← next.value
|
||||||
else
|
else
|
||||||
parent.right ← nodeToRemove.left
|
nodeToRemove.value ← next.value
|
||||||
|
nodeToRemove.right ← nodeToRemove.right.right
|
||||||
end if
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user