zoukankan      html  css  js  c++  java
  • 基于完全二叉树的极大堆的上下漂移实现

     1 //基于完全二叉树的极大堆的上下漂移实现 
     2 //INT_MAX极大数 INT_MIN极小数 
     3 #include <cstdlib>
     4 #include <iostream>
     5 #include <vector> 
     6 #define N 11
     7 
     8 using namespace std;
     9 
    10 typedef int  T;     //!!!!!!!
    11 typedef vector<T>::iterator TP;//!!!!!
    12 
    13 void shiftup(TP a,int n){
    14      for(int p=n/2;p>0&&(a[p]<a[n]);p=(n=p)/2)//向量可以这样索引 
    15       std::swap(a[p],a[n]); 
    16 } 
    17      
    18      
    19 void shiftdown(TP a,int n){
    20      for(int p=1,bc=2;bc<=n;bc=2*p){
    21        if(bc<n&&(a[bc]<a[bc+1])) ++bc;
    22        if(a[p]<a[bc]) {std::swap(a[bc],a[p]);p=bc;}
    23        else break;      
    24              }
    25      }
    26  
    27  
    28  
    29  
    30  int main(){  
    31  vector<int> vv;
    32  vv.reserve(2*N);
    33 vv.push_back(INT_MAX); //INT_MAX极大数 INT_MIN极小数 
    34  //vv[0]=INT_MAX;//!!! 
    35  //cout<<vv[0]<<endl;
    36  //system("PAUSE");
    37 //    return 1;
    38  //for(int i=1;i<21;i++)
    39  vv.push_back(88);
    40  vv.push_back(75);
    41  vv.push_back(83);
    42  vv.push_back(65);
    43  vv.push_back(55);
    44  vv.push_back(63);
    45  vv.push_back(53);
    46  vv.push_back(45);
    47  vv.push_back(35);
    48  //vv[2]=14;//用地址+【】进行索引 
    49  TP first=vv.begin();
    50  //!!!定义向量的指针用该语句,而非用取地址符号来定义 
    51 //cout<<vv[0]<<endl;  
    52 for(int i=0;i<N-1;i++)
    53    cout<<vv[i]<<endl;
    54  
    55  int o=91;
    56 //极大堆的shiftup操作实现 
    57 cout<<"now add value to MAX_HEAP:"<<o<<endl;
    58 vv.push_back(o);
    59 shiftup(first,N-1);
    60 cout<<"after shiftup operation:"<<endl; 
    61 for(int i=0;i<N;i++)
    62    cout<<vv[i]<<endl;
    63    
    64 std::swap(vv[N-1],vv[1]);
    65 shiftdown(first,N-1);   
    66 cout<<"after shiftdown operation:"<<endl;
    67    for(int i=0;i<N;i++)
    68    cout<<vv[i]<<endl;
    69    
    70     system("PAUSE");
    71     return 1;
    72 }
    73 
    74  
  • 相关阅读:
    IntelliJ IDEA创建Java项目
    Oracle常见五个服务的作用
    Windows2008开启telnet客户端命令
    整理jQuery操作select控件常用功能代码
    Asp.Net程序报错
    Oracle中start with connect by prior用法
    Spring中无法访问resources目录下页面或静态资源
    对搜狗输入法的个人评价
    课堂练习寻找水王
    典型用户分析及用户场景分析
  • 原文地址:https://www.cnblogs.com/jieforever/p/4671252.html
Copyright © 2011-2022 走看看