山东大学数据结构实验报告六汇编.docx

上传人:b****4 文档编号:3836141 上传时间:2022-11-25 格式:DOCX 页数:18 大小:27.44KB
下载 相关 举报
山东大学数据结构实验报告六汇编.docx_第1页
第1页 / 共18页
山东大学数据结构实验报告六汇编.docx_第2页
第2页 / 共18页
山东大学数据结构实验报告六汇编.docx_第3页
第3页 / 共18页
山东大学数据结构实验报告六汇编.docx_第4页
第4页 / 共18页
山东大学数据结构实验报告六汇编.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

山东大学数据结构实验报告六汇编.docx

《山东大学数据结构实验报告六汇编.docx》由会员分享,可在线阅读,更多相关《山东大学数据结构实验报告六汇编.docx(18页珍藏版)》请在冰豆网上搜索。

山东大学数据结构实验报告六汇编.docx

山东大学数据结构实验报告六汇编

数据结构实验报告——实验六

 

实验题目:

排序算法

学号:

201411130001

日期:

2015.12.11

班级:

计机14.1

姓名:

刘方铮

Email:

liu191150932@

实验目的:

堆和搜索树

任务要求:

 一、实验目的

1、掌握堆和搜索树的基本概念,插入、删除方法。

二、实验内容

创建最大堆类。

最大堆的存储结构使用链表。

提供操作:

堆的插入、堆的删除。

堆的初始化。

Huffman树的构造。

二叉搜索树的构造。

接收键盘录入的一系列整数,输出其对应的最大堆、Huffman编码以及二叉搜索树。

堆排序。

 

软件环境:

Win7操作系统

开发工具:

visualC++6.0

实验代码:

#include

#include

#include

#include

usingnamespacestd;

classBinaryTreeNode

{

public:

BinaryTreeNode(){LeftChild=RightChild=0;}

BinaryTreeNode(constint&e)

{

data=e;

LeftChild=RightChild=0;

}

BinaryTreeNode(constint&e,BinaryTreeNode*l,BinaryTreeNode*r)

{

data=e;

LeftChild=l;

RightChild=r;

}

intdata;

BinaryTreeNode*LeftChild,*RightChild;

};

voidTravel(BinaryTreeNode*roots){

queueq;

while(roots){

cout<data<<"";

if(roots->LeftChild)q.push(roots->LeftChild);

if(roots->RightChild)q.push(roots->RightChild);

try{

roots=q.front();

q.pop();

}

catch(...)

{return;}

}

}

voidPrOrder(BinaryTreeNode*roots){

if(roots){

cout<data<<" ";

PrOrder(roots->LeftChild);

PrOrder(roots->RightChild);}

}

voidInOrder(BinaryTreeNode*roots){

if(roots){

InOrder(roots->LeftChild);

cout<data<<"";

InOrder(roots->RightChild);}

}

classMaxHeap{

public:

MaxHeap(){

root=0;

state=0;

}

voidMakeHeap(intelement,MaxHeap&left,MaxHeap&right);

intMax(){

if(!

root)

return0;

returnroot->data;

}

MaxHeap&Insert(constint&x);

MaxHeap&DeleteMax(int&x);

MaxHeap&Initialize(inta[],intn);

voidDeactivate(){heap=0;}

voidHeapSort(inta[],intn);

BinaryTreeNode*root,*last,*p_last;

intstate;

voidConditionOrder(BinaryTreeNode*u,intk,inti,BinaryTreeNode*temp);

voidAdjust(BinaryTreeNode*u);

BinaryTreeNode*LocateLast(BinaryTreeNode*u,intk,inti);

private:

MaxHeap*heap;

};

voidMaxHeap:

:

MakeHeap(intelement,MaxHeap&left,MaxHeap&right)

{

root=newBinaryTreeNode(element,left.root,right.root);

left.root=right.root=0;

last=p_last=root;

state++;

}

BinaryTreeNode*MaxHeap:

:

LocateLast(BinaryTreeNode*u,intk,inti)

{

if(k<=1)

returnu;

else

{

intn=(int)pow(2.0,k-1);

ints=n/2;

if(i<=s)

returnLocateLast(u->LeftChild,k-1,i);

else

returnLocateLast(u->RightChild,k-1,i-s);

}

}

voidMaxHeap:

:

ConditionOrder(BinaryTreeNode*u,intk,inti,BinaryTreeNode*temp)

