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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值