ACM summary nineteen

Defined heap
heap structure is an array object, it can be considered a complete binary tree. The tree node and the node values stored in the array element that corresponds to each.
Properties of stack
disposed array A of length len, number of nodes of the binary tree size, size≤len, then A [i] stored in the binary tree node number is the value of i (1≤i≤size), while A [ size] after the element does not belong to the corresponding stack, the root of the tree is a [1], and using the properties of complete binary tree, it is easy to find the parent node of the i th node (parent (i)), left child node (left (i)), right child nodes (right (i)) of the subscripts, respectively: i / 2,2i, 2i + 1 ;
other than the root node for each i, a [parent (i)] ≥A [i] . This reactor is also known as "large root heap"; otherwise, other than the root node for each i, A [parent (i) ] ≤A [i] of the stack, referred to as "rootlets heap."
Heap operations
put operation



void put(int d)

{

       heap[++heap_size] = d;

       //push_heap(heap + 1, heap + heap_size +1);            //大根堆

       push_heap(heap + 1, heap + heap_size + 1,greater<int>()); //小根堆

}


get operation



int get()

{

       //pop_heap(heap + 1, heap + heap_size +1);               //大根堆

       pop_heap(heap + 1, heap + heap_size + 1,greater<int>());    //小根堆

       return heap[heap_size--];

}


More than two codes are required header files

Guess you like

Origin blog.csdn.net/qq_43515378/article/details/90247027