實驗開始後的第七週之前完成
二、 實驗目的
掌握堆和搜索樹的基本概念,插入、刪除方法。
三、 實驗內容
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;
- }