modelo de extraxion heap
Leo
Posted on March 15, 2024
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);
}
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();
}
💖 💪 🙅 🚩
Leo
Posted on March 15, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.