当前位置:网站首页>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;
}
边栏推荐
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- 【数字IC/FPGA】检测最后一个匹配序列的位置
- 【微服务|Sentinel】sentinel数据持久化
- How vivado adds timing constraints
- go日志包 log的使用
- One step forward is excellent, one step backward is ignorant
- ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code
- 一文剖析C语言函数
- Super outline exercises
- seata
猜你喜欢

Elk in Windows Environment - logstash+mysql (4)

VMware virtual machine bridging connectivity

小芯片chiplet技术杂谈

EXCEL 打印设置公共表头

发射,接收天线方向图

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

【数字IC/FPGA】检测最后一个匹配序列的位置
![[PCL self study: pclplotter] pclplotter draws data analysis chart](/img/ca/db68d5fae392c7976bfc93d2107509.png)
[PCL self study: pclplotter] pclplotter draws data analysis chart

Cornernet由浅入深理解

Golang - the difference between new and make
随机推荐
ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
【Vim】使用教程,常用命令,高效使用Vim编辑器
Introduction to quantitative trading
搭建开源美观的数据库监控系统-Lepus
零基础自学SQL课程 | CASE函数
[PCL self study: segmentation4] point cloud segmentation based on Min cut
Golang uses Mongo driver operation - query (basic)
【微服务|Sentinel】sentinel数据持久化
Feign implements path escape through custom annotations
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
Build an open source and beautiful database monitoring system -lepus
c语言之字符串数组
mysql读写分离配置
良/恶性乳腺肿瘤预测(逻辑回归分类器)
解决新版chrome跨域问题:cookie丢失以及samesite属性问题「建议收藏」
Sentinel
How to use the apipost script - global variables
Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
Detect objects and transfer images through mqtt