Sunteți pe pagina 1din 5

Data Structures TAkeHome Test

a) Heap-order property (for a MinHeap)


For every node X key(parent(X)) , key(parent(X)) key(X)
Except root node, which has no parent
Thus minimum key always at root
Alternatively, for a MaxHeap, always keep the maximum key at the root
Insert and deleteMin must maintain heap-order property.

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.

Here follows an algorithm:


void insert(const Comparable & x)
{
if (currentSize == array.size() - 1)
{
array.resize(array.size() * 2);
//percolate up
int hole = ++currentSize;
for( ;hole > 1 && x < array[hole / 2]; hole / = 2)
{
array[hole] = array[hole / 2];
array[hole] = x;
}
}
}

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();

// Delete max element


heap[1].~T();
// Remove last element and reheapify
T lastElement = heap[heapSize--];

// find place for lastElement starting at root


int currentNode = 1,
child = 2; // child of currentNode
while (child <= heapSize)
{
// heap[child] should be larger child of currentNode
if (child < heapSize && heap[child] < heap[child + 1])
child++;

// can we put lastElement in heap[currentNode]?


if (lastElement >= heap[child])
break; // yes

// 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<<"Enter TEN Numbers"<<endl;


for(int i=0;i<10;i++){
cin>>num[i];
}

cout<<endl<<"Data before sorting"<<endl;


for(count=0;count<10;count++){

cout<<num[count]<<"|| ";
}
cout<<endl;

for(int i=0; i <= 10; i++){

for(count=0;count <=10-i; count++){


//here all numbers are being sorted
if(num[count] > num[count+1]){
temp = num[count];
num[count] = num[count+1];
num[count+1] = temp;
}
}
}
cout<<endl;
cout<<"Numbers after sorting"<<endl;
for(count=1;count<=10; count++)
{
cout<<num[count]<<"|| ";
}

int middleArrayIndex = (10)/2;


int randomIndex = rand() % count;
int medianOfThreeElementsIndex;
int medianOfFiveElementsIndex;

medianOfThreeElementsIndex = (3+1)/2;
medianOfFiveElementsIndex = (5+1)/2;

//median of three elements


cout<<endl<<endl;
cout<<"First Element\t"<<"|"<<"Middle Element\t"<<"|" <<"Random Element\t"<<"|" <<"
Median Of Three"<<"|"<<"Median Of Five"<<endl;
cout<<"\t"<<num[count-10] <<"\t"<<"||" <<"\t" <<num[middleArrayIndex] <<"\t"<<"||"
<<"\t" <<num[randomIndex] <<"\t"<<"||" <<"\t" <<num[medianOfThreeElementsIndex]
<<"\t"<<"||" <<"\t" <<num[medianOfFiveElementsIndex]<<endl;

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;
}

S-ar putea să vă placă și