C++堆和搜索樹

一、 要求完成時間
實驗開始後的第七週之前完成
二、 實驗目的
掌握堆和搜索樹的基本概念,插入、刪除方法。
三、 實驗內容
1、 輸入一系列不爲零的正整數(最多不超過20個),遇到0代表輸入結束(不包含0)。
2、 根據輸入的數據,創建最大堆,輸出最大堆的層次序列。
3、 輸出用堆排序後的排序結果。
4、 根據輸入的數據,創建二叉搜索樹,輸出二叉搜索樹的前序序列。
5、 輸出二叉搜索樹的中序序列。

6、 根據輸入的數據作爲字母出現的頻率(第1個數代表A頻率,第2個數代表B頻率,……),創建Huffman樹,輸出Huffman編碼,要求左子樹權值小於右子樹權值,左邊爲0,右邊爲1。輸出按字數順序由小到大輸出,格式採用“字母:編碼”,例如A:0,B:10……



  1. #include <iostream>   
  2. #include <queue>   
  3. #include <string>   
  4. using namespace std;  
  5.   
  6.   
  7. int cc=0;  
  8.   
  9. struct HNode{  
  10.     char character;  
  11.     string str;  
  12.     int num; //表示該節點的大小   
  13.     HNode *left,*right;  
  14. };  
  15.   
  16. struct MinHeap{  
  17.     int currentSize,maxSize;  
  18.     HNode *heap;  
  19.       
  20.     MinHeap(int max);  
  21.     int Size() const {return currentSize;}  
  22.     //int Max(){return heap[1];}   
  23.     MinHeap& Insert(const HNode *x);  
  24.     MinHeap& DeleteMax(HNode *x);  
  25.     void Initialize(HNode *a,int size,int arraySize);  
  26.     void levelOrder();  
  27.     void sort();  
  28. };  
  29.   
  30. MinHeap::MinHeap(int max){  
  31.     maxSize=max;  
  32.     heap=new HNode[max+1]; //這個數組用於存儲最大堆,但要注意:該數組的0號位空置不用,即從1號位計起   
  33.     currentSize=0;  
  34. }  
  35.   
  36. void MinHeap::Initialize(HNode *a,int size,int arraySize){  
  37.     delete [] heap;  
  38.     heap=a;  
  39.     currentSize=size;  
  40.     maxSize=arraySize;  
  41.     /*cout<<heap[1].character<<endl; 
  42.     cout<<heap[2].character<<endl; 
  43.     cout<<heap[3].character<<endl;*/  
  44.       
  45.     for(int i=currentSize/2;i>=1;i--){  
  46.         HNode y=heap[i];  
  47.         int ci=2*i;  
  48.         while(ci<=currentSize){  
  49.             if(ci<currentSize && heap[ci].num > heap[ci+1].num){  
  50.                 ci++;  
  51.             }  
  52.             if(y.num < heap[ci].num) break;  
  53.             heap[ci/2]=heap[ci];  
  54.             ci*=2;  
  55.         }  
  56.         heap[ci/2]=y;  
  57.     }  
  58.     /*cout<<heap[1].character<<endl; 
  59.     cout<<heap[2].character<<endl; 
  60.     cout<<heap[3].character<<endl;*/  
  61. }  
  62.   
  63. MinHeap& MinHeap::Insert(const HNode * x){  
  64.     //將x插入到最小堆中   
  65.     int i=++currentSize;  
  66.     while(i!=1 && x->num<heap[i/2].num){  
  67.         heap[i]=heap[i/2];  
  68.         i/=2;  
  69.     }  
  70.     heap[i]=*x;  
  71.     return *this;  
  72. }  
  73.   
  74. MinHeap& MinHeap::DeleteMax(HNode *x){  
  75.     //cout<<heap[1].character<<"  shjhjdf"<<endl;   
  76.   
  77.     *x=heap[1];  
  78.     HNode y=heap[currentSize--]; //取得當前堆中最後一個元素   
  79.     int i=1;  
  80.     int ci=2; //使ci爲i的子節點   
  81.     while(ci<=currentSize){  
  82.         if(ci<currentSize && heap[ci].num>heap[ci+1].num){  
  83.             ci++; //使ci爲i的子節點   
  84.         }  
  85.         if(y.num<=heap[ci].num) break//此時y可以放在i位上   
  86.         heap[i]=heap[ci];  
  87.         i=ci;  
  88.         ci*=2;  
  89.     }  
  90.     heap[i]=y;  
  91.     //cout<<x->character<<"  sdf"<<x->num<<endl;   
  92.     return *this;  
  93. }  
  94.   
  95.   
  96.   
  97.   
  98.   
  99. struct MaxHeap{  
  100.     int currentSize, maxSize;  
  101.     int *heap;  
  102.   
  103.     MaxHeap(int max);  
  104.     int Size() const {return currentSize;}  
  105.     int Max(){return heap[1];}  
  106.     MaxHeap& Insert(const int & x);  
  107.     MaxHeap& DeleteMax(int & x);  
  108.     void Initialize(int a[],int size,int arraySize);  
  109.     void levelOrder();  
  110.     void sort();  
  111.   
  112. };  
  113.   
  114. void MaxHeap::sort(){  
  115.     int x;  
  116.     int size=currentSize;  
  117.     for(int i=currentSize;i>1;i--){  
  118.         DeleteMax(x);  
  119.         heap[i]=x;  
  120.     }  
  121.     for(int n=1;n<size;n++){  
  122.         cout<<heap[n]<<",";  
  123.     }  
  124.     cout<<heap[size]<<endl;  
  125. }  
  126.   
  127. MaxHeap::MaxHeap(int max){  
  128.     maxSize=max;  
  129.     heap=new int[max+1]; //這個數組用於存儲最大堆,但要注意:該數組的0號位空置不用,即從1號位計起   
  130.     currentSize=0;  
  131. }  
  132.   
  133. MaxHeap& MaxHeap::Insert(const int &x){  
  134.     //將x插入到最大堆中   
  135.     int i=++currentSize;  
  136.     while(i!=1 && x>heap[i/2]){  
  137.         heap[i]=heap[i/2];  
  138.         i/=2;  
  139.     }  
  140.     heap[i]=x;  
  141.     return *this;  
  142. }  
  143.   
  144. MaxHeap& MaxHeap::DeleteMax(int &x){  
  145.     x=heap[1];  
  146.     int y=heap[currentSize--]; //取得當前堆中最後一個元素   
  147.     int i=1;  
  148.     int ci=2; //使ci爲i的子節點   
  149.     while(ci<=currentSize){  
  150.         if(ci<currentSize && heap[ci]<heap[ci+1]){  
  151.             ci++; //使ci爲i的子節點   
  152.         }  
  153.         if(y>=heap[ci]) break//此時y可以放在i位上   
  154.         heap[i]=heap[ci];  
  155.         i=ci;  
  156.         ci*=2;  
  157.     }  
  158.     heap[i]=y;  
  159.     return *this;  
  160. }  
  161.   
  162. void MaxHeap::Initialize(int a[],int size,int arraySize){  
  163.     delete [] heap;  
  164.     heap=a;  
  165.     currentSize=size;  
  166.     maxSize=arraySize;  
  167.       
  168.     for(int i=currentSize/2;i>=1;i--){  
  169.         int y=heap[i];  
  170.         int ci=2*i;  
  171.         while(ci<=currentSize){  
  172.             if(ci<currentSize && heap[ci]<heap[ci+1]){  
  173.                 ci++;  
  174.             }  
  175.             if(y>heap[ci]) break;  
  176.             heap[ci/2]=heap[ci];  
  177.             ci*=2;  
  178.         }  
  179.         heap[ci/2]=y;  
  180.     }  
  181. }  
  182.   
  183. void MaxHeap::levelOrder(){  
  184.     int count=0;  
  185.     queue<int> Q;  
  186.     int p=1;  
  187.     Q.push(1);  
  188.     int left,right;  
  189.     while(!Q.empty()){  
  190.         count++;  
  191.         p=Q.front();  
  192.         Q.pop();  
  193.         cout<<heap[p];  
  194.         if(count<currentSize) cout<<",";  
  195.         left=p*2;  
  196.         if(left<=currentSize) Q.push(left);        
  197.         right=left+1;  
  198.         if(right<=currentSize) Q.push(right);  
  199.     }  
  200.     cout<<endl;  
  201. }  
  202.   
  203. struct Node{  
  204.     Node(int x){data=x; left=right=0;}  
  205.     int data;  
  206.     Node *left;  
  207.     Node *right;  
  208. };  
  209.   
  210. void Insert(Node *node,const int & x){  //將元素x插入以node爲根的二叉搜索樹中   
  211.     Node *p=node;  
  212.     Node *pp=0; //pp爲p的父節點   
  213.     while(p){  
  214.         pp=p;  
  215.         if(x<p->data) p=p->left;  
  216.         else   
  217.             if(x>p->data) p=p->right;  
  218.             else {}//出現重複    
  219.     }  
  220.     Node *y=new Node(x);  
  221.     if(node){ //不是空樹   
  222.         if(pp->data>x){  
  223.             pp->left=y;  
  224.         }  
  225.         else{  
  226.             pp->right=y;  
  227.         }  
  228.     }  
  229.     else{  
  230.     //  node=y;   
  231.     }  
  232.   
  233. }  
  234.   
  235. void preOrder(Node *root,int num){  
  236.     if(root){  
  237.         cc++;  
  238.         cout<<root->data;  
  239.         if(cc<num)  
  240.             cout<<",";  
  241.         preOrder(root->left,num);  
  242.         preOrder(root->right,num);  
  243.     }  
  244. }  
  245.   
  246. void inOrder(Node *root,int num){  
  247.     if(root){  
  248.         inOrder(root->left,num);  
  249.         cc++;  
  250.         cout<<root->data;  
  251.         if(cc<num)  
  252.             cout<<",";          
  253.         inOrder(root->right,num);  
  254.     }  
  255. }  
  256.   
  257. void preOrder(HNode *root,int number,string s){ //num爲指令,1代表爲左節點,2代表爲右節點,0代表爲根節點   
  258.     if(root){  
  259.         if(number==1){root->str=s+"0";}    
  260.         if(number==2){root->str=s+"1";}  
  261.         //cout<<number<<" "<<root->character<<"   "<<root->str<<endl;   
  262.         preOrder(root->left,1,root->str);  
  263.         preOrder(root->right,2,root->str);  
  264.     }  
  265. }  
  266.   
  267.   
  268.   
  269.   
  270. void main(){  
  271.     cout<<"Input1"<<endl;  
  272.     MaxHeap *mh=new MaxHeap(25);  
  273.     int a[25];  
  274.     int b[25];  
  275.     HNode c[25];  
  276.     int i;  
  277.     cin>>i;  
  278.     int count=0;  
  279.     for(;i!=0;cin>>i){  
  280.         count++;  
  281.         a[count]=i;  
  282.         b[count]=i;  
  283.         c[count].num=i;  
  284.         c[count].character='A'+count-1;  
  285.         c[count].str="";  
  286.         c[count].left=0;  
  287.         c[count].right=0;  
  288.     }  
  289.     cout<<"Output"<<endl;  
  290.     mh->Initialize(a,count,25);  
  291.     mh->levelOrder();  
  292.     mh->sort();  
  293.       
  294.     Node *node=new Node(b[1]);  
  295.   
  296.     for(int n=2;n<=count;n++){  
  297.         Insert(node,b[n]);  
  298.     }  
  299.   
  300.     preOrder(node,count);  
  301.     cout<<endl;  
  302.     cc=0;  
  303.     inOrder(node,count);  
  304.     cout<<endl;  
  305.     cc=0;  
  306.       
  307.     MinHeap *minh=new MinHeap(25);  
  308.     minh->Initialize(c,count,25);  
  309.   
  310.     HNode *arr[25];  
  311.     int count2=0;  
  312.     HNode *x,*y;  
  313.     for(int nn=1;nn<count;nn++){  
  314.         x=new HNode;   
  315.         y=new HNode;   
  316.         minh->DeleteMax(x);  
  317.         if(x->character!='0'){  
  318.             arr[count2]=x;  
  319.             count2++;  
  320.         }  
  321.         //cout<<x->num<<"  "<<x->character<<endl;   
  322.         minh->DeleteMax(y);  
  323.         if(y->character!='0'){  
  324.             arr[count2]=y;  
  325.             count2++;  
  326.         }  
  327.         //cout<<y->num<<"  "<<y->character<<endl;   
  328.         HNode *z=new HNode;  
  329.         z->character='0';  
  330.         z->num=x->num+y->num;  
  331.         z->left=x;  
  332.         z->right=y;  
  333.         z->str="";  
  334.         x=z;  
  335.         minh->Insert(x);  
  336.     }  
  337.   
  338.     preOrder(x,0,"");  
  339.   
  340.     for(i=1;i<count;i++){  
  341.         HNode *t=arr[i];  
  342.         for(int n=i-1;n>=0 && arr[n]->character > t->character;n--){  
  343.             arr[n+1]=arr[n];  
  344.         }  
  345.         arr[n+1]=t;  
  346.     }  
  347.       
  348.     for(i=0;i<count-1;i++){  
  349.         cout<<arr[i]->character<<":"<<arr[i]->str<<",";  
  350.     }  
  351.     cout<<arr[count-1]->character<<":"<<arr[count-1]->str<<endl;  
  352.     cout<<endl;  
  353.       
  354.     cout<<"End"<<endl;  
  355.   
  356.   
  357. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章