zoukankan      html  css  js  c++  java
  • C++之Binary Heap/Max Heap


      1 #include <iostream>
      2 #include <time.h>
      3 #include <random>
      4 
      5 using namespace std;
      6 
      7 //Binary Heap; Max Heap;
      8 
      9 class BinaryHeap
     10 {
     11 public:
     12     BinaryHeap();
     13     BinaryHeap(int capacity);
     14     ~BinaryHeap();
     15 
     16     int insert(int value);
     17     int getIndex(int value);
     18     int removeRoot();
     19     void print();
     20     bool isEmpty();
     21 
     22 private:
     23     void sortUp(int start);
     24     void sortDown(int start, int end);
     25 
     26     int *heap;
     27     int capacity;
     28     int size ;
     29 };
     30 
     31 BinaryHeap::BinaryHeap()
     32 {
     33     this->size = 0;
     34     this->capacity = 50;
     35     heap = new int[this->capacity];
     36 }
     37 
     38 BinaryHeap::BinaryHeap(int capacity)
     39 {
     40     this->size = 0;
     41     this->capacity = capacity;
     42     heap = new int[this->capacity];
     43 }
     44 
     45 BinaryHeap::~BinaryHeap()
     46 {
     47     this->size = 0;
     48     this->capacity = 0;
     49     delete[] heap;
     50 }
     51 
     52 int BinaryHeap::insert(int value)
     53 {
     54 
     55     if (this->size==this->capacity) //The heap is full
     56     {
     57         return -1;
     58     }
     59     heap[this->size] = value;
     60     this->sortUp(this->size);
     61     this->size++;
     62     return 0;
     63 }
     64 
     65 int BinaryHeap::getIndex(int value)
     66 {
     67     for (int i = 0; i < this->size; i++)
     68     {
     69         if (value==this->heap[i])
     70         {
     71             return i;
     72         }
     73     }
     74     return -1;
     75 }
     76 
     77 int BinaryHeap::removeRoot()
     78 {
     79 
     80     int index = 0;
     81     if (this->size==0)
     82     {
     83         return 1;//The heap is empty
     84     }
     85 
     86     this->heap[index] = this->heap[--this->size];
     87     this->sortDown(index, this->size - 1);
     88 
     89     return 0;
     90 }
     91 
     92 void BinaryHeap::print()
     93 {
     94     for (int i = 0; i < this->size; i++)
     95     {
     96         cout << "No." << i + 1 << " : " << heap[i] << " " << endl;;
     97     }
     98 }
     99 
    100 
    101 
    102 bool BinaryHeap::isEmpty()
    103 {
    104     if (this->size == 0)
    105     {
    106         return true;
    107     }
    108     else
    109     {
    110         return false;
    111     }
    112 }
    113 
    114 
    115 
    116 void BinaryHeap::sortUp(int start)
    117 {
    118     int c = start;  //The location of current node
    119     int p = (c - 1) / 2; //The location of parent node
    120     int temp = heap[c]; //The value of current node
    121 
    122     while (c>0)
    123     {
    124         if (heap[p] > temp)
    125         {
    126             break;
    127         }
    128         else
    129         {
    130             heap[c] = heap[p];
    131             c = p;
    132             p = (p - 1) / 2;
    133         }
    134     }
    135     heap[c] = temp;
    136 }
    137 
    138 void BinaryHeap::sortDown(int start, int end)
    139 {
    140     int c=start; //the location of current node
    141     int l = 2*c + 1; //The location of left child 
    142     int temp = heap[c]; //The value of current node
    143 
    144     while (l <= end)
    145     {
    146         if (l<end && heap[l]<heap[l+1])
    147         {
    148             l++;  //Choose the bigger one between left child and right child
    149         }
    150         if (temp>=heap[l])
    151         {
    152             break;
    153         }
    154         else
    155         {
    156             heap[c] = heap[l];
    157             c = l;
    158             l = 2 * l + 1;
    159         }
    160     }
    161     heap[c] = temp;
    162 }
    163 
    164 
    165 int main()
    166 {
    167     
    168 
    169     BinaryHeap *b = new  BinaryHeap(50);
    170     
    171     default_random_engine random(time(NULL)); //C++ 11
    172     uniform_int_distribution<int> num(0, 999);//C++ 11
    173 
    174     cout << "Insert 50 randon number which between 0~999 " << endl ;
    175     for (int i = 0; i <50; i++)
    176     {
    177         b->insert(num(random));
    178     }
    179 
    180     cout << "Print: " << endl;
    181     b->print();
    182 
    183     cout << endl << endl;
    184     cout << "Is the heap empty? " << endl;
    185     cout << boolalpha << b->isEmpty() << endl << endl;
    186 
    187     cout << "Remove" << endl;
    188     switch (int n=b->removeRoot())
    189     {
    190     case 0:
    191         cout << "Success! The root node has been removed!" << endl; break;
    192     case 1:
    193         cout << "Heap is empty! " << endl; break;
    194     }
    195     cout << endl;
    196 
    197     cout << "Print: " << endl;
    198     b->print();
    199     
    200     
    201 
    202     system("pause");
    203     return 0;
    204 }
  • 相关阅读:
    数据库30条规范
    数据库索引原理
    HashMap的实现原理
    Google 和 Baidu 常用的搜索技巧
    Arrays工具类十大常用方法
    fastjson将json格式null转化空串
    SolrCloud的介绍
    网页背景图片自适应浏览器大小
    addlinkedserver
    常用
  • 原文地址:https://www.cnblogs.com/0Nullptr/p/6869915.html
Copyright © 2011-2022 走看看