C++堆和搜索树

一、 要求完成时间
实验开始后的第七周之前完成
二、 实验目的
掌握堆和搜索树的基本概念,插入、删除方法。
三、 实验内容
1、 输入一系列不为零的正整数(最多不超过20个),遇到0代表输入结束(不包含0)。
2、 根据输入的数据,创建最大堆,输出最大堆的层次序列。
3、 输出用堆排序后的排序结果。
4、 根据输入的数据,创建二叉搜索树,输出二叉搜索树的前序序列。
5、 输出二叉搜索树的中序序列。

6、 根据输入的数据作为字母出现的频率(第1个数代表A频率,第2个数代表B频率,……),创建Huffman树,输出Huffman编码,要求左子树权值小于右子树权值,左边为0,右边为1。输出按字数顺序由小到大输出,格式采用“字母:编码”,例如A:0,B:10……



#include <iostream>
#include <queue>
#include <string>
using namespace std;


int cc=0;

struct HNode{
	char character;
	string str;
	int num; //表示该节点的大小
	HNode *left,*right;
};

struct MinHeap{
	int currentSize,maxSize;
	HNode *heap;
	
	MinHeap(int max);
	int Size() const {return currentSize;}
	//int Max(){return heap[1];}
	MinHeap& Insert(const HNode *x);
	MinHeap& DeleteMax(HNode *x);
	void Initialize(HNode *a,int size,int arraySize);
	void levelOrder();
	void sort();
};

MinHeap::MinHeap(int max){
	maxSize=max;
	heap=new HNode[max+1]; //这个数组用于存储最大堆,但要注意:该数组的0号位空置不用,即从1号位计起
	currentSize=0;
}

void MinHeap::Initialize(HNode *a,int size,int arraySize){
	delete [] heap;
	heap=a;
	currentSize=size;
	maxSize=arraySize;
	/*cout<<heap[1].character<<endl;
	cout<<heap[2].character<<endl;
	cout<<heap[3].character<<endl;*/
	
	for(int i=currentSize/2;i>=1;i--){
		HNode y=heap[i];
		int ci=2*i;
		while(ci<=currentSize){
			if(ci<currentSize && heap[ci].num > heap[ci+1].num){
				ci++;
			}
			if(y.num < heap[ci].num) break;
			heap[ci/2]=heap[ci];
			ci*=2;
		}
		heap[ci/2]=y;
	}
	/*cout<<heap[1].character<<endl;
	cout<<heap[2].character<<endl;
	cout<<heap[3].character<<endl;*/
}

MinHeap& MinHeap::Insert(const HNode * x){
	//将x插入到最小堆中
	int i=++currentSize;
	while(i!=1 && x->num<heap[i/2].num){
		heap[i]=heap[i/2];
		i/=2;
	}
	heap[i]=*x;
	return *this;
}

MinHeap& MinHeap::DeleteMax(HNode *x){
	//cout<<heap[1].character<<"  shjhjdf"<<endl;

	*x=heap[1];
	HNode y=heap[currentSize--]; //取得当前堆中最后一个元素
	int i=1;
	int ci=2; //使ci为i的子节点
	while(ci<=currentSize){
		if(ci<currentSize && heap[ci].num>heap[ci+1].num){
			ci++; //使ci为i的子节点
		}
		if(y.num<=heap[ci].num) break; //此时y可以放在i位上
		heap[i]=heap[ci];
		i=ci;
		ci*=2;
	}
	heap[i]=y;
	//cout<<x->character<<"  sdf"<<x->num<<endl;
	return *this;
}





struct MaxHeap{
	int currentSize, maxSize;
	int *heap;

	MaxHeap(int max);
	int Size() const {return currentSize;}
	int Max(){return heap[1];}
	MaxHeap& Insert(const int & x);
	MaxHeap& DeleteMax(int & x);
	void Initialize(int a[],int size,int arraySize);
	void levelOrder();
	void sort();

};

void MaxHeap::sort(){
	int x;
	int size=currentSize;
	for(int i=currentSize;i>1;i--){
		DeleteMax(x);
		heap[i]=x;
	}
	for(int n=1;n<size;n++){
		cout<<heap[n]<<",";
	}
	cout<<heap[size]<<endl;
}

MaxHeap::MaxHeap(int max){
	maxSize=max;
	heap=new int[max+1]; //这个数组用于存储最大堆,但要注意:该数组的0号位空置不用,即从1号位计起
	currentSize=0;
}

MaxHeap& MaxHeap::Insert(const int &x){
	//将x插入到最大堆中
	int i=++currentSize;
	while(i!=1 && x>heap[i/2]){
		heap[i]=heap[i/2];
		i/=2;
	}
	heap[i]=x;
	return *this;
}

MaxHeap& MaxHeap::DeleteMax(int &x){
	x=heap[1];
	int y=heap[currentSize--]; //取得当前堆中最后一个元素
	int i=1;
	int ci=2; //使ci为i的子节点
	while(ci<=currentSize){
		if(ci<currentSize && heap[ci]<heap[ci+1]){
			ci++; //使ci为i的子节点
		}
		if(y>=heap[ci]) break; //此时y可以放在i位上
		heap[i]=heap[ci];
		i=ci;
		ci*=2;
	}
	heap[i]=y;
	return *this;
}

