




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、數(shù)據(jù)結(jié)構(gòu)實驗報告實驗六 實驗題目:排序算法 學(xué)號: 201411130001日期:2015.12.11班級:計機14.1姓名:劉方錚 Email:實驗?zāi)康模憾押退阉鳂淙蝿?wù)要求: 一、實驗?zāi)康?1、掌握堆和搜索樹的基本概念,插入、刪除方法。二、實驗內(nèi)容 創(chuàng)建最大堆類。最大堆的存儲結(jié)構(gòu)使用鏈表。提供操作:堆的插入、堆的刪除。堆的初始化。Huffman樹的構(gòu)造。二叉搜索樹的構(gòu)造。接收鍵盤錄入的一系列整數(shù),輸出其對應(yīng)的最大堆、Huffman編碼以及二叉搜索樹。堆排序。軟件環(huán)境:Win7 操作系統(tǒng)開發(fā)工具:visual C+ 6.0實驗代碼:#include <iostream
2、>#include <string>#include<cmath>#include<queue>using namespace std;class BinaryTreeNode public:BinaryTreeNode() LeftChild=RightChild=0;BinaryTreeNode(const int & e) data=e;LeftChild=RightChild=0;BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r) data=e;Lef
3、tChild=l;RightChild=r;int data;BinaryTreeNode *LeftChild,*RightChild; void Travel(BinaryTreeNode* roots) queue<BinaryTreeNode *> q; while(roots) cout<<roots->data<<" " if(roots->LeftChild)q.push(roots->LeftChild); if(roots->RightChild)q.push(roots->RightChi
4、ld); try roots=q.front(); q.pop (); catch(.) return; void PrOrder(BinaryTreeNode* roots)if(roots) cout<<roots->data<<"" PrOrder(roots->LeftChild); PrOrder(roots->RightChild);void InOrder(BinaryTreeNode* roots)if(roots) InOrder(roots->LeftChild); cout<<roots-&g
5、t;data<<" " InOrder(roots->RightChild);class MaxHeap public:MaxHeap() root = 0; state = 0; void MakeHeap(int element, MaxHeap& left, MaxHeap& right); int Max() if (!root) return 0; return root->data; MaxHeap& Insert(const int& x); MaxHeap& DeleteMax(int&
6、 x); MaxHeap& Initialize(int a, int n); void Deactivate()heap=0; void HeapSort(int a,int n); BinaryTreeNode *root, *last,*p_last; int state; void ConditionOrder(BinaryTreeNode *u, int k, int i,BinaryTreeNode *temp); void Adjust(BinaryTreeNode *u); BinaryTreeNode* LocateLast(BinaryTreeNode *u,int
7、 k,int i);private: MaxHeap *heap; void MaxHeap:MakeHeap(int element, MaxHeap& left, MaxHeap& right) root = new BinaryTreeNode(element, left.root, right.root); left.root = right.root = 0; last=p_last=root; state+; BinaryTreeNode* MaxHeap:LocateLast(BinaryTreeNode *u,int k,int i) if(k<=1) r
8、eturn u; else int n=(int)pow(2.0,k-1); int s=n/2; if(i<=s) return LocateLast(u->LeftChild,k-1,i); else return LocateLast(u->RightChild,k-1,i-s); void MaxHeap:ConditionOrder(BinaryTreeNode *u, int k, int i,BinaryTreeNode *temp) int half = (int) pow(2.0, k - 2); if (u->data < temp->d
9、ata) swap(u->data, temp->data); if (!u->LeftChild | !u->RightChild) if (!u->LeftChild) u->LeftChild = temp; p_last=u; state+; else u->RightChild = temp; p_last=u; state+; else if (i <= half) ConditionOrder(u->LeftChild, k - 1, i, temp); else ConditionOrder(u->RightChild
10、, k - 1, i - half, temp); MaxHeap& MaxHeap:Insert(const int& x) if(root) int k = (int) (log(double)(state) / log(2.0) + 1; int index = state - (int) (pow(2.0, k - 1) - 1); int p_index = index / 2 + 1; BinaryTreeNode *temp = new BinaryTreeNode (x); last = temp; if (index = (int) (pow(2.0, k -
11、 1) p_index = 1; ConditionOrder(root, k, p_index, temp); else ConditionOrder(root, k - 1, p_index, temp); else root = new BinaryTreeNode(x); last=p_last=root; state+; return *this; void MaxHeap:Adjust(BinaryTreeNode *u) if (!u->LeftChild && !u->RightChild) return; else if(u->LeftChi
12、ld && u->RightChild) if (u->LeftChild->data > u->RightChild->data) if (u->data < u->LeftChild->data) swap(u->data, u->LeftChild->data); Adjust(u->LeftChild); else if (u->data < u->RightChild->data) swap(u->data, u->RightChild->da
13、ta); Adjust(u->RightChild); MaxHeap& MaxHeap:DeleteMax(int& x) if (!root) exit(1) ; else if(!last) x=root->data; state=0;root=0; else x = root->data; root->data = last->data; int k = (int)(log(double)(state)/log(double)(2)+ 1; int index = state - (int)(pow(2.0, k - 1) - 1); Ad
14、just(root); if(index%2) p_last->LeftChild=0; else p_last->RightChild=0; delete last; state-; k = (int)(log(double)(state-1)/log(double)(2)+ 1; index = state - 1 - (int)(pow(2.0, k - 1) - 1); int p_index = index / 2 + 1; if (index = (int) (pow(2.0, k - 1) p_index=1; p_last=LocateLast(root,k,p_i
15、ndex); else p_last=LocateLast(root,k-1,p_index); if(!p_last->RightChild) last=p_last->LeftChild; else last=p_last->RightChild; return *this; MaxHeap& MaxHeap:Initialize(int a, int n) MaxHeap LMaxHeap,RMaxHeap; MakeHeap(a0,LMaxHeap,RMaxHeap); for(int i=1;i<n;i+) Insert(ai); return *th
16、is; void MaxHeap:HeapSort(int * a,int n)/創(chuàng)建一個最大堆MaxHeap maxHeap;maxHeap.Initialize(a,n);/從最大堆中逐個抽取元素int x;for(int i=n-1;i>=0;i-) maxHeap.DeleteMax(x);/ cout<<"i="<<i<<"-"<<x<<endl;/ Travel(root);cout<<endl; ai=x;/在堆的析構(gòu)函數(shù)中保存數(shù)組a/maxHeap.Deacti
17、vate();class HTNodepublic:int weight;/權(quán)重int parent,lchild,rchild; ; typedef HTNode * HuffmanTree;HuffmanTree HuffmanCoding(char* &HC,int w,int a,int n);void Select(HuffmanTree HT,int n,int&s1,int&s2); void HTEcode(char* &HC,int w,int code,int codeLen,int a,int aLen );void Select(Huff
18、manTree HT,int n,int&s1,int&s2)int i=1,j;while(HTi.parent!=0) i+; j=i+1;while(HTj.parent!=0) j+;if(HTi.weight>HTj.weight) s1=j;/s1為權(quán)重小的 s2=i;else s1=i; s2=j;i=j+1;while(i<=n) if(HTi.parent!=0) i+; else if(HTi.weight<HTs1.weight) s2=s1; s1=i; else if(HTi.weight<HTs2.weight) s2=i;
19、i+;HuffmanTree HuffmanCoding(char *& HC,int w,int a,int n)int i,start,c,f;HTNode *p;char *cd;if(n<1)return NULL;int m=2*n-1;/定義一個有m+1個節(jié)點的霍夫曼樹HuffmanTree HT=new HTNodem+1; /初始化for(p=HT+1,i=1;i<=n;i+,p+) p->weight=wi-1; p->lchild=0; p->rchild=0; p->parent=0; int s1,s2;for(;i<=
20、m;+i) Select(HT,i-1,s1,s2); HTs1.parent=i; HTs2.parent=i; HTi.parent=0; HTi.lchild=s1; HTi.rchild=s2; HTi.weight=HTs1.weight+HTs2.weight;HC=new char* n;cd=new charn; cdn-1='0' for(i=1;i<=n;+i) start=n-1; for(c=i,f=HTi.parent;f!=0;c=f,f=HTf.parent) if(HTf.lchild=c) cd-start='0' els
21、e cd-start='1' HCi-1=new charn-start; strcpy(HCi-1,&cdstart); return HT;void HTEcode(char* &HC,int w,int code,int codeLen,int a,int aLen ) int j;HuffmanTree HT=HuffmanCoding(HC,w,code,codeLen); /for(int f=1;f<=2*codeLen-1;f+) / cout<<"n序號:"<<f<<" 雙
22、親結(jié)點:" <<HTf.parent<<" 左孩子結(jié)點:" <<HTf.lchild <<" 右孩子結(jié)點:" <<HTf.rchild<< " 權(quán)值:"<<HTf.weight<<endl; for(int r=0;r<codeLen;r+) cout<<"n"<<coder<<"的字符的赫夫曼編碼為:"<<HCr<<endl
23、;for(int i=0;i<aLen;i+) bool find=false; for(j=0;j<codeLen;j+) if(ai=codej) find=true; cout<<HCj; / if(!find) / cout<<"空" class DBSTreepublic :DBSTree()root=0;BinaryTreeNode * root;DBSTree & BSInitialize(int a,int len); DBSTree & BSInsert(const int& e);DBSTree
24、 & DBSTree:BSInsert(const int& e)BinaryTreeNode *p=root,*pp=0;while(p) pp=p; if(e<p->data) p=p->LeftChild; else if(e>p->data) p=p->RightChild;BinaryTreeNode * r=new BinaryTreeNode(e);if(root) if(e<pp->data) pp->LeftChild=r; else pp->RightChild=r;else root=r;retu
25、rn *this;DBSTree & DBSTree:BSInitialize(int a,int len) for(int i=0;i<len;i+) BSInsert(ai); return *this;void main() MaxHeap maxHeap;DBSTree bstree;int s, n,sel,Alen;char *codeA;int* IntA,* w;cout<<"輸入最大堆元素個數(shù)"int len;cin>>len;int * a=new intlen;for(int i=0;i<len;i+) co
26、ut<<"輸入第"<<i+1<<"個元素"<<endl; cin>>ai;maxHeap.Initialize(a,len);cout<<"最大堆操作:"<<endl;cout<<"0.堆排序"<<endl;maxHeap.HeapSort(a,len);for(int h=0;h<len;h+) cout <<ah<<" " cout <<end
27、l;cout<<"1.初始化層次遍歷輸出最大堆"<<endl;Travel(maxHeap.root);cout<<endl;cout<<"前序遍歷輸出最大堆n"PrOrder(maxHeap.root);cout<<endl;cout<<"2.插入整數(shù)元素"<<endl;cin>>s;maxHeap.Insert(s);cout<<"層次遍歷輸出最大堆n"Travel(maxHeap.root);cout<<endl;cout<<"前序遍歷輸出最大堆n"PrOrder(maxHeap.root);cout<<endl; cout<<"3.刪除最大元素n"maxHeap.DeleteMax(s);cout<<"層次遍歷輸出最大堆n" Travel(maxHeap.root);cout
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 河北省張家口市萬全縣2025年數(shù)學(xué)三下期末檢測模擬試題含解析
- 2025屆玉樹藏族自治州雜多縣數(shù)學(xué)三下期末經(jīng)典試題含解析
- 天津科技大學(xué)《市場調(diào)查與預(yù)測》2023-2024學(xué)年第二學(xué)期期末試卷
- 婁底市雙峰縣2025屆三下數(shù)學(xué)期末教學(xué)質(zhì)量檢測試題含解析
- 2025年02月浙江麗水職業(yè)技術(shù)學(xué)院公開招聘專業(yè)技術(shù)人員18人(一)筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 課題開題報告:大中小學(xué)鑄牢中華民族共同體意識教育一體化研究
- 課題開題報告:楚天技能名師教學(xué)崗位建設(shè)與管理研究
- 課題開題報告:產(chǎn)業(yè)協(xié)同高質(zhì)量發(fā)展的統(tǒng)計監(jiān)測和路徑研究
- 2020-2021學(xué)年江蘇鹽城亭湖區(qū)五年級下冊語文期末試卷及答案
- 出租信息共享協(xié)議
- 第3課《列夫·托爾斯泰》課件-2024-2025學(xué)年統(tǒng)編版語文七年級下冊
- TSDLPA 0001-2024 研究型病房建設(shè)和配置標(biāo)準
- 陜09J01 建筑用料及做法圖集
- PI形式發(fā)票范文模板
- 建筑冷熱源素材
- 網(wǎng)絡(luò)安全用戶實體行為分析技術(shù)UEBA白皮書
- 室內(nèi)設(shè)計-中式古典風(fēng)格課件
- MOC3061驅(qū)動BT134雙向可控硅
- 無線通信與網(wǎng)絡(luò)復(fù)習(xí)資料
- 八大員考試試題——勞務(wù)員題庫
- 人教版小學(xué)數(shù)學(xué)五年級下冊教材分析
評論
0/150
提交評論