当前位置:网站首页>支持删除,更新任意结点的优先级队列
支持删除,更新任意结点的优先级队列
2022-06-27 20:51:00 【CaspianSea】
class HashMap
{
public:
void init();
void push(int key, int value);
int get(int key);
struct dataItem
{
int key;
int value;
};
struct hashItem
{
int bufIdx;
int next;
};
private:
static const int size = 1000;
dataItem buffer[size];
hashItem hashBuf[size];
hashItem hashTbl[1009];
int bufIdx;
int hashBufIdx;
};
void HashMap::init(void)
{
hashBufIdx = 0;
bufIdx = 0;
for(int i = 0; i < 1009; ++i)
{
hashTbl[i].next = -1;
}
}
void HashMap::push(int key, int value)
{
int hash = key %1009;
int iter = hashTbl[hash].next;
while(iter != -1)
{
int bufIdx = hashBuf[iter].bufIdx;
if( buffer[bufIdx].key == key)
{
buffer[bufIdx].value = value;
return;
}
iter = hashBuf[iter].next;
}
int i = bufIdx++;
buffer[i].key = key;
buffer[i].value = value;
int j = hashBufIdx++;
hashBuf[j].bufIdx = i;
hashBuf[j].next = hashTbl[hash].next;
hashTbl[hash].next = j;
}
int HashMap::get(int key)
{
int hash = key%1009;
int iter = hashTbl[hash].next;
while(iter != -1 )
{
dataItem value = buffer[ hashBuf[iter].bufIdx ];
if(value.key == key)
{
return value.value;
}
iter = hashBuf[iter].next;
}
return -1;
}
class CPrioTree
{
public:
void init(void);
void push(int data);
int pop(void);
void update(int idx, int data);
void del(int idx);
int getIndex(int data);
int top();
bool empty() { return size == 0;}
protected:
virtual bool compare(int a, int b) = 0;
private:
void downward(int idx, int value);
void upward(int idx, int value);
static const int capacity = 1000;
int queue[capacity];
int size;
HashMap hashMap;
};
void CPrioTree::init(void)
{
size = 0;
hashMap.init();
}
void CPrioTree::downward(int idx, int value)
{
int i = idx;
while( 2 * i + 1 < size)
{
int a = 2 * i + 1;
int b = a + 1;
if( b < size && compare(queue[b], queue[a]))
{
a = b;
}
if( compare(value, queue[a]) )
{
break;
}
queue[i] = queue[a];
hashMap.push(queue[a], i);
i = a;
}
queue[i] = value;
hashMap.push(value, i);
}
void CPrioTree::upward(int idx, int value)
{
int i = idx;
while( i > 0)
{
int parentIdx = (i-1)/2;
int parent = queue[parentIdx];
if( compare(parent, value))
{
break;
}
queue[i] = parent;
hashMap.push(parent, i);
i = parentIdx;
}
queue[i] = value;
hashMap.push(value, i);
}
void CPrioTree::push(int data)
{
int i =size++;
upward(i, data);
}
int CPrioTree::pop(void)
{
int ret = queue[0];
int tmp = queue[--size];
downward(0, tmp);
return ret;
}
void CPrioTree::update(int idx, int newValue)
{
queue[idx] = newValue;
bool downFlag = false;
if( idx == 0)
{
downFlag = true;
}
if(!downFlag)
{
int parentIdx = (idx -1)/2;
int parent = queue[parentIdx];
if(compare(parent, newValue))
{
downFlag = true;
}
}
if(downFlag)
{
downward(idx, newValue);
}
else
{
upward(idx, newValue);
}
}
void CPrioTree::del(int idx)
{
int tmp = queue[--size];
bool downFlag = false;
if( idx == 0)
{
downFlag = true;
}
if(!downFlag)
{
int parentIdx = (idx-1)/2;
int parent = queue[parentIdx];
if(compare(parent, tmp))
{
downFlag = true;
}
}
if(downFlag)
{
downward(idx, tmp);
}
else
{
upward(idx, tmp);
}
}
int CPrioTree::getIndex(int data)
{
return hashMap.get(data);
}
int CPrioTree::top()
{
return queue[0];
}
class CMaxPrioTree : public CPrioTree
{
protected:
bool compare(int a, int b);
};
bool CMaxPrioTree::compare(int a, int b)
{
return a > b;
}
class CMinPrioTree : public CPrioTree
{
protected:
bool compare(int a, int b);
};
bool CMinPrioTree::compare(int a, int b)
{
return a < b;
}
边栏推荐
- 打造南沙“强芯”,南沙首届IC Nansha大会召开
- [js]var, let, const
- How to use RPA to achieve automatic customer acquisition?
- The choice and trade-off between vector recall and literal recall
- Spatial relation query and graph based query in secondary development of ArcGIS Engine
- Feign implements path escape through custom annotations
- Getting started with pytorch
- Design of STM32 and rc522 simple bus card system
- 跟着存档教程动手学RNAseq分析(一)
- 【剑指Offer】47. 礼物的最大价值
猜你喜欢

2022年第一季度“广州好人”刘磊峰:具有强烈的诚信意识和食品安全意识

C# Winform 读取Resources图片

未能加载文件或程序集“CefSharp.Core.Runtime.dll”或它的某一个依赖项。 不是有效的 Win32 应用程序。 (异常来自 HRESULT:0x800700C1)

Netease cloud lost its "feelings" card

在线JSON转PlainText工具

MapReduce初级编程实践

Is the dog virtue training with a monthly salary of 30000 a good business?

Azure Kinect DK realizes 3D reconstruction (PC non real time version)

seata

This year's examinees are more "desperate" than the college entrance examination
随机推荐
Spark BUG实践(包含的BUG:ClassCastException;ConnectException;NoClassDefFoundError;RuntimeExceptio等。。。。)
vivado VIO IP的用法
Follow the archiving tutorial to learn rnaseq analysis (III): count standardization using deseq2
[electron] basic learning
Design of STM32 and rc522 simple bus card system
How to set the enterprise wechat group robots to send messages regularly?
第一性原理(最优解理论)
向量召回和字面召回的选择与权衡
Web Worker介绍及使用案例
Using the cucumber automated test framework
Is it safe to use flush mobile phones to speculate in stocks?
云辅助隐私集合求交(Server-Aided PSI)协议介绍:学习
跟着存档教程动手学RNAseq分析(二)
First principles (optimal solution theory)
Zabbix6.0 upgrade Guide - how to synchronize database upgrades?
小程序referer
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
Classification of cifar-10 dataset with pytorch
pytorch实现kaggle猫狗识别
[can you really use es] Introduction to es Basics (II)