DEV Community

Leo
Leo

Posted on

modelo de extraxion heap

 int heap::tout(){

     assert(!empty());

     int x = a[0];
     int i = 0;
     a[0] = a[s];
     s--;

     while (i <= parent(s) && a[i] < a[maxChild(i)]){

         int m = maxChild(i);

         int c = a[m];
         a[i] = a[m];
         a[m] = c;

         i = m;
     }

     return x;
 }

 int heap::maxChild(int i){

     assert(i<=parent(s));

     if (right(i) > s) return left(i);
     return (a[left(i)] > a[right(i)]) ? left(i) : right (i);
 }
Enter fullscreen mode Exit fullscreen mode
int main() {

  srand((unsigned) time(nullptr));

  int n = 100;

  heap m(n);

  for (int i = 0; i < 10; i++){

    int x = rand() % n + 1;

    cout << "Agregar " << x << ": ";

    m.ins(x);
    m.print();

  }
      cout << "Extraer " << m.tout() <<": ";

    m.print();

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)