{

inthalf=(int)pow(2.0,k-2);

if(u->datadata)

{

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,k-1,i-half,temp);

}

}

MaxHeap&MaxHeap:

:

Insert(constint&x){

if(root){

intk=(int)(log((double)(state))/log(2.0))+1;

intindex=state-(int)(pow(2.0,k-1)-1);

intp_index=index/2+1;

BinaryTreeNode*temp=newBinaryTreeNode(x);

last=temp;

if(index==(int)(pow(2.0,k-1))){

p_index=1;

ConditionOrder(root,k,p_index,temp);

}

else

ConditionOrder(root,k-1,p_index,temp);}

else{

root=newBinaryTreeNode(x);

last=p_last=root;

state++;}

return*this;

}

voidMaxHeap:

:

Adjust(BinaryTreeNode*u){

if(!

u->LeftChild&&!

u->RightChild)

return;

elseif(u->LeftChild&&u->RightChild){

if(u->LeftChild->data>u->RightChild->data){

if(u->dataLeftChild->data)

{

swap(u->data,u->LeftChild->data);

Adjust(u->LeftChild);

}

}

else

{

if(u->dataRightChild->data)

{

swap(u->data,u->RightChild->data);

Adjust(u->RightChild);

}

}

}}

MaxHeap&MaxHeap:

:

DeleteMax(int&x)

