Min heap and binary search tree Problème 1.
Consider the following sequence of values S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51, 64] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ]
We can represent tree textually via the following representation
13 (
11 (
8 (
2
4
)
12 (
*
1
)
)
7 (
5
6 (
99
*
)
)
)
Where we use ∗ * ∗ as a placeholder for a missing child for those nodes that only have a single child.
Draw the min heap (as a tree) obtained by adding the values in S S S in sequence. Show each step
S = [ 3 ] S = [3] S = [ 3 ] . The root of the heap.
3
S = [ 3 , 42 ] S = [3, 42] S = [ 3 , 42 ] . Added to the left of the root.
3 (
42
*
)
S = [ 3 , 42 , 39 ] S = [3, 42, 39] S = [ 3 , 42 , 39 ] . Added to the right of the root.
3 (
42
39
)
S = [ 3 , 42 , 39 , 86 ] S = [3, 42, 39, 86] S = [ 3 , 42 , 39 , 86 ] . Added to the left of the left child of the root. (42 < 86)
3 (
42 (
86
*
)
39
)
S = [ 3 , 42 , 39 , 86 , 49 ] S = [3, 42, 39, 86, 49] S = [ 3 , 42 , 39 , 86 , 49 ] . Added to the right of 42.
3 (
42 (
86
49
)
39
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 ] S = [3, 42, 39, 86, 49, 89] S = [ 3 , 42 , 39 , 86 , 49 , 89 ] . Added to the left of 39.
3 (
42 (
86
49
)
39 (
89
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] S = [3, 42, 39, 86, 49, 89, 99] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] . Added to the right of 39.
3 (
42 (
86
49
)
39 (
89
99
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] S = [3, 42, 39, 86, 49, 89, 99, 20] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] . 20 becomes left child of 86. (20 < 86) then swap. (20 < 42) then swap.
3 (
20 (
42
(
86
*
)
49
)
39 (
89
99
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] . 88 becomes right of 42
3 (
20 (
42 (
86
88
)
49
)
39 (
89
99
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] . 51 becomes right of 49
3 (
20 (
42 (
86
88
)
49 (
51
*
)
)
39 (
89
99
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51, 64] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] . 64 becomes right of 49
3 (
20 (
42 (
86
88
)
49 (
51
64
)
)
39 (
89
99
)
)
Draw the max heap (as a tree) obtained by adding the values in S S S in sequence. Show each step
S = [ 3 ] S = [3] S = [ 3 ] . The root of the heap.
3
S = [ 3 , 42 ] S = [3, 42] S = [ 3 , 42 ] . 42 becomes the root, 3 becomes left child.
42 (
3
*
)
S = [ 3 , 42 , 39 ] S = [3, 42, 39] S = [ 3 , 42 , 39 ] . 39 becomes right child.
42 (
3
39
)
S = [ 3 , 42 , 39 , 86 ] S = [3, 42, 39, 86] S = [ 3 , 42 , 39 , 86 ] . 86 becomes root. 42 becomes left child, 3 becomes left child of 42.
86 (
42 (
3
*
)
39
)
S = [ 3 , 42 , 39 , 86 , 49 ] S = [3, 42, 39, 86, 49] S = [ 3 , 42 , 39 , 86 , 49 ] . 49 becomes left child of 86, swap 42, 42 becomes right child of 49.
86 (
49 (
3
42
)
39
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 ] S = [3, 42, 39, 86, 49, 89] S = [ 3 , 42 , 39 , 86 , 49 , 89 ] . 89 become routes, swap 86, 49.
89 (
49 (
3
42
)
86 (
39
*
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] S = [3, 42, 39, 86, 49, 89, 99] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] . 99 becomes root, swap 89, 49.
99 (
49 (
3
42
)
89 (
39
86
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] S = [3, 42, 39, 86, 49, 89, 99, 20] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] . 20 swap with 3, 3 becomes left child of 20.
99 (
49 (
20 (
3
*
)
42
)
89 (
39
86
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] . 88 becomes left child of 99, swap 49, 20.
99 (
88 (
49 (
20 (
3
*
)
*
)
42
)
89 (
39
86
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] . 51 becomes right child of 88, swap 42, 20.
99 (
88 (
51 (
42 (
20 (
3
*
)
)
*
)
49
)
89 (
39
86
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51, 64] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] . 64, pushes 49 down.
99 (
88 (
51 (
42 (
20 (
3
*
)
)
*
)
64 (
49
*
)
)
89 (
39
86
)
)
Draw the binary search tree obtained by adding the values in S S S in sequence. Show each step
S = [ 3 ] S = [3] S = [ 3 ] . The root of the tree.
3
S = [ 3 , 42 ] S = [3, 42] S = [ 3 , 42 ] . 42 becomes the right child of 3.
3 (
*
42
)
S = [ 3 , 42 , 39 ] S = [3, 42, 39] S = [ 3 , 42 , 39 ] . 39 becomes the left child of 42.
3 (
*
42 (
39
*
)
)
S = [ 3 , 42 , 39 , 86 ] S = [3, 42, 39, 86] S = [ 3 , 42 , 39 , 86 ] . 86 becomes the right child of 42.
3 (
*
42 (
39
86
)
)
S = [ 3 , 42 , 39 , 86 , 49 ] S = [3, 42, 39, 86, 49] S = [ 3 , 42 , 39 , 86 , 49 ] . 49 becomes the left child of 86.
3 (
*
42 (
39
86 (
49
*
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 ] S = [3, 42, 39, 86, 49, 89] S = [ 3 , 42 , 39 , 86 , 49 , 89 ] . 89 becomes the right child of 86.
3 (
*
42 (
39
86 (
49
89
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] S = [3, 42, 39, 86, 49, 89, 99] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 ] . 99 becomes the right child of 89.
3 (
*
42 (
39
86 (
49
89 (
*
99
)
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] S = [3, 42, 39, 86, 49, 89, 99, 20] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 ] . 20 becomes the left child of 39.
3 (
*
42 (
39 (
20
*
)
86 (
49
89 (
*
99
)
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 ] . 88 becomes the right child of 86.
3 (
*
42 (
39 (
20
*
)
86 (
49
89 (
88
99
)
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 ] . 51 becomes the right child of 49.
3 (
*
42 (
39 (
20
*
)
86 (
49 (
*
51
)
89 (
88
99
)
)
)
)
S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] S = [3, 42, 39, 86, 49, 89, 99, 20, 88, 51, 64] S = [ 3 , 42 , 39 , 86 , 49 , 89 , 99 , 20 , 88 , 51 , 64 ] . 64 becomes the left child of 51.
3 (
*
42 (
39 (
20
*
)
86 (
49 (
*
51 (
*
64
)
)
89 (
88
99
)
)
)
)
Problème 2.
Given an ordered list L L L and value v v v , the LowerBound
algorithm provide the position p p p in list L L L such that p p p is the first offset in L L L of a value larger-equal to v v v . Hence, v ≤ L [ p ] v \leq L[p] v ≤ L [ p ] (or, if no such offset exists, p = ∣ L ∣ p = |L| p = ∣ L ∣ ). The LowerBound
algorithm does so in Θ ( log 2 ( ∣ L ∣ ) ) \Theta(\log_2(|L|)) Θ ( log 2 ( ∣ L ∣ )) comparisons. Argue that LowerBound
is worst-case optimal : any algorithm that finds the correct position p p p for any inputs L L L and v v v using only comparisons will require Θ ( log 2 ( ∣ L ∣ ) ) \Theta(\log_2(|L|)) Θ ( log 2 ( ∣ L ∣ )) comparisons.
Solution
For a list of size ∣ L ∣ |L| ∣ L ∣ there are ∣ L ∣ + 1 |L| + 1 ∣ L ∣ + 1 possible outcomes for the position p p p in the list. The minimum height of a binary tree needed for ∣ L ∣ + 1 |L| + 1 ∣ L ∣ + 1 outcomes is log 2 ( ∣ L ∣ + 1 ) \log_2(|L| + 1) log 2 ( ∣ L ∣ + 1 ) (at most 2 h 2^h 2 h leaves or 2 h ≥ ∣ L ∣ + 1 → h ≥ log 2 ( ∣ L ∣ + 1 ) 2^h \geq |L| + 1 \rightarrow h \geq \log_2(|L| +1) 2 h ≥ ∣ L ∣ + 1 → h ≥ log 2 ( ∣ L ∣ + 1 )
From Stirling’s approximation, comparison-based sorting algorithm lower bound is Ω ( n log ( n ) ) \Omega(n \log(n)) Ω ( n log ( n )) . Given that the algorithm operates in Θ ( log 2 ( ∣ L ∣ ) ) \Theta(\log_2(|L|)) Θ ( log 2 ( ∣ L ∣ )) comparisons, it matches with the theoretical lower bound for the search algorithm. Therefore, no comparison-based algorithm can guarantee a better worst-case performance for position p p p , making LowerBound
the worst-case optimal.
Problème 3.
Min heaps and max heaps allow one to efficiently store values and efficiently look up and remove the smallest values and largest values , respectively. One cannot easily remove the largest value from a min heap or the smallest value from a max heap, however.
Assume a value v v v is a part of a min heap of at-most n n n values and that we know v is stored at position p p p in that heap. Provide an algorithm that can remove v v v from the heap in worst-case O ( log 2 ( n ) ) \mathcal{O}(\log_2(n)) O ( log 2 ( n ))
"\\begin{algorithm}\n\\caption{RemoveValue($heap, p$)}\n\\begin{algorithmic}\n\\Procedure{RemoveValue}{$heap, i$}\n \\State $n \\gets heap.length$\n \\State $temp \\gets heap[p]$\n \\State $heap[p] \\gets heap[n]$\n \\State $heap[n] \\gets temp$\n \\State $heap \\gets heap[:n]$\n \\State $\\text{HeapifyDown}(heap, p)$\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 23 RemoveValue(h e a p , p heap, p h e a p , p )
1: procedure RemoveValue (h e a p , i heap, i h e a p , i )
2: n ← h e a p . l e n g t h n \gets heap.length n ← h e a p . l e n g t h
3: t e m p ← h e a p [ p ] temp \gets heap[p] t e m p ← h e a p [ p ]
4: h e a p [ p ] ← h e a p [ n ] heap[p] \gets heap[n] h e a p [ p ] ← h e a p [ n ]
5: h e a p [ n ] ← t e m p heap[n] \gets temp h e a p [ n ] ← t e m p
6: h e a p ← h e a p [ : n ] heap \gets heap[:n] h e a p ← h e a p [ : n ]
7: HeapifyDown ( h e a p , p ) \text{HeapifyDown}(heap, p) HeapifyDown ( h e a p , p )
8: end procedure
"\\begin{algorithm}\n\\caption{HeapifyDown($heap, p$)}\n\\begin{algorithmic}\n\\Procedure{HeapifyDown}{$heap, i$}\n \\State $n \\gets \\text{size of } heap$\n \\While{$\\text{lchild}(i) \\leq n$}\n \\State $\\text{left} \\gets \\text{lchild}(i)$\n \\State $\\text{right} \\gets \\text{rchild}(i)$\n \\State $\\text{smallest} \\gets i$\n \\If{$\\text{left} \\leq n \\text{ and } heap[\\text{left}] < heap[\\text{smallest}]$}\n \\State $\\text{smallest} \\gets \\text{left}$\n \\EndIf\n \\If{$\\text{right} \\leq n \\text{ and } heap[\\text{right}] < heap[\\text{smallest}]$}\n \\State $\\text{smallest} \\gets \\text{right}$\n \\EndIf\n\n \\If{$\\text{smallest} = i$}\n \\State \\textbf{break}\n \\Else\n \\State $\\text{Swap } heap[i] \\text{ with } heap[\\text{smallest}]$\n \\State $i \\gets \\text{smallest}$\n \\EndIf\n \\EndWhile\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 24 HeapifyDown(h e a p , p heap, p h e a p , p )
1: procedure HeapifyDown (h e a p , i heap, i h e a p , i )
2: n ← size of h e a p n \gets \text{size of } heap n ← size of h e a p
3: while lchild ( i ) ≤ n \text{lchild}(i) \leq n lchild ( i ) ≤ n do
4: left ← lchild ( i ) \text{left} \gets \text{lchild}(i) left ← lchild ( i )
5: right ← rchild ( i ) \text{right} \gets \text{rchild}(i) right ← rchild ( i )
6: smallest ← i \text{smallest} \gets i smallest ← i
7: if left ≤ n and h e a p [ left ] < h e a p [ smallest ] \text{left} \leq n \text{ and } heap[\text{left}] < heap[\text{smallest}] left ≤ n and h e a p [ left ] < h e a p [ smallest ] then
8: smallest ← left \text{smallest} \gets \text{left} smallest ← left
9: end if
10: if right ≤ n and h e a p [ right ] < h e a p [ smallest ] \text{right} \leq n \text{ and } heap[\text{right}] < heap[\text{smallest}] right ≤ n and h e a p [ right ] < h e a p [ smallest ] then
11: smallest ← right \text{smallest} \gets \text{right} smallest ← right
12: end if
13: if smallest = i \text{smallest} = i smallest = i then
15: else
16: Swap h e a p [ i ] with h e a p [ smallest ] \text{Swap } heap[i] \text{ with } heap[\text{smallest}] Swap h e a p [ i ] with h e a p [ smallest ]
17: i ← smallest i \gets \text{smallest} i ← smallest
18: end if
19: end while
20: end procedure
Provide a data structure that allows one to efficiently store values and efficiently look up and remove both the smallest and the largest values: all three of these operations should be supported in Θ ( log 2 ( n ) ) \Theta(\log_2(n)) Θ ( log 2 ( n ))
We will implement a Double-ended Priority Queue (DEPQ), which is a min-max heap.
"\\begin{algorithm}\n\\caption{Insert($heap, v$)}\n\\begin{algorithmic}\n\\Procedure{Insert}{$heap, v$}\n \\State $heap.push(v)$\n \\State $\\text{Swim}(heap, \\text{size}(heap))$\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 25 Insert(h e a p , v heap, v h e a p , v )
1: procedure Insert (h e a p , v heap, v h e a p , v )
2: h e a p . p u s h ( v ) heap.push(v) h e a p . p u s h ( v )
3: Swim ( h e a p , size ( h e a p ) ) \text{Swim}(heap, \text{size}(heap)) Swim ( h e a p , size ( h e a p ))
4: end procedure
"\\begin{algorithm}\n\\caption{RemoveMin($heap$)}\n\\begin{algorithmic}\n\\Procedure{RemoveMin}{$heap$}\n \\State $n \\gets \\text{size}(heap)$\n \\State $temp \\gets heap[1]$\n \\State $heap[1] \\gets heap[\\text{size}(heap)]$\n \\State $heap[n] \\gets temp$\n \\State $heap \\gets heap[:n]$\n \\State $\\text{Sink}(heap, 1)$\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 26 RemoveMin(h e a p heap h e a p )
1: procedure RemoveMin (h e a p heap h e a p )
2: n ← size ( h e a p ) n \gets \text{size}(heap) n ← size ( h e a p )
3: t e m p ← h e a p [ 1 ] temp \gets heap[1] t e m p ← h e a p [ 1 ]
4: h e a p [ 1 ] ← h e a p [ size ( h e a p ) ] heap[1] \gets heap[\text{size}(heap)] h e a p [ 1 ] ← h e a p [ size ( h e a p )]
5: h e a p [ n ] ← t e m p heap[n] \gets temp h e a p [ n ] ← t e m p
6: h e a p ← h e a p [ : n ] heap \gets heap[:n] h e a p ← h e a p [ : n ]
7: Sink ( h e a p , 1 ) \text{Sink}(heap, 1) Sink ( h e a p , 1 )
8: end procedure
"\\begin{algorithm}\n\\caption{RemoveMax($heap$)}\n\\begin{algorithmic}\n\\Procedure{RemoveMax}{$heap$}\n \\State $maxPos \\gets \\text{argmax}\\{heap[2], heap[3]\\}$\n \\State $heap[maxPos] \\gets heap[\\text{size}(heap)]$\n \\State $\\text{remove last el from} heap$\n \\State $\\text{Sink}(heap, maxPos)$\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 27 RemoveMax(h e a p heap h e a p )
1: procedure RemoveMax (h e a p heap h e a p )
2: m a x P o s ← argmax { h e a p [ 2 ] , h e a p [ 3 ] } maxPos \gets \text{argmax}\{heap[2], heap[3]\} ma x P os ← argmax { h e a p [ 2 ] , h e a p [ 3 ]}
3: h e a p [ m a x P o s ] ← h e a p [ size ( h e a p ) ] heap[maxPos] \gets heap[\text{size}(heap)] h e a p [ ma x P os ] ← h e a p [ size ( h e a p )]
4: remove last el from h e a p \text{remove last el from} heap remove last el from h e a p
5: Sink ( h e a p , m a x P o s ) \text{Sink}(heap, maxPos) Sink ( h e a p , ma x P os )
6: end procedure
"\\begin{algorithm}\n\\caption{Swim($heap, i$)}\n\\begin{algorithmic}\n\\Procedure{Swim}{$heap, i$}\n \\While{$i > 1$}\n \\State $parent \\gets \\lfloor i/2 \\rfloor$\n \\State $grandParent \\gets \\lfloor parent/2 \\rfloor$\n \\If{$(i \\mod 2 = 0 \\text{ and } heap[i] < heap[parent]) \\text{ or } (i \\mod 2 \\neq 0 \\text{ and } heap[i] > heap[parent])$}\n \\State $\\text{Swap}(heap[i], heap[parent])$\n \\EndIf\n \\If{$grandParent \\geq 1 \\text{ and } (heap[i] < heap[grandParent] \\text{ or } heap[i] > heap[grandParent])$}\n \\State $\\text{Swap}(heap[i], heap[grandParent])$\n \\EndIf\n \\State $i \\gets parent$\n \\EndWhile\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 28 Swim(h e a p , i heap, i h e a p , i )
1: procedure Swim (h e a p , i heap, i h e a p , i )
2: while i > 1 i > 1 i > 1 do
3: p a r e n t ← ⌊ i / 2 ⌋ parent \gets \lfloor i/2 \rfloor p a re n t ← ⌊ i /2 ⌋
4: g r a n d P a r e n t ← ⌊ p a r e n t / 2 ⌋ grandParent \gets \lfloor parent/2 \rfloor g r an d P a re n t ← ⌊ p a re n t /2 ⌋
5: if ( i m o d 2 = 0 and h e a p [ i ] < h e a p [ p a r e n t ] ) or ( i m o d 2 ≠ 0 and h e a p [ i ] > h e a p [ p a r e n t ] ) (i \mod 2 = 0 \text{ and } heap[i] < heap[parent]) \text{ or } (i \mod 2 \neq 0 \text{ and } heap[i] > heap[parent]) ( i mod 2 = 0 and h e a p [ i ] < h e a p [ p a re n t ]) or ( i mod 2 = 0 and h e a p [ i ] > h e a p [ p a re n t ]) then
6: Swap ( h e a p [ i ] , h e a p [ p a r e n t ] ) \text{Swap}(heap[i], heap[parent]) Swap ( h e a p [ i ] , h e a p [ p a re n t ])
7: end if
8: if g r a n d P a r e n t ≥ 1 and ( h e a p [ i ] < h e a p [ g r a n d P a r e n t ] or h e a p [ i ] > h e a p [ g r a n d P a r e n t ] ) grandParent \geq 1 \text{ and } (heap[i] < heap[grandParent] \text{ or } heap[i] > heap[grandParent]) g r an d P a re n t ≥ 1 and ( h e a p [ i ] < h e a p [ g r an d P a re n t ] or h e a p [ i ] > h e a p [ g r an d P a re n t ]) then
9: Swap ( h e a p [ i ] , h e a p [ g r a n d P a r e n t ] ) \text{Swap}(heap[i], heap[grandParent]) Swap ( h e a p [ i ] , h e a p [ g r an d P a re n t ])
10: end if
11: i ← p a r e n t i \gets parent i ← p a re n t
12: end while
13: end procedure
"\\begin{algorithm}\n\\caption{Sink($heap, i$)}\n\\begin{algorithmic}\n\\Procedure{Sink}{$heap, i$}\n \\State $n \\gets \\text{size}(heap)$\n \\While{$\\text{lchild}(i) \\leq n$}\n \\State $left \\gets \\text{lchild}(i)$\n \\State $right \\gets \\text{rchild}(i)$\n \\State $target \\gets i$\n\n \\If{$\\text{on min level and } heap[left] < heap[target]$}\n \\State $target \\gets left$\n \\ElsIf{$\\text{on max level and } heap[left] > heap[target]$}\n \\State $target \\gets left$\n \\EndIf\n \\If{$right \\leq \\text{size}(heap)$}\n \\If{$\\text{on min level and } heap[right] < heap[target]$}\n \\State $target \\gets right$\n \\ElsIf{$\\text{on max level and } heap[right] > heap[target]$}\n \\State $target \\gets right$\n \\EndIf\n \\EndIf\n\n \\If{$target = i$}\n \\State \\textbf{break}\n \\Else\n \\State $\\text{Swap}(heap[i], heap[target])$\n \\State $i \\gets target$\n \\EndIf\n \\EndWhile\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"
Algorithm 29 Sink(h e a p , i heap, i h e a p , i )
1: procedure Sink (h e a p , i heap, i h e a p , i )
2: n ← size ( h e a p ) n \gets \text{size}(heap) n ← size ( h e a p )
3: while lchild ( i ) ≤ n \text{lchild}(i) \leq n lchild ( i ) ≤ n do
4: l e f t ← lchild ( i ) left \gets \text{lchild}(i) l e f t ← lchild ( i )
5: r i g h t ← rchild ( i ) right \gets \text{rchild}(i) r i g h t ← rchild ( i )
6: t a r g e t ← i target \gets i t a r g e t ← i
7: if on min level and h e a p [ l e f t ] < h e a p [ t a r g e t ] \text{on min level and } heap[left] < heap[target] on min level and h e a p [ l e f t ] < h e a p [ t a r g e t ] then
8: t a r g e t ← l e f t target \gets left t a r g e t ← l e f t
9: else if on max level and h e a p [ l e f t ] > h e a p [ t a r g e t ] \text{on max level and } heap[left] > heap[target] on max level and h e a p [ l e f t ] > h e a p [ t a r g e t ] then
10: t a r g e t ← l e f t target \gets left t a r g e t ← l e f t
11: end if
12: if r i g h t ≤ size ( h e a p ) right \leq \text{size}(heap) r i g h t ≤ size ( h e a p ) then
13: if on min level and h e a p [ r i g h t ] < h e a p [ t a r g e t ] \text{on min level and } heap[right] < heap[target] on min level and h e a p [ r i g h t ] < h e a p [ t a r g e t ] then
14: t a r g e t ← r i g h t target \gets right t a r g e t ← r i g h t
15: else if on max level and h e a p [ r i g h t ] > h e a p [ t a r g e t ] \text{on max level and } heap[right] > heap[target] on max level and h e a p [ r i g h t ] > h e a p [ t a r g e t ] then
16: t a r g e t ← r i g h t target \gets right t a r g e t ← r i g h t
17: end if
18: end if
19: if t a r g e t = i target = i t a r g e t = i then
21: else
22: Swap ( h e a p [ i ] , h e a p [ t a r g e t ] ) \text{Swap}(heap[i], heap[target]) Swap ( h e a p [ i ] , h e a p [ t a r g e t ])
23: i ← t a r g e t i \gets target i ← t a r g e t
24: end if
25: end while
26: end procedure