Skip to content

Commit

Permalink
Update 6.5.md
Browse files Browse the repository at this point in the history
  • Loading branch information
walkccc committed Mar 25, 2019
1 parent f5ecd69 commit e69b8eb
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions docs/Chap06/6.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,43 @@ Both are not very efficient. Furthermore, if the priority can overflow or underf
```cpp
HEAP-DELETE(A, i)
A[i] = A[A.heap-size]
if A[i] > A[h.heap-size]
A[i] = A[A.heap-size]
MAX-HEAPIFY(A, i)
else
HEAP-INCREASE-KEY(A, i, A[A.heap-size])
A.heap-size = A.heap-size - 1
```

**Note:** The following algorithm is wrong. For example, given an array $A = [15, 7, 9, 1, 2, 3, 8]$ which is a max-heap, and if we delete $A[5] = 2$, then it will fail.

```cpp
HEAP-DELETE(A, i)
A[i] = A[A.heap-size]
MAX-HEAPIFY(A, i)
A.heap-size = A.heap-size - 1
```

We just move the last element of the heap to the deleated position and then call $\text{MAX-HEAPIFY}$ on it. This works, because the element is already smaller than its parent (because it was already under it on the heap), but might be larger than its children. $\text{MAX-HEAPIFY}$ restored the heap property.
- before:

```
15
/ \
7 9
/ \ / \
1 2 3 8
```
- after (which is wrong since $8 > 7$ violates the max-heap property):
```
15
/ \
7 9
/ \ /
1 8 3
```
## 6.5-9
Expand Down

0 comments on commit e69b8eb

Please sign in to comment.