C++ Max Heap class
Ocak 26, 2008 tarihinde C/C++ kategorisi altında kayda geçti
Here is the max heap class code which is implemented by a vector. vector holds the hnode class object pointers. this code is a little fantasy. because of this you can implement more efficient heap classes looking at this code. this code is just for giving hints. hadi rast gele agalar :)
hnode class
class hnode{ public: string file; int size; hnode(string s,int a); }; hnode::hnode(string s,int a){ file=s; size=a; }
max heap class
class heap{ private: vector files; public: void insert(string file,int size); void remove(string file); void change(string file,int delta); void swap(hnode *a,hnode *b); void largest(); void display(); }; void heap::display(){ int cnt=0; while(cnt cout << cnt << " " << files.at(cnt)->file << " " << files.at(cnt)->size << endl; cnt++; } } void heap::largest(){ cout << files.at(0)->file <<" " << files.at(0)->size << endl; } void heap::insert(string file,int size){ hnode *temp=new hnode(file,size); files.push_back(temp); size=files.size()-1; while(files.at(size)->size > files.at((size-1)/2)->size){ swap(files.at(size),files.at((size-1)/2)); size=(size-1)/2; } } void heap::swap(hnode *a,hnode *b){ int temp; string demp; temp=a->size; a->size=b->size; b->size=temp; demp=a->file; a->file = b->file; b->file= demp; } void heap::remove(string file){ int cnt=0; while(files.at(cnt)->file!=file) cnt++; swap(files.at(cnt),files.at(files.size()-1)); files.pop_back(); if(files.at( (cnt-1)/2 )->size < files.at(cnt)->size ){ while(files.at(cnt)->size > files.at((cnt-1)/2)->size){ swap(files.at(cnt),files.at((cnt-1)/2)); cnt=(cnt-1)/2; } }else{ while(true){ hnode *cur=files.at(cnt); if((2*cnt)+1> files.size()-1) return; if((2*cnt)+1 <= files.size()-1 && (2*cnt)+2 > files.size()-1){ hnode *left=files.at((2*cnt)+1); if(left->size > cur->size) swap(cur,left); return; } if((2*cnt)+1 < files.size() && (2*cnt)+2 < files.size()){ hnode *left=files.at((2*cnt)+1); hnode *right=files.at((2*cnt)+2); if(cur->size > left->size && cur->size > right->size){ return; }else if( cur->size > left->size && cur->size < right->size ){ swap(cur,right); cnt=(2*cnt)+2; }else if(cur->size < left->size && cur->size > right->size ){ swap(cur,left); cnt=(2*cnt)+1; }else{ if(left->size > right->size){ swap(cur,left); cnt=(2*cnt)+1; }else{ swap(cur,right); cnt=(2*cnt)+2; } } } } } } void heap::change(string file,int delta){ int cnt=0; while(files.at(cnt)->file!=file) cnt++; files.at(cnt)->size=files.at(cnt)->size + delta; }Damgalar: c++, heap class, heapify, max heap
Yorum Yap
Eğer bir diyeceğin varsa işte meydan.
Post It
-
* Bir tarafımı kesmeden tıraş olmayı becerdiğim gün hayat çok daha güzel olacak.
-
* Ütü yapmak sanatmış onu öğrendim.
-
* Az dua edin lan şu garibe...
Son Yazılar
- Allah Sorar
- ah azizim ah
- Şikayetim var
- Yağmır
- Bugün cuma
- …..
- Adamlar yapmış
- İstihdam
- Reamonn - Tonight
- Hibernatele developmenta başlamadan önce…
Kategoriler
- Ayaktopu
- Bir Karikatür
- C/C++
- Eften Püften
- Galatasaray
- İnternet
- Java
- Kategorilenmemiş
- Kültür Sanat
- PHP
- Püf Noktası
Son Yorumlar
-
flo demiş ki; i’ve heard this song in pes 2009, also. but i couldn’t find the lyrics. THANKS, YOU REALLY...
sie demiş ki; söz konusu vatan ise gerisi teferruattır askerlik hhiçmi yapmadınız ayılar görmemiş öküsler.
The_The_Theory demiş ki; Bilgisayar muhendisligi bolumunun is hayatinda katma deger olusturmadigi bilinmek...
fatih şeker demiş ki; bu şarkıları muse ın eski şarkılarına benziyor. pes2009 da olmasa zaten duyacağımz...
seda demiş ki; ne oya cok kötü bise
Takiptekiler
Arşivler
- Şubat 2010
- Ocak 2010
- Aralık 2009
- Kasım 2009
- Mayıs 2009
- Nisan 2009
- Mart 2009
- Ocak 2009
- Aralık 2008
- Ekim 2008
- Eylül 2008
- Temmuz 2008
- Haziran 2008
- Mayıs 2008
- Mart 2008
- Şubat 2008
- Ocak 2008
- Kasım 2007
- Ekim 2007
- Eylül 2007
- Ağustos 2007
- Temmuz 2007
- Haziran 2007
Olm bu ne kod lan:D