Skip to main content

Priority Queue implementation using MaxHeap

The Priority Queue with a MaxRemove() interface can be implemented using a MaxHeap which is a complete binary tree that maintains the maximum (here it would be the maximum of the priority values) of the elements that it contains at its root.

The Code is super simple and is as follows:

[code language="cpp"]

PriorityQueue(int CapacityParam)
{
Data = (int*)malloc(CapacityParam*sizeof(int));
ToH = -1;
Capacity = CapacityParam;
}

void Display()
{
cout << "\nList:\n";
for (int iter = 0; iter <= ToH; iter++)
cout << " " << Data[iter];
}

void HeapifyUp(int Position)
{
if (Position < 1) return;
if (Data[Position]>Data[(Position - 1) / 2])
{
Swap(&(Data[Position]), &(Data[(Position - 1) / 2]));
HeapifyUp((Position - 1) / 2);
}
}

bool Insert(int ParamValue)
{
if (ToH >= Capacity - 1) return false; //QueueOverflow
Data[++ToH] = ParamValue;
HeapifyUp(ToH);
return true;
}

void HeapifyDown(int Position)
{
if (2 * Position + 1 > ToH) return;
if (2 * Position + 2 <= ToH)
{
//LR exists
if (Data[2 * Position + 1] > Data[2 * Position + 2])
{
//L
if (Data[2 * Position + 1] > Data[Position]) Swap(&(Data[2 * Position + 1]) ,& (Data[Position]));
HeapifyDown(2 * Position + 1);
}
else
{
//R
if (Data[2 * Position + 2] > Data[Position]) Swap(&(Data[2*Position+2]), &(Data[Position]));
HeapifyDown(2 * Position + 2);
}
}
else
{
//Only L exists
//L
if (Data[2 * Position + 1] > Data[Position]) Swap(&(Data[2 * Position + 1]), &(Data[Position]));
HeapifyDown(2 * Position + 1);
}
}

void SortDump()
{
int SortToH = ToH;
while (ToH >= 0)
{
Swap(&(Data[0]), &(Data[ToH]));
--ToH;
HeapifyDown(0);
}
ToH = SortToH;
Display();
}
};

void FillArrayWithRand(int *Array, int Size)
{
if (!Array || Size<=0) return;
for (int i = 0; i < Size; i++)
Array[i] = (rand() % 10);
}
int main()
{
//Dont allow 0 in the tree for CommonAncestor problem

const int DataSetSize = 9999;
int *RandArray = (int*)malloc(DataSetSize * sizeof(int));
FillArrayWithRand(RandArray, DataSetSize);
cout << "\nRandom Array:\n";
for (int i = 0; i < DataSetSize; i++)
cout<<" "<<RandArray[i];

PriorityQueue test(DataSetSize);
for (int i = 0; i < DataSetSize; i++)
test.Insert( RandArray[i]);
test.Display();
test.SortDump();

cout << "\n\nEnd of code";
system("pause");

return 0;
}

&nbsp;

[/code]

Comments

Popular posts from this blog

GraphQL

GraphQL



What is GraphQL

It is a specification laid out by Facebook which proposed an alternative way to query and modify data. Think of it is an as a complimentary of REST/RPC.

Now head here and read the original graphQL documentation. It will take 2-3 hours tops but is a worthy read. This will help you build some impressions on it and help contrast against mine below:


Why use GraphQL

Core advantage

Instead of defining custom backend rpc/rest endpoints for every data-shape, graphql allows you to build a more general endpoint which give frontend/mobile engineers freedom and query and play with the data. It might be less efficient, add a bit more complexity (need for things like data-loader), harder to standardize and control client-contracts for. What it looses in complexity and control, it gains in flexibility and freedom - provided your data model is worth of a graphql-ish query How to tell if my data-model graphql-ish?Are there complex relationships between your models? Is there a need …

About me

Hi,

I'm currently working as a software developer in the logistics industry in San Francisco. 
My aim is to impact billions of people's live for the better and make the world a better place.

Cheers,
Vignesh



Backend - Tech refresher 2019

Hello there

As a software engineer, it is important to keep updating your skillsets by learning the latest programming-elements (includes paradigms, patterns, languages, tools and frameworks). This becomes a bit easy if you already working on the cutting edge of something. Even then, it is possible to go too deep and loose breadth.

I've taken upon myself to do a tech refresher every year. The intent is to read, experiment and understand these elements by spending anywhere between 4 days to 4 weeks. The ultimate goal is: "do I know most that I need to know to build a planet-scale backend tech-stack ground up"

I'll write up my learnings in posts to help myself (and maybe others) refer it.

Here is the initial list I'm thinking about:


RedisMySQL, GraphQLAurora, Mesos, KubernetesCadence, SWSCassandra, MangoDB, NoSQL, MySQL, Spanner, S<, DynDBELKFlink, Storm, Samza, SparkHadoop HDFS, Yarn, MapReduceHive, HBaseKafka, ZookeeperNW: http, Tcp, thrift, grpc, yarpc, Terraform,…