Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A double-ended priority queue is a data structure that supports the following operations:
inserting an element with an arbitrary key deleting an element with the largest key deleting an element with the smallest key
70 30 9 10
40 15
45
50
30
20
12
70 30 9 10
40 15 j
45
50
30
20
12
70 30 9 7
40 15 10
45
50
30
20
12
70 30 9 10
80 15 40
45
50
30
20
12
Program 9.4 Searching For The Correct Max Node For Insertion
template <class KeyType> void MinMaxHeap<KeyType>::VerifyMax(int i, const Elemetn<KeyType>& x) // Follow max nodes from the max node I to the root and insert x at proper place { for (int gp = i/4; // grandparent of i gp && (x.key > h[gp].key); gp /= 4) O(log n) { // move h[gp] to h[i] h[i] = h[gp]; i = gp; } h[i] = x; // x is to be inserted into node i }
70 30 9 10
40 15
45
50
30
20
The root has no children. In this case x is to be inserted into the root. The root has at least one child. Now the smallest key in the min-max heap is in one of the children or grandchildren of the root. Assume node k has the smallest key, then following conditions must be considered:
x.key h[k].key. x may be inserted into the root. x.key >h[k].key and k is a child of the root. Since k is a max node, it has not descendents with key larger than h[k].key. So, node k has no descendents with key larger than x.key. So the element h[k] may be moved to the root, and x can be inserted into node k. x.key> h[k] and k is a grandchild of the root. h[k] is moved to the root. Let p the parent of k. If x.key > h[p].key, then h[p] and x are to be interchanged.
70 30 12 10
40 15
45
50
30
20
Deaps
A deap is a double-ended heap that supports the double-ended priority operations of insert, delet-min, and delete-max. Similar to min-max heap but deap is faster on these operations by a constant factor, and the algorithms are simpler.
Deaps (Cont.)
Definition: A deap is a complete binary tree that is either empty or satisfies the following properties: (1) The root contains no element (2) The left subtree is a min heap. (3) The right subtree is a max heap. (4) If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such a j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.
A Deap Example
5 10 8 25
45 40
15
19
30
20
Deap (Cont.)
From Deaps definition, its obvious that for an n-element deap, the min element is the root of min heap and the max element is the root of the max heap. If n = 1, then the min and max elements are the same and are in the root of the min heap. Since deap is a complete binary tree, it may be stored as an implicit data structure in a one-dimension array similar to min, max, min-max heaps. In the case of deap, the position 1 of the array is not used. For an n-element deap, it occupied n+1 element of an array. If i is a node in the min heap of the deap, its corresponding node in the max heap is i 2 log 2 i 1. Then j defined in property (4) of definition is given by
j i 2 log 2 i 1 ;
if (j > n+1) j /= 2;
5 10 8 25
45 40 j
15
19
i
30
20
4 5 8 25
45 40
15
10
30
20
19
5 10 8 30
45 40
15
19
30
20
25
n)
8 10 9 25
45 40
15
19
20
30