山东大学数据结构实验报告六.docx
《山东大学数据结构实验报告六.docx》由会员分享,可在线阅读,更多相关《山东大学数据结构实验报告六.docx(18页珍藏版)》请在冰豆网上搜索。
![山东大学数据结构实验报告六.docx](https://file1.bdocx.com/fileroot1/2022-10/26/f8cb5b4f-4b4a-4ef1-91bb-f606ca593aa7/f8cb5b4f-4b4a-4ef1-91bb-f606ca593aa71.gif)
山东大学数据结构实验报告六
数据结构实验报告——实验六
实验题目:
排序算法
学号:
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;iInsert(a[i]);
return*this;
}
voidMaxHeap:
:
HeapSort(int*a,intn)
{
//创建一个最大堆
MaxHeapmaxHeap;
maxHeap.Initialize(a,n);
//从最大堆中逐个抽取元素
intx;
for(inti=n-1;i>=0;i--)
{
max