Binary Heap
Binomial heap
DATA STRUCTURES AND ALGORITHMS
LECTURE 7
Lect. PhD. Oneţ-Marian Zsuzsanna
Babeş - Bolyai University
Computer Science and Mathematics Faculty
2018 - 2019
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
In Lecture 6...
Linked Lists on Arrays
Stack, Queue, Deque
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Today
1 Binary Heap
2 Binomial heap
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
ADT Priority Queue
The ADT Priority Queue is a container in which each element
has an associated priority (of type TPriority ).
In a Priority Queue access to the elements is restricted: we
can access only the element with the highest priority.
Because of this restricted access, we say that the Priority
Queue works based on a HPF - Highest Priority First policy.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Representation
What data structures can be used to implement a priority
queue?
Dynamic Array
Linked List
(Binary) Heap - will be discussed later
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Representation
If the representation is a Dynamic Array or a Linked List we
have to decide how we store the elements in the array/list:
we can keep the elements ordered by their priorities
Where would you put the element with the highest priority?
we can keep the elements in the order in which they were
inserted
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Representation
Complexity of the main operations for the two representation
options:
Operation Sorted Non-sorted
push O(n) Θ(1)
pop Θ(1) Θ(n)
top Θ(1) Θ(n)
What happens if we keep in a separate field the element with
the highest priority?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap
A binary heap is a data structure that can be used as an
efficient representation for Priority Queues.
A binary heap is a kind of hybrid between a dynamic array and
a binary tree.
The elements of the heap are actually stored in the dynamic
array, but the array is visualized as a binary tree.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap
Assume that we have the following array (upper row contains
positions, lower row contains elements):
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap
We can visualize this array as a binary tree, where the root is
the first element of the array, its children are the next two
elements, and so on. Each node has exactly 2 children, except
for the last two rows, but there the children of the nodes are
completed from left to right.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap
If the elements of the array are: a1 , a2 , a3 , ..., an , we know
that:
a1 is the root of the heap
for an element from position i, its children are on positions 2 ∗ i
and 2 ∗ i + 1 (if 2 ∗ i and 2 ∗ i + 1 is less than or equal to n)
for an element from position i (i > 1), the parent of the
element is on position [i/2] (integer part of i/2)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap
A binary heap is an array that can be visualized as a binary
tree having a heap structure and a heap property.
Heap structure: in the binary tree every node has exactly 2
children, except for the last two levels, where children are
completed from left to right.
Heap property: ai ≥ a2∗i (if 2 ∗ i ≤ n) and ai ≥ a2∗i+1 (if
2 ∗ i + 1 ≤ n)
The ≥ relation between a node and both its descendants can
be generalized (other relations can be used as well).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples I
Are the following binary trees heaps? If yes, specify the
relation between a node and its children. If not, specify if the
problem is with the structure, the property, or both.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples II
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples III
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples IV
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples V
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples VI
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples VII
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Examples VIII
Are the following arrays valid heaps? If not, transform them
into a valid heap by swapping two elements.
1 [70, 10, 50, 7, 1, 33, 3, 8]
2 [1, 2, 4, 8, 16, 32, 64, 65, 10]
3 [10, 12, 100, 60, 13, 102, 101, 80, 90, 14, 15, 16]
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Notes
If we use the ≥ relation, we will have a MAX-HEAP. Do you
know why?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Notes
If we use the ≥ relation, we will have a MAX-HEAP. Do you
know why?
If we use the ≤ relation, we will have a MIN-HEAP. Do you
know why?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Notes
If we use the ≥ relation, we will have a MAX-HEAP. Do you
know why?
If we use the ≤ relation, we will have a MIN-HEAP. Do you
know why?
The height of a heap with n elements is log2 n.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - operations
A heap can be used as representation for a Priority Queue and
it has two specific operations:
add a new element in the heap (in such a way that we keep
both the heap structure and the heap property).
remove (we always remove the root of the heap - no other
element can be removed).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - representation
Heap:
cap: Integer
len: Integer
elems: TElem[]
For the implementation we will assume that we have a
MAX-HEAP.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Add - example
Consider the following (MAX) heap:
Let’s add the number 55 to the heap.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Add - example
In order to keep the heap structure, we will add the new node
as the right child of the node 14 (and as the last element of
the array in which the elements are kept).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Add - example
Heap property is not kept: 14 has as child node 55 (since it is
a MAX-heap, each node has to be greater than or equal to its
descendants).
In order to restore the heap property, we will start a bubble-up
process: we will keep swapping the value of the new node
with the value of its parent node, until it gets to its final
place. No other node from the heap is changed.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Add - example
When bubble-up ends:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - add
subalgorithm add(heap, e) is:
//heap - a heap
//e - the element to be added
if heap.len = heap.cap then
@ resize
end-if
heap.elems[heap.len+1] ← e
heap.len ← heap.len + 1
bubble-up(heap, heap.len)
end-subalgorithm
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - add
subalgorithm bubble-up (heap, p) is:
//heap - a heap
//p - position from which we bubble the new node up
poz ← p
elem ← heap.elems[p]
parent ← p / 2
while poz > 1 and elem > heap.elems[parent] execute
//move parent down
heap.elems[poz] ← heap.elems[parent]
poz ← parent
parent ← poz / 2
end-while
heap.elems[poz] ← elem
end-subalgorithm
Complexity:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - add
subalgorithm bubble-up (heap, p) is:
//heap - a heap
//p - position from which we bubble the new node up
poz ← p
elem ← heap.elems[p]
parent ← p / 2
while poz > 1 and elem > heap.elems[parent] execute
//move parent down
heap.elems[poz] ← heap.elems[parent]
poz ← parent
parent ← poz / 2
end-while
heap.elems[poz] ← elem
end-subalgorithm
Complexity: O(log2 n)
Can you give an example when the complexity of the
algorithm is less than log2 n (best case scenario)?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Remove - example
From a heap we can only remove the root element.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Remove - example
In order to keep the heap structure, when we remove the root,
we are going to move the last element from the array to be
the root.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Remove - example
Heap property is not kept: the root is no longer the maximum
element.
In order to restore the heap property, we will start a
bubble-down process, where the new node will be swapped
with its maximum child, until it becomes a leaf, or until it will
be greater than both children.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - Remove - example
When the bubble-down process ends:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - remove
function remove(heap) is:
//heap - is a heap
if heap.len = 0 then
@ error - empty heap
end-if
deletedElem ← heap.elems[1]
heap.elems[1] ← heap.elems[heap.len]
heap.len ← heap.len - 1
bubble-down(heap, 1)
remove ← deletedElem
end-function
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - remove
subalgorithm bubble-down(heap, p) is:
//heap - is a heap
//p - position from which we move down the element
poz ← p
elem ← heap.elems[p]
while poz < heap.len execute
maxChild ← -1
if poz * 2 ≤ heap.len then
//it has a left child, assume it is the maximum
maxChild ← poz*2
end-if
if poz*2+1 ≤ heap.len and heap.elems[2*poz+1]>heap.elems[2*poz] th
//it has two children and the right is greater
maxChild ← poz*2 + 1
end-if
//continued on the next slide...
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - remove
if maxChild 6= -1 and heap.elems[maxChild] > elem then
tmp ← heap.elems[poz]
heap.elems[poz] ← heap.elems[maxChild]
heap.elems[maxChild] ← tmp
poz ← maxChild
else
poz ← heap.len + 1
//to stop the while loop
end-if
end-while
end-subalgorithm
Complexity:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binary Heap - remove
if maxChild 6= -1 and heap.elems[maxChild] > elem then
tmp ← heap.elems[poz]
heap.elems[poz] ← heap.elems[maxChild]
heap.elems[maxChild] ← tmp
poz ← maxChild
else
poz ← heap.len + 1
//to stop the while loop
end-if
end-while
end-subalgorithm
Complexity: O(log2 n)
Can you give an example when the complexity of the
algorithm is less than log2 n (best case scenario)?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Questions
In a max-heap where can we find the:
maximum element of the array?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Questions
In a max-heap where can we find the:
maximum element of the array?
minimum element of the array?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Questions
In a max-heap where can we find the:
maximum element of the array?
minimum element of the array?
Assume you have a MAX-HEAP and you need to add an
operation that returns the minimum element of the heap.
How would you implements this operation, using constant
time and space? (Note: we only want to return the minimum,
we do not want to be able to remove it).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Exercises
Consider an initially empty Binary MAX-HEAP and insert the
elements 8, 27, 13, 15*, 32, 20, 12, 50*, 29, 11* in it. Draw
the heap in the tree form after the insertion of the elements
marked with a * (3 drawings). Remove 3 elements from the
heap and draw the tree form after every removal (3 drawings).
Insert the following elements, in this order, into an initially
empty MIN-HEAP: 15, 17, 9, 11, 5, 19, 7. Remove all the
elements, one by one, in order from the resulting MIN HEAP.
Draw the heap after every second operation (after adding 17,
11, 19, etc.)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort
There is a sorting algorithm, called Heap-sort, that is based
on the use of a heap.
In the following we are going to assume that we want to sort
a sequence in ascending order.
Let’s sort the following sequence: [6, 1, 3, 9, 11, 4, 2, 5]
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Naive approach
Based on what we know so far, we can guess how heap-sort
works:
Build a min-heap adding elements one-by-one to it.
Start removing elements from the min-heap: they will be
removed in the sorted order.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Naive approach
The heap when all the elements were added:
When we remove the elements one-by-one we will have: 1, 2,
3, 4, 5, 6, 9, 11.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Naive approach
What is the time complexity of the heap-sort algorithm
described above?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Naive approach
What is the time complexity of the heap-sort algorithm
described above?
The time complexity of the algorithm is O(nlog2 n)
What is the extra space complexity of the heap-sort algorithm
described above (do we need an extra array)?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Naive approach
What is the time complexity of the heap-sort algorithm
described above?
The time complexity of the algorithm is O(nlog2 n)
What is the extra space complexity of the heap-sort algorithm
described above (do we need an extra array)?
The extra space complexity of the algorithm is Θ(n) - we need
an extra array.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Better approach
If instead of building a min-heap, we build a max-heap (even
if we want to do ascending sorting), we do not need the extra
array.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Better approach
We can improve the time complexity of building the heap as
well.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Heap-sort - Better approach
We can improve the time complexity of building the heap as
well.
If we have an unsorted array, we can transform it easier into a
heap: the second half of the array will contain leaves, they can
be left where they are.
Starting from the first non-leaf element (and going towards the
beginning of the array), we will just call bubble-down for every
element.
Time complexity of this approach: O(n) (but removing the
elements from the heap is still O(nlog2 n))
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Representation on a binary heap
When an element is pushed to the priority queue, it is simply
added to the heap (and bubbled-up if needed)
When an element is popped from the priority queue, the root
is removed from the heap (and bubble-down is performed if
needed)
Top simply returns the root of the heap.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Representation
Let’s complete our table with the complexity of the operations
if we use a heap as representation:
Operation Sorted Non-sorted Heap
push O(n) Θ(1) O(log2 n)
pop Θ(1) Θ(n) O(log2 n)
top Θ(1) Θ(n) Θ(1)
Consider the total complexity of the following sequence of
operations:
start with an empty priority queue
push n random elements to the priority queue
perform pop n times
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Application
Print all numbers of the form a3 + b 3 in ascending order
where 0 ≤ a ≤ N and 0 ≤ b ≤ N.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Application
Print all numbers of the form a3 + b 3 in ascending order
where 0 ≤ a ≤ N and 0 ≤ b ≤ N.
Option 1: Generate all possible numbers, sort them and print
the sorted array
We can use less memory if we use the fact that a3 + b 3 is
always less than (a + 1)3 + b 3 and a3 + (b + 1)3 . But if we
push those two numbers when popping a3 + b 3 , we will have
many duplicates in the priority queue (a3 + b 3 will be pushed
both for (a − 1)3 + b 3 and a3 + (b − 1)3 ).
Generate all numbers of the form 03 + i 3 , and add them in a
priority queue. When a number is popped, we push
(a + 1)3 + b 3 .
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Extension
We have discussed the standard interface of a Priority Queue,
the one that contains the following operations:
push
pop
top
isEmpty
init
Sometimes, depending on the problem to be solved, it can be
useful to have the following three operations as well:
increase the priority of an existing element
delete an arbitrary element
merge two priority queues
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Extension
What is the complexity of these three extra operations if we
use as representation a binary heap?
Increasing the priority of an existing element is O(log2 n) if we
know the position where the element is.
Deleting an arbitrary element is O(log2 n) if we know the
position where the element is.
Merging two priority queues has complexity Θ(n) (assume
both priority queues have n elements).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Priority Queue - Other representations
If we do not want to merge priority queues, a binary heap is a
good representation. If we need the merge operation, there
are other heap data structures that can be used, which offer a
better complexity.
Out of these data structures we are going to discuss one: the
binomial heap.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap
A binomial heap is a collection of binomial trees.
A binomial tree can be defined in a recursive manner:
A binomial tree of order 0 is a single node.
A binomial tree of order k is a tree which has a root and k
children, each being the root of a binomial tree of order k − 1,
k − 2, ..., 2, 1, 0 (in this order).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - Example
Binomial trees of order 0, 1, 2 and 3
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree
A binomial tree of order k has exactly 2k nodes.
The height of a binomial tree of order k is k.
If we delete the root of a binomial tree of order k, we will get
k binomial trees, of orders k − 1, k − 2, ..., 2, 1, 0.
Two binomial trees of the same order k can be merged into a
binomial tree of order k + 1 by setting one of them to be the
leftmost child of the other.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - Merge I
Before merge we have two binomial trees of order 2
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - Merge II
One way of merging the two binomial trees into one of order 3
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - Merge III
Another way of merging the two binomial trees into one of order 3
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - representation
If we want to implement a binomial tree, we can use the
following representation:
We need a structure for nodes, and for each node we keep the
following:
The information from the node
The address of the parent node
The address of the first child node
The address of the next sibling node
For the tree we will keep the address of the root node (and
probably the order of the tree)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap
A binomial heap is made of a collection/sequence of binomial
trees with the following property:
Each binomial tree respects the heap-property: for every node,
the value from the node is less than the value of its children
(assume MIN HEAPS).
There can be at most one binomial tree of a given order k.
As representation, a binomial heap is usually a sorted linked
list, where each node contains a binomial tree, and the list is
sorted by the order of the trees.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree - Example
Binomial heap with 14 nodes, made of 3 binomial trees of orders 1, 2 and 3
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial tree
For a given number of elements, n, the structure of a binomial
heap (i.e. the number of binomial trees and their orders) is
unique.
The structure of the binomial heap is determined by the
binary representation of the number n.
For example 14 = 1110 (in binary) = 23 + 22 + 21 , so a
binomial heap with 14 nodes contains binomial trees of orders
3, 2, 1 (but they are stored in the reverse order: 1, 2, 3).
For example 21 = 10101 = 24 + 22 + 20 , so a binomial heap
with 21 nodes contains binomial trees of orders 4, 2, 0.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap
A binomial heap with n elements contains at most log2 n
binomial trees.
The height of the binomial heap is at most log2 n.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - merge
The most interesting operation for two binomial heaps is the
merge operation, which is used by other operations as well.
After the merge operation the two previous binomial heaps
will no longer exist, we will only have the result.
Since both binomial heaps are sorted linked lists, the first step
is to merge the two linked lists (standard merge algorithm for
two sorted linked lists).
The result of the merge can contain two binomial trees of the
same order, so we have to iterate over the resulting list and
transform binomial trees of the same order k into a binomial
tree of order k + 1. When we merge the two binomial trees we
must keep the heap property.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - merge - example I
Let’s merge the following two binomial heaps:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - merge - example II
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - merge - example III
After merging the two linked lists of binomial trees:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - merge - example IV
After transforming the trees of the same order (final result of
the merge operation).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - Merge operation
If both binomial heaps have n elements, merging them will
have O(log2 n) complexity (the maximum number of binomial
trees for a binomial heap with n elements is log2 n).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations I
Most of the other operations that we have for the binomial
heap (because we need them for the priority queue) will use
the merge operation presented above.
Push operation: Inserting a new element means creating a
binomial heap with just that element and merging it with the
existing one. Complexity of insert is O(log2 n) in worst case
(Θ(1) amortized).
Top operation: The minimum element of a binomial heap (the
element with the highest priority) is the root of one of the
binomial trees. Returning the minimum means checking every
root, so it has complexity O(log2 n).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations II
Pop operation: Removing the minimum element means
removing the root of one of the binomial trees. If we delete
the root of a binomial tree, we will get a sequence of binomial
trees. These trees are transformed into a binomial heap (just
reverse their order), and a merge is performed between this
new binomial heap and the one formed by the remaining
elements of the original binomial heap.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations III
The minimum is one of the roots.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations IV
Break the corresponding tree into k binomial trees
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations V
Create a binomial heap of these trees
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations VI
Merge it with the existing one (after the merge algorithm)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations VII
After the transformation
The complexity of the remove-minimum operation is O(log2 n)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Binomial heap - other operations VIII
Assuming that we have a pointer to the element whose
priority has to be increased (in our figures lower number
means higher priority), we can just change the priority and
bubble-up the node if its priority is greater than the priority of
its parent. Complexity of the operation is: O(log2 n)
Assuming that we have a pointer to the element that we want
to delete, we can first decrease its priority to −∞ (this will
move it to the root of the corresponding binomial tree) and
remove it. Complexity of the operation is: O(log2 n)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Problems with stacks, queues and priority queues I
Red-Black Card Game:
Statement: Two players each receive n2 cards, where each card
can be red or black. The two players take turns; at every turn
the current player puts the card from the upper part of his/her
deck on the table. If a player puts a red card on the table, the
other player has to take all cards from the table and place
them at the bottom of his/her deck. The winner is the player
that has all the cards.
Requirement: Given the number n of cards, simulate the game
and determine the winner.
Hint: use stack(s) and queue(s)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Problems with stacks, queues and priority queues II
Robot in a maze:
Statement: There is a rectangular maze, composed of
occupied cells (X) and free cells (*). There is a robot (R) in
this maze and it can move in 4 directions: N, S, E, V.
Requirements:
Check whether the robot can get out of the maze (get to the
first or last line or the first or last column).
Find a path that will take the robot out of the maze (if exists).
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Problems with stacks, queues and priority queues III
Hint:
Let T be the set of positions where the robot can get from
the starting position.
Let s be the set of positions to which the robot can get at a
given moment and from which it could continue going to
other positions.
A possible way of determining the sets T and S could be the
following:
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Problems with stacks, queues and priority queues IV
T ← {initial position}
S ← {initial position}
while S 6= ∅ execute
Let p be one element of S
S ← S \{p}
for each valid position q where we can get from p and which is not in T do
T ← T ∪ {q}
S ← S ∪ {q}
end-for
end-while
T can be a list, a vector or a matrix associated to the maze
S can be a stack or a queue (or even a priority queue,
depending on what we want)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Think about it - Linked Lists
Write a non-recursive algorithm to reverse a singly linked list
with Θ(n) time complexity, using constant space/memory.
Suppose there are two singly linked lists both of which
intersect at some point and become a single linked list (see
the image below). The number of nodes in the two list before
the intersection is not known and may be different in each list.
Give an algorithm for finding the merging point (hint - use a
Stack)
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Think about it - Stacks and Queues I
How can we implement a Stack using two Queues? What will
be the complexity of the operations?
How can we implement a Queue using two Stacks? What will
be the complexity of the operation?
How can we implement two Stacks using only one array? The
stack operations should throw an exception only if the total
number of elements in the two Stacks is equal to the size of
the array.
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Think about it - Stacks and Queues II
Given a string of lower-case characters, recursively remove
adjacent duplicate characters from the string. For example,
for the word ”mississippi” the result should be ”m”.
Given an integer k and a queue of integer numbers, how can
we reverse the order of the first k elements from the queue?
For example, if k=4 and the queue has the elements [10, 20,
30, 40, 50, 60, 70, 80, 90], the output should be [40, 30, 20,
10, 50, 60, 70, 80, 90].
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS
Binary Heap
Binomial heap
Think about it - Priority Queues
How can we implement a stack using a Priority Queue?
How can we implement a queue using a Priority Queue?
Lect. PhD. Oneţ-Marian Zsuzsanna DATA STRUCTURES AND ALGORITHMS