{

if(!

root)

exit

(1);

elseif(!

last)

{x=root->data;

state=0;root=0;}

else{

x=root->data;

root->data=last->data;

intk=(int)(log((double)(state))/log((double)

(2)))+1;

intindex=state-(int)(pow(2.0,k-1)-1);

Adjust(root);

if(index%2)

p_last->LeftChild=0;

else

p_last->RightChild=0;

deletelast;

state--;

k=(int)(log((double)(state-1))/log((double)

(2)))+1;

index=state-1-(int)(pow(2.0,k-1)-1);

intp_index=index/2+1;

if(index==(int)(pow(2.0,k-1))){

p_index=1;

p_last=LocateLast(root,k,p_index);

}

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(inta[],intn)

{

MaxHeapLMaxHeap,RMaxHeap;

MakeHeap(a[0],LMaxHeap,RMaxHeap);

for(inti=1;i

Insert(a[i]);

return*this;

}

voidMaxHeap:

:

HeapSort(int*a,intn)

{

//创建一个最大堆

MaxHeapmaxHeap;

maxHeap.Initialize(a,n);

//从最大堆中逐个抽取元素

intx;

for(inti=n-1;i>=0;i--)

{

maxHeap.DeleteMax(x);

//cout<<"i="<

//Travel(root);cout<

a[i]=x;

}

//在堆的析构函数中保存数组a

//maxHeap.Deactivate();

}

classHTNode

{

public:

intweight;//权重

intparent,lchild,rchild;

};

typedefHTNode*HuffmanTree;

HuffmanTreeHuffmanCoding(char**&HC,intw[],inta[],intn);

voidSelect(HuffmanTreeHT,intn,int&s1,int&s2);

voidHTEcode(char**&HC,intw[],intcode[],intcodeLen,inta[],intaLen);

voidSelect(HuffmanTreeHT,intn,int&s1,int&s2)

{

inti=1,j;

while(HT[i].parent!

=0)

i++;

j=i+1;

while(HT[j].parent!

=0)

j++;

if(HT[i].weight>HT[j].weight)

{

s1=j;//s1为权重小的

s2=i;

}

else

{

s1=i;

s2=j;

}

i=j+1;

while(i<=n)

{

if(HT[i].parent!

=0)

i++;

elseif(HT[i].weight

{

s2=s1;

s1=i;

}

elseif(HT[i].weight

s2=i;

i++;

}

}

HuffmanTreeHuffmanCoding(char**&HC,intw[],inta[],intn)

{

inti,start,c,f;

HTNode*p;

char*cd;

if(n<1)returnNULL;

intm=2*n-1;

//定义一个有m+1个节点的霍夫曼树

HuffmanTreeHT=newHTNode[m+1];

//初始化

for(p=HT+1,i=1;i<=n;i++,p++)

{

p->weight=w[i-1];

p->lchild=0;

p->rchild=0;

p->parent=0;

}

ints1,s2;

for(;i<=m;++i)

{

Select(HT,i-1,s1,s2);

HT[s1].parent=i;

HT[s2].parent=i;

HT[i].parent=0;

HT[i].lchild=s1;

HT[i].rchild=s2;

HT[i].weight=HT[s1].weight+HT[s2].weight;

}

HC=newchar*[n];

cd=newchar[n];

cd[n-1]='\0';

for(i=1;i<=n;++i)

{

start=n-1;

for(c=i,f=HT[i].parent;f!

=0;c=f,f=HT[f].parent)

{

if(HT[f].lchild==c)

cd[--start]='0';

else

cd[--start]='1';

}

HC[i-1]=newchar[n-start];

strcpy(HC[i-1],&cd[start]);

}

returnHT;

}

voidHTEcode(char**&HC,intw[],intcode[],intcodeLen,inta[],intaLen)

{intj;

HuffmanTreeHT=HuffmanCoding(HC,w,code,codeLen);

//for(intf=1;f<=2*codeLen-1;f++)

//cout<<"\n序号:

"<

"<

"<

"<

"<

for(intr=0;r

{

cout<<"\n"<

"<

}

for(inti=0;i

{boolfind=false;

for(j=0;j

{

if(a[i]==code[j])

{

find=true;

cout<

}

}

//if(!

find)

//cout<<"空";

}

}

classDBSTree{

public:

DBSTree(){root=0;};

BinaryTreeNode*root;

DBSTree&BSInitialize(inta[],intlen);

DBSTree&BSInsert(constint&e);

};

DBSTree&DBSTree:

:

BSInsert(constint&e){

BinaryTreeNode*p=root,*pp=0;

while(p){

pp=p;

if(edata)

p=p->LeftChild;

elseif(e>p->data)

p=p->RightChild;

}

BinaryTreeNode*r=newBinaryTreeNode(e);

if(root){

if(edata)

pp->LeftChild=r;

elsepp->RightChild=r;}

else

root=r;

return*this;

}

DBSTree&DBSTree:

:

BSInitialize(inta[],intlen){

for(inti=0;i

BSInsert(a[i]);

return*this;

}

voidmain()

{MaxHeapmaxHeap;

DBSTreebstree;

ints,n,sel,Alen;

char**codeA;

int*IntA,*w;

cout<<"输入最大堆元素个数";

intlen;

cin>>len;

int*a=newint[len];

for(inti=0;i

cout<<"输入第"<

cin>>a[i];

}

maxHeap.Initialize(a,len);

cout<<"最大堆操作:

"<

cout<<"0.堆排序"<

maxHeap.HeapSort(a,len);

for(inth=0;h

cout<

cout<

cout<<"1.初始化层次遍历输出最大堆"<

Travel(maxHeap.root);

cout<

cout<<"前序遍历输出最大堆\n";

PrOrder(maxHeap.root);

cout<

cout<<"2.插入整数元素"<

cin>>s;

maxHeap.Insert(s);

cout<<"层次遍历输出最大堆\n";

Travel(maxHeap.root);

cout<

cout<<"前序遍历输出最大堆\n";

PrOrder(maxHeap.root);

cout<

cout<<"3.删除最大元素\n";

maxHeap.DeleteMax(s);

cout<<"层次遍历输出最大堆\n";

Travel(maxHeap.root);

cout<

///////////////////////////////////////

cout<<"Huffman操作";

cout<<"输入元素个数"<

cin>>Alen;

IntA=newint[Alen];

w=newint[Alen];

for(n=0;n

cout<<"\n输入第"<

cin>>IntA[n];

cout<<"\n输入对应权值\n";

cin>>w[n];

}

HuffmanCoding(codeA,w,IntA,len);

HTEcode(codeA,w,IntA,Alen,a,len);

cout<

///////////////////////////////////////////

cout<<"二叉搜索树层次遍历"<

cout<<"输入二叉搜索树元素个数";

intbslen;

cin>>bslen;

int*b=newint[bslen];

for(intj=0;j

cout<<"输入第"<

cin>>b[j];

}

bstree.BSInitialize(b,bslen);

cout<<"前序遍历输出二叉搜索树:

"<

PrOrder(bstree.root);

cout<

cout<<"中序遍历输出二叉搜索树:

"<

InOrder(bstree.root);

cout<

cout<<"层次遍历输出二叉搜索树:

"<

Travel(bstree.root);

cout<

}

实验结果:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1