優先順序佇列

2021-07-31 22:16:49 字數 2357 閱讀 4480

#include 

#include

using

namespace

std;

#include

template

struct less

};//模板引數

template

class heap

heap(const t array, size_t size)

int root = (_heap.size() - 2)>>1; //找到最後乙個非葉子節點

for(; root>=0; root--)

}size_t size()const

bool empty()const

void insert(const t& data)

t& top()

void remove()

protected:

void _adjustdown(size_t parent)

if(com(_heap[child], _heap[parent]))

else}}

void _adjustup()

else

return;}}

protected:

std::vector

_heap;

};int main()

; heap h(array, sizeof(array)/sizeof(array[0]));//小堆

heap> hp(array, sizeof(array)/sizeof(array[0]));//大堆

h.insert(8);

hp.insert(100);

h.remove();

hp.remove();

system("pause");

return

0;}

template

struct less

};//模板的模板引數

template

class compare = less>

// templateclass compare定義類模板依賴,呼叫處不用傳型別

class heap

heap(const t array, size_t size)

int root = (_heap.size() - 2)>>1; //找到最後乙個非葉子節點

for(; root>=0; root--)

}size_t size()const

bool empty()const

void insert(const t& data)

t& top()

void remove()

protected:

void _adjustdown(size_t parent)

if(compare()(_heap[child], _heap[parent]))

else}}

void _adjustup()

else

return;}}

protected:

std::vector

_heap;

};int main()

; heap h(array, sizeof(array)/sizeof(array[0]));//小堆

heap hp(array, sizeof(array)/sizeof(array[0]));//大堆

h.insert(8);

hp.insert(100);

h.remove();

hp.remove();

system("pause");

return

0;}

template

class priorityqueue

void push(const t& data)

void pop()

const t& top()const

size_t size()const

bool empty()const

protected:

heap_hp;

};int main()

; heap h(array, sizeof(array)/sizeof(array[0]));

heap hp(array, sizeof(array)/sizeof(array[0]));

h.insert(8);

hp.insert(100);

h.remove();

hp.remove();

system("pause");

return

0;}

佇列 優先順序佇列

優先順序佇列的隊尾是不需要改變的,永遠在低下標處。當佇列增加資料時,隊頭的位置就是資料項的大小減去1.public class priorityq 插入 public void insert long item else quearray j 1 item nitem 刪除 public long ...

優先順序佇列

分為最小優先順序佇列和最大優先順序佇列。優先順序佇列是一種用來維護一組元素構成的集合s的資料結構,這一組元素都有乙個關鍵字key,乙個最大優先順序佇列支援的操作 insert s,x 把x插入到集合s中 maxmum s 返回s中最大元素 extra max s 去掉s中最大關鍵字並返回該最大關鍵子...

優先順序佇列

1 include stdafx.h 2 include3 4 using namespace std 5 6 define max heap len 107 int heap max heap len 8 int heap size 0 the number of elements in heap...