iliya785

heap

May 24th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.66 KB | None | 0 0
  1. var heap:array[1..100000] of longint;
  2.     i,j,n,m,z,c:longint;
  3. procedure build(i:longint);
  4. var l,r,max:longint;
  5.  begin
  6.   l:=i shl 1; r:=l+1; max:=i;
  7.    if (l<=n) and (heap[l] > heap[max]) then
  8.       max:=l;
  9.    if (r<=n) and (heap[r] > heap[max]) then
  10.       max:=r;
  11.    if (max <> i) then
  12.     begin
  13.      swap(heap[i],heap[max]);
  14.      build(max);
  15.     end;
  16.  end;
  17. procedure delete_max;
  18.  begin
  19.   swap(heap[1],heap[n]); dec(n);
  20.   build(1);
  21.  end;
  22. procedure add(a:longint);
  23.  begin
  24.   inc(n); heap[n]:=a; i:=n;
  25.    while (i > 1) and (heap[i] > heap[i shr 1]) do
  26.     begin
  27.      swap(heap[i],heap[i shr 1]);
  28.      i:=i shr 1;
  29.     end;
  30.  end;
  31. Begin
  32. end.
Advertisement
Add Comment
Please, Sign In to add comment