In computer science, a shadow heap is a mergeable heap data structure which supports efficient heap merging in the amortized sense. More specifically, shadow heaps make use of the shadow merge algorithm to achieve insertion in O(f(n)) amortized time and deletion in O((log n log log n)/f(n)) amortized time, for any choice of 1 ≤ f(n) ≤ log log n. Throughout this article, it is assumed that A and B are binary heaps with |A| ≤ |B|.
Shadow merge Shadow merge is an algorithm for merging two binary heaps efficiently if these heaps are implemented as arrays. Specifically, the running time of shadow merge on two heaps A {\displaystyle A} and B {\displaystyle B} is O ( | A | + min { log | B | log log | B | , log | A | log | B | } ) {\displaystyle O(|A|+\min\{\log |B|\log \log |B|,\log |A|\log |B|\})} .
Algorithm We wish to merge the two binary min-heaps A {\displaystyle A} and B {\displaystyle B} . The algorithm is as follows:
Concatenate the array A {\displaystyle A} at the end of the array B {\displaystyle B} to obtain an array C {\displaystyle C} . Identify the shadow of A {\displaystyle A} in C {\displaystyle C} ; that is, the ancestors of the last | A | {\displaystyle |A|} nodes in C {\displaystyle C} which destroy the heap property. Identify the following two parts of the shadow from C {\displaystyle C} : The path P {\displaystyle P} : the set of nodes in the shadow for which there are at most 2 at any depth of C {\displaystyle C} ; The subtree T {\displaystyle T} : the remainder of the shadow. Extract and sort the smallest | P | {\displaystyle |P|} nodes from the shadow into an array S {\displaystyle S} . Transform S {\displaystyle S} as follows: If | S | > | C | {\displaystyle |S|>|C|} , then starting from the smallest element in the sorted array, sequentially insert each element of S {\displaystyle S} into C {\displaystyle C} , replacing them with C {\displaystyle C} 's smallest elements. If | S | ≤ | C | {\displaystyle |S|\leq |C|} , then extract and sort the | P | {\displaystyle |P|} smallest elements from C {\displaystyle C} , and merge this sorted list with S {\displaystyle S} . Replace the elements of S {\displaystyle S} into their original positions in C {\displaystyle C} . Make a heap out of T {\displaystyle T} .
… excerpt ends here. Continue reading the full article.
