第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下內(nèi)容是關(guān)于數(shù)據(jù)結(jié)構(gòu) 最小堆求解的問題!請看代碼~

以下內(nèi)容是關(guān)于數(shù)據(jù)結(jié)構(gòu) 最小堆求解的問題!請看代碼~

#include<iostream>using namespace std;template <class T>class MinHeap{private:T *heapArray;int CurrentSize;int MaxSize;void BuildHeap();public:MinHeap( int n);bool isEmpty();int LeftChild(int pos) ;int Parent(int pos) ;bool Insert( T& newNode);T& RemoveMin();void SiftUp(int position);void SiftDown(int left);};class Dist{public:int index;int length;int pre;};template <class T>MinHeap<T>::MinHeap( int n){if(n<=0)return;CurrentSize=0;MaxSize=n;heapArray=new T[MaxSize];BuildHeap();}template<class T>bool MinHeap<T>::isEmpty(){if(CurrentSize==0)return true;elsereturn false;}template <class T>void MinHeap<T>::BuildHeap(){for(int i=CurrentSize/2-1;i>=0;i--)SiftDown(i);}template <class T>int MinHeap<T>::LeftChild(int pos) {return 2*pos+1;}template <class T>int MinHeap<T>::Parent(int pos) {return (pos-1)/2;}template <class T>bool MinHeap<T>::Insert( T& newNode){if(CurrentSize==MaxSize)return false;heapArray[CurrentSize]=newNode;SiftUp(CurrentSize);CurrentSize++;return true;}template <class T>T& MinHeap<T>::RemoveMin(){if(CurrentSize==0){cout<<"Can`t Delete";exit(1);}else{swap(0,--CurrentSize);if(CurrentSize>1)SiftDown(0);return heapArray[CurrentSize];}}template <class T>void MinHeap<T>::SiftUp(int position){int temppos=position;T temp=heapArray[temppos];while((temppos>0)&&(heapArray[Parent(temppos)]>temp)){heapArray[temppos]=heapArray[Parent(temppos)];temppos=Parent(temppos);}heapArray[temppos]=temp;}template <class T>void MinHeap<T>::SiftDown(int left){int i=left;int j=LeftChild(i);T temp=heapArray[i];while(j<CurrentSize){if((j<CurrentSize-1)&&(heapArray[j]>heapArray[j+1]))j++;if(temp>heapArray[j]){heapArray[i]=heapArray[j];i=j;j=LeftChild(j);}else break;}heapArray[i]=temp;}void main(){MinHeap<Dist> heap(5);}//MinHeap<int> heap(5)就沒問題 為什么MinHeap<Dist> heap(5)把Dist類放進(jìn)去就錯了?前面的都不太用看 主要看最后的main那里就好
查看完整描述

1 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊

heapArray[j]>heapArray[j+1]等等 比較語句
int是沒問題 但自定義的需要重載下運算符

查看完整回答
反對 回復(fù) 2022-03-22
  • 1 回答
  • 0 關(guān)注
  • 385 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號