Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class Kopiec{
  7. private:
  8. int el; //dodawany element
  9. int n; //rozmiar tablicy
  10. int *tab;
  11. int wielkosckopca;
  12. public:
  13. Kopiec();
  14. void zatapianie(int el, int n);
  15. void wynurzanie(int el, int n);
  16. void wypisz(int n);
  17. void inserting(int wartosc);
  18. int first();
  19. int extract();
  20. virtual ~Kopiec(){}
  21. };
  22.  
  23. Kopiec::Kopiec(){
  24.  
  25. int wielkosckopca=11;
  26. tab = new int[20];
  27. tab[0]=1;
  28. tab[1]=12;
  29. tab[2]=11;
  30. tab[3]=10;
  31. tab[4]=11;
  32. tab[5]=8;
  33. tab[6]=5;
  34. tab[7]=3;
  35. tab[8]=7;
  36. tab[9]=2;
  37. tab[10]=9;
  38. }
  39.  
  40. void Kopiec::inserting(int wartosc)
  41. {
  42. tab[n+1] = wartosc;
  43. wynurzanie(el, n+1);
  44. n++;
  45. wielkosckopca++;
  46. }
  47.  
  48. int Kopiec::first()
  49. {
  50. if(isEmpty()==false){
  51. return tab[0];
  52. }
  53. }
  54.  
  55. int Kopiec::extract(){
  56. if(isEmpty()==false)
  57. {
  58. wielkosckopca--;
  59. int temp = tab[0];
  60. tab[0]=tab[wielkosckopca];
  61. //
  62. return temp;
  63. }
  64.  
  65. }
  66.  
  67. bool Kopiec::isEmpty()
  68. {
  69. if(wielkosckopca==0)
  70. return true;
  71. }
  72.  
  73. void Kopiec::zatapianie(int el, int n)
  74. {
  75. int l = 2*el+1;
  76. int r = 2*el+2;
  77. int wiekszy;
  78.  
  79. if((l<n)&&(tab[l]>tab[el]))
  80. {
  81. wiekszy=l;
  82. }
  83. else
  84. {
  85. wiekszy=el;
  86. }
  87.  
  88. if((r<n)&&(tab[r]>tab[wiekszy]))
  89. {
  90. wiekszy=r;
  91. }
  92.  
  93. if(wiekszy != el)
  94. {
  95. swap(tab[el],tab[wiekszy]);
  96. zatapianie(wiekszy, n);
  97. }
  98.  
  99. }
  100.  
  101. void Kopiec::wynurzanie(int el, int n)
  102. {
  103. int ojciec = (el-1)/2;
  104. if(el>1)
  105. {
  106. if(tab[el]>tab[ojciec])
  107. {
  108. swap(tab[el],tab[ojciec]);
  109. wynurzanie(ojciec,n);
  110. }
  111. }
  112. }
  113.  
  114. void Kopiec::wypisz(int n)
  115. {
  116. for(int i=0;i<n;i++)
  117. {
  118. cout << tab[i];
  119. }
  120. cout << endl;
  121. }
  122.  
  123. int main()
  124. {
  125. Kopiec k;
  126.  
  127. //k.wypisz(11);
  128. //k.wynurzanie(0,11);
  129. //k.wypisz(11);
  130. cout << "Hello world!";
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement