




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、平衡二叉樹AVL操作模板這篇文章主要介紹了平衡二叉樹AVL操作*II板,需要的朋友可以參考下復(fù)制代碼代碼如下:/* 目的:實(shí)現(xiàn)AVLacm 模板* 利用數(shù)組對(duì)左右兒子簡(jiǎn)化代碼,但是對(duì)腦力難度反而增大不少,只適合* 其實(shí)avl在acm中基本不用,基本被treap取代* avl一般只要求理解思路,不要求寫出代碼,因?yàn)檎嫘暮軣?/#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<time.h>
2、;#include<queue>usingnamespacestd;intCOUNT;/統(tǒng)計(jì)樹中不重復(fù)節(jié)點(diǎn)的個(gè)數(shù)intHEIGHT;/統(tǒng)計(jì)數(shù)的高度/數(shù)據(jù)節(jié)點(diǎn)classDNodepublic:intdata;DNode():data(0);DNode(intd):data(d)booloperator=(constDNode&d)returndata=d.data;booloperator=(constDNode&d)returndata=d.data;booloperator>(constDNode&d)returndata>d.data;boo
3、loperator<(constDNode&d)returndata<d.data;voidshow()cout<<endl;cout <<"*"<< endl;cout<<"data:"<<data<<endl;/treap的節(jié)點(diǎn)template<classT>classAVLNodeprivate:inthgt;/節(jié)點(diǎn)的高度public:Tdata;intcount;AVLNode<T>*son2;/0是左兒子,1是右兒子AVLNode
4、<T>(Tdata):data(data),count(1)son0=son1=NULL;hgt=1;intmax(inta,intb)returna>b?a:b;voidshow()data.show();cout<<"chgt:"<<this->height()<<endl;cout<<"lhgt:"<<this->son0->height()<<endl;cout<<"rhgt:"<<this-&g
5、t;son1->height()<<endl;cout<<"count:"<<count<<endl;cout<<endl;intheight()if(NULL=this)return0;return_getHeight(this);int_getHeight(AVLNode<T>*cur)if(NULL=cur)return0;return1+max(_getHeight(cur->son0),_getHeight(cur->son1);voidsetHeight()hgt=_get
6、Height(this);/AVLTree/這里的T是節(jié)點(diǎn)中的數(shù)據(jù)類型template<classT>classAVLTreeprivate:AVLNode<T>*root;/根節(jié)點(diǎn)inthgt;/樹的高度intsize;/樹中不重復(fù)節(jié)點(diǎn)數(shù)量void_insert(AVLNode<T>*&cur,Tdata);void_mid_travel(AVLNode<T>*cur);/層次遍歷void_cengci_travel(AVLNode<T>*cur);/單旋轉(zhuǎn)(左左,右右),左旋不是想左旋轉(zhuǎn)的意思,而是由于左子樹的左兒子的高度
7、太大/與treap的旋轉(zhuǎn)命名相反void_singleRoate(AVLNode<T>*&cur,intdir);/雙旋轉(zhuǎn)(兩次單旋轉(zhuǎn))void_doubleRoate(AVLNode<T>*&cur,intdir);intmax(inta,intb)returna>b?a:b;public:AVLTree():root(NULL),hgt(0)voidinsert(constT&data);voidmid_travel();intheight()returnroot->height();/層次遍歷voidcengci_travel(
8、)_cengci_travel(root);/*私有方法開始*/template<classT>voidAVLTree<T>:_insert(AVLNode<T>*&cur,Tdata)if(NULL=cur)cur=newAVLNode<T>(data);elseif(data=cur->data)cur->count+;elseintdir=(data>cur->data);_insert(cur->sondir,data);if(2<=cur->sondir->height()-cur
9、->son!dir->height()boollr=(data>cur->data);lr?_singleRoate(cur,dir):_doubleRoate(cur,dir);cur->setHeight();/cout<<"setheight:"<<endl;/cur->show();template<classT>voidAVLTree<T>:_mid_travel(AVLNode<T>*cur)if(NULL!=cur)/先查找做子樹_mid_travel(cur-&g
10、t;son0);/if(abs(cur->son0->height()-cur->son1->height()>=2)cur->show();_mid_travel(cur->son1);/層次遍歷,/如果節(jié)點(diǎn)為空就輸出0來占位template<classT>voidAVLTree<T>:_cengci_travel(AVLNode<T>*cur)if(NULL=cur)return;queue<AVLNode<T>*>q;q.push(cur);AVLNode<T>*top=NU
11、LL;queue<AVLNode<T>*>tmp;while(!q.empty()while(!q.empty()top=q.front();q.pop();if(NULL=top)/用#代表該節(jié)點(diǎn)是空,#的后代還是#cout<<'#'<<""tmp.push(top);elsecout<<top->data.data<<""tmp.push(top->son0);tmp.push(top->son1);boolflag=false;while(!e
12、mpty()if(NULL!=tmp.front()flag=true;q.push(tmp.front();tmp.pop();cout<<endl;if(!flag)break;/單旋轉(zhuǎn),即只旋轉(zhuǎn)一次/dir=0時(shí),左左旋轉(zhuǎn);否則為右右旋轉(zhuǎn)template<classT>voidAVLTree<T>:_singleRoate(AVLNode<T>*&cur,intdir)AVLNode<T>*&k2=cur,*k1=k2->sondir;/k2必須是引用k2->sondir=k1->son!dir
13、;k1->son!dir=k2;k2=k1;k2->setHeight();k1->setHeight();/雙旋轉(zhuǎn),即調(diào)兩次單旋轉(zhuǎn)/dir=0是左右情況;否則為右左情況template<classT>voidAVLTree<T>:_doubleRoate(AVLNode<T>*&cur,intdir)_singleRoate(cur->sondir,!dir);_singleRoate(cur,dir);/*公有方法(接口)開始*/template<classT>voidAVLTree<T>:inse
14、rt(constT&data)_insert(root,data);template<classT>voidAVLTree<T>:mid_travel()_mid_travel(root);intmain()AVLTree<DNode>*avlt=newAVLTree<DNode>();constintnum=30;for(inti=0;i<num;i+)DNode*d=newDNode(i);avlt->insert(*d);cout<<"*"<<endl;avlt->mid_travel();co
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025貴州中醫(yī)藥大學(xué)輔導(dǎo)員考試試題及答案
- 2025秦皇島職業(yè)技術(shù)學(xué)院輔導(dǎo)員考試試題及答案
- 2025蚌埠醫(yī)學(xué)院輔導(dǎo)員考試試題及答案
- 居住空間衛(wèi)生間設(shè)計(jì)要點(diǎn)
- 常見眼底疾病診療概述
- 安順市平壩區(qū)美農(nóng)科技有限公司招聘筆試題庫(kù)2025
- 審計(jì)師職稱考試試題及答案2025年
- 公共關(guān)系與溝通技巧2025年考試試卷及答案
- 2025年文化產(chǎn)業(yè)管理師考試模擬試卷及答案
- 2025年移動(dòng)互聯(lián)網(wǎng)與應(yīng)用開發(fā)基礎(chǔ)知識(shí)測(cè)試試卷及答案
- 2024-2025西師大版一年級(jí)下冊(cè)數(shù)學(xué)期末考試卷及參考答案
- 中國(guó)卒中學(xué)會(huì)急性缺血性卒中再灌注治療指南(2024)解讀
- 浙江開放大學(xué)2025年《社會(huì)保障學(xué)》形考任務(wù)2答案
- 【+初中語文++】++第11課《山地回憶》課件++統(tǒng)編版語文七年級(jí)下冊(cè)
- 2025年度企業(yè)應(yīng)急預(yù)案演練計(jì)劃
- 2025屆東北三省四市教研聯(lián)合體高三下學(xué)期高考模擬考試(一模)英語試題及答案
- 煤炭工業(yè)建筑結(jié)構(gòu)設(shè)計(jì)標(biāo)準(zhǔn)
- 食品科學(xué)與工程實(shí)踐試題集及答案
- 消防設(shè)備維護(hù)質(zhì)量控制及保障措施
- 人教版七年級(jí)下冊(cè)數(shù)學(xué)壓軸題訓(xùn)練(含解析)
- 2025年共青團(tuán)入團(tuán)積極分子考試測(cè)試試卷題庫(kù)及答案
評(píng)論
0/150
提交評(píng)論