Documente Academic
Documente Profesional
Documente Cultură
So, according to min-max heap property, All nodes are either greater than equal to
(Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. Thus, the
minimum key in a min-max heap is found at the root. The maximum key is the
largest child of the root.
b) Insert new element into the heap at the next available slot ( hole ) hole)
According to maintaining a complete binary tree
Then, percolate the element up the heap while heap-order property not satisfied.
So, the min-max heap property states that the new node should be added at the bot-
tom leaf of the Heap and then perform percolate-up operation. Which goes like;
If inserted element is smaller than its parent node in case of Min-Heap OR
greater than its parent node in case of Max-Heap, swap the element with its
parent.
Keep repeating the above step, if node reaches its correct position, STOP.
c) DeleteMax Algorithm
template<class T>
void maxHeap<T>::pop()
{// Remove max element.
// if heap is empty return null
if (heapSize == 0) // heap empty
throw queueEmpty();
// no
heap[currentNode] = heap[child]; // move child up
currentNode = child; // move down a level
child *= 2;
}
heap[currentNode] = lastElement;
}
procedure DeleteMin
1. MinItemA[root]
2. NewItemlast leaf of the heap
3. Remove the last leaf from the heap
4. Detect the chain C consisting of smallergrandchild(root), smaller
grandchild(smallergrandchild(root))...
5. if the last member is not a leaf then include its child to the chain C
6. Use binary search to find the place m in C, so
that inserting NewItem in position m in C keeps it sorted
7. for all nodes k above position m in the chain, in
increasing order of k A[grandparent(k)] A[k]Update bit-field of parent(grandparent(k))
8. Place NewItem to the position m in C, and update its parents bit-field.
9. return MinItem
end procedure
d) Yes, min-max heap can be built in a linear time, a max-heap of size n can be constructed in
linear time and can be stored in an n-element array; hence it is referred to as an implicit
data structure. When a max-heap implements a priority queue,
FindMax can be performed in constant time, while both DeleteMax and Insert(x) have
logarithmic time.
e) Binomial heap can be a proposed data structure that support deleteMin, deleteMax, and
merge to support all operations in O(log N) time.
7.49
a)
No7.49
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
using namespace std;
int main(){
int num[10];
int temp;
int count;
srand( time(NULL) );
cout<<num[count]<<"|| ";
}
cout<<endl;
medianOfThreeElementsIndex = (3+1)/2;
medianOfFiveElementsIndex = (5+1)/2;
return 0;
}
b)
#include<iostream>
using namespace std;
int main()
{
void qsort (int left, int right)
{
int num, count, pivot, center;
if (left + CUTOFF < right)
{
if ((right - left) > = A)
{
pivot = medianOfSeventeenElements (left,right);
}
else
{
if ((right - left) > = B)
pivot = medianOfNineElements (left,right);
else if ((right - left) > = C)
pivot = medianOfFiveElements (left,right);
else
pivot = medianOfThreeElements (left,right);
}
}
}
return 0;
}