Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class Item>
- void d_ary_fixDown2(Item a[], int node, int n){
- int first_child = node*d + 1 - (d - 1);
- int last_child = node*d + 1;
- int max_child = first_child; /*Начнем с самого первого*/
- while(node*d + 1 - (d - 1) <= n){
- int j = node*d + 1 - (d - 1);
- /*Ищем максимального потомка среди всех d потомков узла node*/
- for(int i = first_child; i <= last_child; i++)
- if(a[i] > a[max_child])
- max_child = i;
- /*Если потомок max_child больше своего родителя, то меняем их местами*/
- //if(a[max_child] > a[(max_child + d - 2) / d])
- if(a[max_child] > a[node])
- std::swap(a[max_child], a[node]);
- node = max_child;
- first_child = node*d + 1 - (d - 1);
- last_child = node*d + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement