Lecture07 PDF
Lecture07 PDF
Binomial heap
2018 - 2019
In Lecture 6...
Today
1 Binary Heap
2 Binomial heap
Dynamic Array
Linked List
Where would you put the element with the highest priority?
Binary Heap
Binary Heap
Binary Heap
Binary Heap
Binary Heap
Heap:
cap: Integer
len: Integer
elems: TElem[]
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).
Complexity:
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
Complexity:
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
Questions
Questions
Exercises
Heap-sort
Starting from the first non-leaf element (and going towards the
beginning of the array), we will just call bubble-down for every
element.
Binomial heap
Binomial tree
One way of merging the two binomial trees into one of order 3
Another way of merging the two binomial trees into one of order 3
For the tree we will keep the address of the root node (and
probably the order of the tree)
Binomial heap
Binomial tree
Binomial heap
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.
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:
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
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.