当前位置:网站首页>It supports deleting and updating the priority queue of any node
It supports deleting and updating the priority queue of any node
2022-06-27 23:50: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;
}
边栏推荐
- [从零开始学习FPGA编程-48]:视野篇 - 智能传感器的发展与应用
- [PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
- webService
- [sword finger offer] 48 Longest substring without duplicate characters
- NDSS 2022 接收的列表
- How to find Chinese documents corresponding to foreign documents?
- EXCEL 打印设置公共表头
- Sentinel
- Zero foundation self-study SQL course | case function
- Cornernet由浅入深理解
猜你喜欢

手把手教你移植 tinyriscv 到FPGA上

Working at home is more tiring than going to work at the company?

Feign implements path escape through custom annotations

Zero foundation self-study SQL course | complete collection of SQL basic functions

golang - new和make的区别

Cornernet understands from simple to profound

Zero foundation self-study SQL course | if function
![Technical implementation process of easycvr platform routing log function [code attached]](/img/cc/f4f81cbb72d2d43430a8d15bbbf27b.png)
Technical implementation process of easycvr platform routing log function [code attached]

零基础自学SQL课程 | SQL中的日期函数大全

Windows环境下的ELK——Logstash+Mysql(4)
随机推荐
ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
手把手教你移植 tinyriscv 到FPGA上
Are the registered accounts of the top ten securities companies safe and risky?
支持删除,更新任意结点的优先级队列
Golang uses Mongo driver operation -- Query (array related)
[PCL self study: pclplotter] pclplotter draws data analysis chart
mysql读写分离配置
PAT乙级1013
企业架构师面试的100个问题
一文剖析C语言函数
Swing UI container (I)
Classification of cifar-10 dataset with pytorch
图的存储结构
MSP430F5529 单片机 读取 GY-906 红外温度传感器
解决新版chrome跨域问题:cookie丢失以及samesite属性问题「建议收藏」
[PCL self study: segmentation4] point cloud segmentation based on Min cut
刚开始看英文文献,想问一下各位,最初应该怎么看进去?
Started a natural language model bloom
Grab those duplicate genes
Course strategy sharing plan of Zhejiang University