五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

【從堆的定義到優(yōu)先隊列、堆排序】 10分鐘看懂必考的數(shù)據(jù)結(jié)構(gòu)——堆

2023-08-03 22:01 作者:景峰小白豬  | 我要投稿

根據(jù)UP的視頻使用C++實現(xiàn)堆,不知道是否是不是最優(yōu),反正結(jié)果沒問題??

//上浮,從最后一個位置開始調(diào)整堆O(nlogn)

void shiftUp(vector<int> &as){

??size_t child = as.size() - 1;

??size_t parent = (child - 1)/ 2 ;


??while(parent < as.size()){

????/* if(child + 1 < as.size() && as[child] < as[child + 1]){ */

????/*???++child; */

????/* } */

????if(as[parent] < as[child]){

??????swap(as[parent] , as[child]);

??????child = parent;

??????parent = (child - 1)/ 2;

????}else{

??????break;

????}

??}

}

template <typename Container>

void print(const Container & con){

??for(auto &elem : con){

????cout << elem << " ";

??}

??cout << endl;

}


void test0(){

??vector<int> vec = {3,4,5,6,1,7,8};

??vector<int> as;


??for(const auto & elem : vec){

????as.emplace_back(elem);

????shiftUp(as);

??}

??print(vec);

??print(as);

}

下濾

template<typename Conpare = std::less<int>>

void heapify(vector<int> &heap,int i,size_t size){

??size_t parent = i;

??size_t child = parent * 2 + 1;


??while(child < heap.size()){

????if(child + 1 < heap.size() && Conpare()(heap[child],heap[child + 1])){

??????child++;

????}

????if(Conpare()(heap[parent],heap[child])){

??????swap(heap[parent] , heap[child]);


??????parent = child;

??????child = parent * 2 + 1;

????}else{

??????break;

????}

??}


}

==============分隔線=================

template<typename Conpare = std::less<int>>

?void heapify(vector<int> &heap,int i,size_t size){

???size_t parent = i;

???size_t child = parent * 2 + 1;

?

???while(child < heap.size()){

?????if(child + 1 < heap.size() && Conpare()(heap[child],heap[child + 1])){

???????child++;

?????}

?????if(Conpare()(heap[parent],heap[child])){

???????swap(heap[parent] , heap[child]);

?

???????parent = child;????????????????????????????

???????child = parent * 2 + 1;

?????}else{

???????break;

?????}

???}

?

?}

//下浮

void buildHeap(vector<int> &heap){

??size_t lastNotLeaf = heap.size()/2 - 1;

??for(int i = lastNotLeaf ; i >= 0 ; --i){

????heapify<std::greater<int>>(heap,i,heap.size());

??}

}

void test1(){

??vector<int> vec = {3,4,5,6,1,7,8};

??buildHeap(vec);

??print(vec);

}

=============優(yōu)先隊列=================

template<typename Conpare = std::less<int>>

void heapify(vector<int> &heap,int i,size_t size){

??size_t parent = i;

??size_t child = parent * 2 + 1;


??while(child < heap.size()){

????if(child + 1 < heap.size() && Conpare()(heap[child],heap[child + 1])){

??????child++;

????}

????if(Conpare()(heap[parent],heap[child])){

??????swap(heap[parent] , heap[child]);


??????parent = child;

??????child = parent * 2 + 1;

????}else{

??????break;

????}

??}


}


int pop(vector<int> &queue){

??int front = queue.front();

??queue[0] = queue.back();

??if(!queue.empty()) queue.pop_back();

??heapify<std::greater<int>>(queue,0,queue.size());

??return front;

}

void test2(){

??//優(yōu)先隊列

??vector<int> vec = {1,3,2,6,5,10,12};

??size_t size = vec.size();

??for(size_t i = 0 ; i < size ; ++i){

????int min = pop(vec);

????cout << min << " ";

??}

??cout << endl;

}





【從堆的定義到優(yōu)先隊列、堆排序】 10分鐘看懂必考的數(shù)據(jù)結(jié)構(gòu)——堆的評論 (共 條)

分享到微博請遵守國家法律
卢湾区| 民和| 静乐县| 仁布县| 龙里县| 勃利县| 南岸区| 夏邑县| 锡林郭勒盟| 曲沃县| 横峰县| 余庆县| 建德市| 淄博市| 唐河县| 津市市| 邹城市| 扎兰屯市| 五台县| 侯马市| 泗阳县| 五河县| 富顺县| 彩票| 永登县| 宁河县| 郓城县| 菏泽市| 张掖市| 郑州市| 辽宁省| 惠州市| 丰宁| 青阳县| 五常市| 澄江县| 左云县| 固阳县| 井陉县| 湛江市| 黎平县|