Proofs: Min-max heap
These are the human proofs of Min-max heap, at full length.
The operations of a min-max heap keep the min-max order full
This proof shows: correctness.
Call a pair of nodes, with a proper ancestor of , good if when is a min node and when is a max node. The heap is in min-max order if every pair is good. For a node , write for its subtree, and say that if when is a min node and when is a max node. "Better for the level of " means , the strict form.
Lemma 0 (answers). In min-max order, is a smallest key; a largest key is if , if , and the larger of and if .
Proof. The root is a min node and an ancestor of all other nodes. Nodes 1 and 2 are max nodes, and every node other than the root is in or .
Lemma 1 (best candidate). Let have a child, and let every pair inside whose upper node is not be good. Let be a child or grandchild of with the -best key among them. Then for every in other than .
Proof. Say is a min node; the other case is the same with the order reversed. The children of are max nodes and its grandchildren are min nodes. A node of is a child or a grandchild of , so by the choice of , or it is a proper descendant of a grandchild , so because is good.
Lemma 2 (PUSH-DOWN). If every pair inside whose upper node is not is good, then after every pair inside is good, and the keys of are permuted among its nodes.
Proof. By induction on the height of . All changes are swaps inside , so the keys are permuted. If has no child, there is no pair to check. Let be a min node; the max case is symmetric. Let be the key at at the start, and let be chosen at line L2. By Lemma 1, is a smallest key of other than .
Case 1: is a child (lines L3 to L5). If , then is at most every key of , and every pair is good. Otherwise the keys swap. Now is the old , a smallest key of , so the pairs are good. The node now holds . Its descendants are grandchildren of or below them, so by Lemma 1, and because was good. So all of them equal , and the pairs , which need , are good. No other pair changed.
Case 2: is a grandchild and (line L6). Then is at most every key of , and all pairs are good.
Case 3: is a grandchild and (lines L7 to L9). After line L7, , the old , which is a smallest key of , so the pairs are good, also after the later steps, since they only permute keys of . Let be the parent of , a max node, and let be its key. Before line L8, every old key of , because those pairs were good. If , line L8 swaps, so holds , which is larger than every other key now in : those are and old keys of . If , keeps , which is at least and every old key of . Either way, every pair is good now and remains good, since the recursion only permutes the keys in . Pairs whose upper node is in but not in did not change. So the only pairs that may be bad have their upper node at , and the loop continues with . By induction, it makes all pairs inside good.
Lemma 3 (PUSH-UP). If every pair not involving the leaf is good, then after every pair is good.
Proof. Let be the key at . If there is nothing to prove. Let be the parent of . Say is a min node and a max node; the other case is symmetric. Note that the ancestors of alternate between max nodes (, its grandparent, and so on) and min nodes.
If (line L12 true). Line L13 swaps. The leaf now holds . All pairs are good: now holds ; the other ancestors of are ancestors of , and was good for . Now is at . The pairs are good, because every key of other than was at most . A min ancestor of satisfies . Only the pairs with a max ancestor of may be bad, and these are the grandparent chain of . Lines L14 and L15 move up this chain while it is larger than the next grandparent . Each swap puts the key , which was at least every key below , at the position of below it; so the pairs below the new place of stay good, and the min node between them holds a key at most , since it was below . When the loop stops, is at most its next max ancestor and so at most all of them, and it is larger than all keys below it and than its min ancestors, by the same argument as for . All pairs are good.
If (line L12 false). Every max ancestor of is or an ancestor of , so , and these pairs are good. Only the pairs with a min ancestor may be bad, and these are the grandparent chain of . Lines L14 and L15 move up this chain while it is smaller than the next grandparent, with the same argument as above, and the order reversed.
Theorem. INSERT, DELETE-MIN, DELETE-MAX and the build keep min-max order, and find-min, find-max, DELETE-MIN and DELETE-MAX return correct keys.
Proof. Insert. Before line L20, all pairs are good; the new leaf adds only pairs that involve it, so Lemma 3 applies.
Delete-min. By Lemma 0, is a smallest key. After the last key moves to the root, every pair whose upper node is not the root is a pair of the old heap, so it is good. Lemma 2 at makes all pairs good.
Delete-max. By Lemma 0, line L18 finds a largest key. If is the last cell, removing it removes only pairs. Otherwise the last key moves to . If , then before the delete and nothing remains. If , the only pair with upper node outside that involves is , and because the root was a smallest key. Inside , all pairs with upper node other than are old pairs. Lemma 2 at makes all pairs inside good, and pairs stay good since only permutes its keys.
Build. By induction on the steps: when PUSH-DOWN runs at , the nodes with a larger index have already been processed, or are leaves, so the subtrees of the children of are in order, and every pair inside whose upper node is not is good. Lemma 2 makes in order. After , the whole heap is in order.
Find-min and find-max are correct by Lemma 0.