void MaxHeap::Initialize(int a[],int size,int arraySize){
	delete [] heap;
	heap=a;
	currentSize=size;
	maxSize=arraySize;
	
	for(int i=currentSize/2;i>=1;i--){
		int y=heap[i];
		int ci=2*i;
		while(ci<=currentSize){
			if(ci<currentSize && heap[ci]<heap[ci+1]){
				ci++;
			}
			if(y>heap[ci]) break;
			heap[ci/2]=heap[ci];
			ci*=2;
		}
		heap[ci/2]=y;
	}
}

void MaxHeap::levelOrder(){
	int count=0;
	queue<int> Q;
	int p=1;
	Q.push(1);
	int left,right;
	while(!Q.empty()){
		count++;
		p=Q.front();
		Q.pop();
		cout<<heap[p];
		if(count<currentSize) cout<<",";
		left=p*2;
		if(left<=currentSize) Q.push(left);		
		right=left+1;
		if(right<=currentSize) Q.push(right);
	}
	cout<<endl;
}

struct Node{
	Node(int x){data=x; left=right=0;}
	int data;
	Node *left;
	Node *right;
};

void Insert(Node *node,const int & x){  //将元素x插入以node为根的二叉搜索树中
	Node *p=node;
	Node *pp=0; //pp为p的父节点
	while(p){
		pp=p;
		if(x<p->data) p=p->left;
		else 
			if(x>p->data) p=p->right;
			else {}//出现重复 
	}
	Node *y=new Node(x);
	if(node){ //不是空树
		if(pp->data>x){
			pp->left=y;
		}
		else{
			pp->right=y;
		}
	}
	else{
	//	node=y;
	}

}

void preOrder(Node *root,int num){
	if(root){
		cc++;
		cout<<root->data;
		if(cc<num)
			cout<<",";
		preOrder(root->left,num);
		preOrder(root->right,num);
	}
}

void inOrder(Node *root,int num){
	if(root){
		inOrder(root->left,num);
		cc++;
		cout<<root->data;
		if(cc<num)
			cout<<",";		
		inOrder(root->right,num);
	}
}

void preOrder(HNode *root,int number,string s){ //num为指令,1代表为左节点,2代表为右节点,0代表为根节点
	if(root){
		if(number==1){root->str=s+"0";}	
		if(number==2){root->str=s+"1";}
		//cout<<number<<" "<<root->character<<"   "<<root->str<<endl;
		preOrder(root->left,1,root->str);
		preOrder(root->right,2,root->str);
	}
}




void main(){
	cout<<"Input1"<<endl;
	MaxHeap *mh=new MaxHeap(25);
	int a[25];
	int b[25];
	HNode c[25];
	int i;
	cin>>i;
	int count=0;
	for(;i!=0;cin>>i){
		count++;
		a[count]=i;
		b[count]=i;
		c[count].num=i;
		c[count].character='A'+count-1;
		c[count].str="";
		c[count].left=0;
		c[count].right=0;
	}
	cout<<"Output"<<endl;
	mh->Initialize(a,count,25);
	mh->levelOrder();
	mh->sort();
	
	Node *node=new Node(b[1]);

	for(int n=2;n<=count;n++){
		Insert(node,b[n]);
	}

	preOrder(node,count);
	cout<<endl;
	cc=0;
	inOrder(node,count);
	cout<<endl;
	cc=0;
	
	MinHeap *minh=new MinHeap(25);
	minh->Initialize(c,count,25);

	HNode *arr[25];
	int count2=0;
	HNode *x,*y;
	for(int nn=1;nn<count;nn++){
		x=new HNode; 
		y=new HNode; 
		minh->DeleteMax(x);
		if(x->character!='0'){
			arr[count2]=x;
			count2++;
		}
		//cout<<x->num<<"  "<<x->character<<endl;
		minh->DeleteMax(y);
		if(y->character!='0'){
			arr[count2]=y;
			count2++;
		}
		//cout<<y->num<<"  "<<y->character<<endl;
		HNode *z=new HNode;
		z->character='0';
		z->num=x->num+y->num;
		z->left=x;
		z->right=y;
		z->str="";
		x=z;
		minh->Insert(x);
	}

	preOrder(x,0,"");

	for(i=1;i<count;i++){
		HNode *t=arr[i];
		for(int n=i-1;n>=0 && arr[n]->character > t->character;n--){
			arr[n+1]=arr[n];
		}
		arr[n+1]=t;
	}
	
	for(i=0;i<count-1;i++){
		cout<<arr[i]->character<<":"<<arr[i]->str<<",";
	}
	cout<<arr[count-1]->character<<":"<<arr[count-1]->str<<endl;
	cout<<endl;
	
	cout<<"End"<<endl;


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值