A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
Heaps with a mathematical "greater than or equal to" (≥) comparison predicate are called max-heaps; those with a mathematical "less than or equal to" (≤) comparison predicate are called min-heaps. Min-heaps are often used to implement priority queues.
Since the ordering of siblings in a heap is not specified by the heap property, a single node's two children can be freely interchanged unless doing so violates the shape property (compare with treap). Note, however, that in the common array-based heap, simply swapping the children might also necessitate moving the children's sub-tree nodes to retain the heap property.
The binary heap is a special case of the d-ary heap in which d = 2.
Both the insert and remove operations modify the heap to conform to the shape property first, by adding or removing from the end of the heap. Then the heap property is restored by traversing up or down the heap. Both operations take O(log n) time.
"O father, O father, build me a boat,
That down the Wisconsin I may float,
And every raft that I pass by
There I will inquire for my sweet Pinery Boy."
As she was rowing down the stream
She saw three rafts all in a string.
She hailed the pilot as they pass by
And there she did inquire for her sweet Pinery Boy.
"O pilot, O pilot, tell me true,
Is my sweet Willie among your crew?
Oh, tell me quick and give me joy,
For none other will I have but my sweet Pinery Boy."
"Oh, auburn was the color of his hair,
His eyes were blue and his cheeks were fair.
And his lips were of a ruby fine;
Ten thousand times they've met with mine."
"O dear dear lady, he is not here.
He's drownded in the dells I fear.
'Twas at Lone Rock as we passed by,
Oh, there is where we left your sweet Pinery Boy."
She wrung her hands and tore her hair,
Just like a lady in great despair,
She rowed her boat against Lone Rock