• Adjust the function
  • Konteynerlar bilan ishlash algoritmlari.
  • Mechanical Implementation of Priority Queues




    Download 0,71 Mb.
    bet7/9
    Sana24.05.2024
    Hajmi0,71 Mb.
    #252108
    1   2   3   4   5   6   7   8   9
    Bog'liq
    Nishonova G (D2 M2)

    4.3 Mechanical Implementation of Priority Queues
    Review the plug insertion and deletion.
    Insertion of the heap: Adjust the data in the stacked tail, adjust the pile up.
    Remove of a pile: Exchange the top elements and tail elements, and adjust the pile down.
    Do not use the imitation function:
    Adjust the function:


    1. #pragma once

    2. #include

    3. #include

    4. #include

    5. #include

    6. using namespace std;



    7. namespace my{

    8. template>

    9. class priority_queue{

    10. public:

    11. priority_queue(){};

    12. template

    13. priority_queue(inputiterator first, inputiterator last){

    14. while (first != last){

    15. push(*first);

    16. first++;

    17. }

    18. }

    19. bool empty(){

    20. return _con.empty();

    21. }

    22. size_t size(){

    23. return _con.size();

    24. }

    25. T& top(){

    26. return _con.front();

    27. }

    28. const T& top()const{

    29. return _con.front();

    30. }

    31. void push(const T& val){

    32. _con.push_back(val);

    33. Adjustup(_con.size() - 1);

    34. }

    35. void pop(){

    36. swap(_con[0], _con[_con.size() - 1]);

    37. // Less an element is Size reduction, after exchange, delete the last element directly

    38. _con.pop_back();

    39. Adjustdown(0);

    40. }

    41. private:

    42. void Adjustup(int child){

    43. int parent = (child - 1) / 2;

    44. while (child > 0){

    45. if (_con[child] > _con[parent]){

    46. swap(_con[child], _con[parent]);

    47. child = parent;

    48. parent = (child - 1) / 2;

    49. }

    50. else{

    51. break;

    52. }

    53. }

    54. }



    55. void Adjustdown(int parent){

    56. int child = parent * 2 + 1;

    57. / / Can not be reduced, if there is no element size () is 0, type size_t, minus 1 is the maximum number (positive), will enter the loop

    58. while ((size_t)child < _con. size() ){

    59. if (child + 1 < _con.size() && _con[child + 1] > _con[child]){

    60. child++;

    61. }

    62. if (_con[child]>_con[parent]){

    63. swap(_con[child], _con[parent]);

    64. parent = child;

    65. child = parent * 2 + 1;

    66. }

    67. else{

    68. break;

    69. }

    70. }

    71. }



    72. Container _con;

    73. };



    74. void test_priority_queue(){

    75. int array[] = { 8, 4, 6, 2, 10 };

    76. priority_queue pq(array, array + sizeof(array) / sizeof(int));

    77. cout << pq.size() << endl;



    78. while (!pq.empty()){

    79. cout << pq.top() << " ";

    80. pq.pop();

    81. }

    82. cout << endl;



    83. }

    84. }

    Imitation function: The priority queue achieves the establishment of large roots or small roots through the imitation function.
    Adjusting the establishment of a large root or a small root, just changing the adjustment function. The father's node is more than smaller than less than that of the child. How to achieve it through the imitation function?

    How to modify the adjustment function:


    1. #pragma once

    2. #include

    3. #include

    4. #include

    5. #include

    6. using namespace std;



    7. namespace my{

    8. template

    9. struct less{

    10. bool operator()(const T& left,const T& right){

    11. return left < right;

    12. }

    13. };

    14. template

    15. struct greater{

    16. bool operator()(const T& left, const T& right){

    17. return left>right;

    18. }

    19. };





    20. template,class Compare=less>

    21. class priority_queue{

    22. public:

    23. priority_queue(){};

    24. template

    25. priority_queue(inputiterator first, inputiterator last){

    26. while (first != last){

    27. push(*first);

    28. first++;

    29. }

    30. }

    31. bool empty(){

    32. return _con.empty();

    33. }

    34. size_t size(){

    35. return _con.size();

    36. }

    37. T& top(){

    38. return _con.front();

    39. }

    40. const T& top()const{

    41. return _con.front();

    42. }

    43. void push(const T& val){

    44. _con.push_back(val);

    45. Adjustup(_con.size() - 1);

    46. }

    47. void pop(){

    48. swap(_con[0], _con[_con.size() - 1]);

    49. // Less an element is Size reduction, after exchange, delete the last element directly

    50. _con.pop_back();

    51. Adjustdown(0);

    52. }

    53. private:

    54. void Adjustup(int child){

    55. int parent = (child - 1) / 2;

    56. Compare com;

    57. while (child){

    58. if (com(_con[parent],_con[child])){

    59. swap(_con[child], _con[parent]);

    60. child = parent;

    61. parent = (child - 1) / 2;

    62. }

    63. else{

    64. break;

    65. }

    66. }

    67. }



    68. void Adjustdown(int parent){

    69. int child = parent * 2 + 1;

    70. // Define an object

    71. Compare com;

    72. / / Can not be reduced, if there is no element size () is 0, type size_t, minus 1 is the maximum number (positive), will enter the loop

    73. while ((size_t)child < _con. size() ){



    74. if (child + 1 < _con.size() && com(_con[child], _con[child + 1])){

    75. child++;

    76. }

    77. if (com(_con[parent], _con[child])){

    78. swap(_con[child], _con[parent]);

    79. parent = child;

    80. child = parent * 2 + 1;

    81. }

    82. else{

    83. break;

    84. }

    85. }

    86. }



    87. Container _con;

    88. };



    89. void test_priority_queue(){

    90. int array[] = { 8, 4, 6, 2, 10 };

    91. priority_queue,greater> pq(array, array + sizeof(array) / sizeof(int));

    92. cout << pq.size() << endl;



    93. while (!pq.empty()){

    94. cout << pq.top() << " ";

    95. pq.pop();

    96. }

    97. cout << endl;



    98. }

    99. }

    4. Konteynerlar bilan ishlaydigan algoritmlar.
    Funktorlarni qo`llash
    Konteynerlar bilan ishlash algoritmlari. Barcha konteynerlar umumiy yondashuvi tufayli, amalda qiziqtirgan asosiy algoritmlar konteynerlarning har qanday turi uchun qo‘llanilishi mumkin bo‘lgan umumiy shaklda amalga oshirilishi mumkinligi tufayli STL konteynerlar uzoq amaliy foydalanish uchun bir arzigulik ixtiro bo‘ldi.
    Algoritmlar kutubxonaning eng katta va ommabop qismidir. Juda ko‘p algoritmlar mavjudki, ularning hammasini batafsil bayon qilish uchun katta kitob kerak. Quyida ularni guruhlarga ajratib, ularning nomlari bilan ataymiz (ularning hammasi emas) va ulardan faqat ayrimlari misol tariqasida keltiramiz.
    for_each()algoritmi. for_each() eng ko‘p ishlatiladigan algoritmdir. Konteyner elementlar guruhi (ehtimol, barcha elementlari) uchun ketma-ket harakatni amalga oshiradi. U har qanday STL konteynerda foydalanish mumkin. Quyidagi 4.9-dasturda for_each() algoritm massiv va vektor uchun ishlatilgan.
    4.9-dastur. for_each() algoritmidan foydalanish.

    // Created by MBBahodir #include "stdafx.h" #include #include #include


    using namespace std;





    inline ostream& operator <<( ostream& out, const vector< unsigned > & obj ) { cout << "< ";

    for( auto& p: obj ) cout << p << " ";


    return out << ">";
    }
    void pow2( unsigned& i ) { i *= i; } int main( void ) {
    const int examples = 4;
    for( int i = 0; i < examples; i++ ) {
    unsigned ai[] = { 1, 2, 3, 4 , 5, 6, 7, 8, 9 }, ni = sizeof( ai ) / sizeof( ai[ 0 ] );
    vector< unsigned > vi( ai, ai + ni ); cout << vi;
    switch( i ) { case 0:
    for_each( vi.begin(), vi.end(), pow2 ); cout << " => " << vi << endl;
    break; case 1:
    for_each( ai, ai + ni, pow2 );
    cout << " => " << vector< unsigned >( ai, ai + ni ) << endl; break;
    case 2:
    for( auto& i : ai ) pow2( i );
    cout << " => " << vector< unsigned >( ai, ai + ni ) << endl; break;
    case 3:
    for_each( vi.begin() + 2, vi.end() - 2, pow2 ); cout << " => " << vi << endl;
    break;
    }
    }
    system("pause"); return 0;
    }



    4.9-dastur.Output.




    < 1 2 3 4 5 6 7 8 9 > => < 1 4 9 16 25 36 49 64 81 >


    < 1 2 3 4 5 6 7 8 9 > => < 1 4 9 16 25 36 49 64 81 >
    < 1 2 3 4 5 6 7 8 9 > => < 1 4 9 16 25 36 49 64 81 >
    < 1 2 3 4 5 6 7 8 9 > => < 1 2 9 16 25 36 49 8 9 >


    Dasturning birinchi global (auto &x : ...) takrorlanish fragmenti ishlashini ko‘rsatilgan (C++11 standarti tomonidan joriy qilingan). Bu kabi fragmentni massivlar va konteynerlarga ham qo‘llanilishi mumkin (bu vektorni oqimga chiqarish operatori variant funksiyasida ko‘rsatilgan). Bu takrorlanish fragmenti aslida STL kutubxona yoki algoritmlarni qismi emas, lekin u for_each() algoritm bilan bir xil taʻsir ko‘rsatadi, yaʻni to‘plam barcha elementlar uchun izchil qo‘llash mumkin.
    Bu yuqoridagi dastur barcha algoritmlarni berilgan interval asosida tashkiliy asosiy mantiq qismini ko‘rsatadi (shart emas, barcha konteynerlar). Boshi va oxiri bilan cheklangan iteratorga (ko‘pincha, birinchi berilgan 2 ta parametrlar) funksiya, funktor va predikat (bu funksiyalarga - qaysidir elementlari bo‘yicha saralash imkoni bo‘lsa va mantiqiy qiymat qaytaradigan funksiyalar kiradi) navbat bilan qo‘llaniladi.
    Find(). Navbatdagi algoritm – bu find(). Bu nomidan maʻlumki, to‘plamdagi elementlarni qidirish funksyasidir. Eʻtibor bersak, juda ko‘p konteynerlar bunday find() – funksiyasiga ega va u obʻyekt uchun 
    Download 0,71 Mb.
    1   2   3   4   5   6   7   8   9




    Download 0,71 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Mechanical Implementation of Priority Queues

    Download 0,71 Mb.