Data structures 如何在没有递归的情况下编写最大堆代码

Data structures 如何在没有递归的情况下编写最大堆代码,data-structures,heap,heapsort,binary-heap,Data Structures,Heap,Heapsort,Binary Heap,我在《算法导论》一书中编写了MAX-HEAPIFY(A,I)方法。现在我想使用while循环编写它,而不使用递归。你能帮我一下吗?你可以使用while循环,条件是你的i上面的解决方案是有效的,但我认为下面的代码更接近递归版本 (* Code TP compatible *) const maxDim = 1000; type TElem = integer; TArray = array[1..maxDim]of TElem procedure heapify(var A:TArr

我在《算法导论》一书中编写了MAX-HEAPIFY(A,I)方法。现在我想使用while循环编写它,而不使用递归。你能帮我一下吗?

你可以使用while循环,条件是你的i上面的解决方案是有效的,但我认为下面的代码更接近递归版本

(* Code TP compatible *)
const maxDim = 1000; 
type TElem = integer;
     TArray = array[1..maxDim]of TElem

procedure heapify(var A:TArray;i,heapsize:integer);
var l,r,largest,save:integer;
    temp:TElem;
(*i - index of node that violates heap property
  l - index of left child of node with index i
  r - index of right child of node with index i
  largest - index of largest element of the triplet (i,l,r) 
  save - auxiliary variable to save the value of i 
  temp - auxiliary variable used for swapping *)   
begin
  repeat
    l:=2*i;
    r:=2*i + 1;
    if(l <= heapsize) and (A[l] > A[i]) then
       largest:=l
    else 
       largest:=i;
    if(r <= heapsize) and (A[r] > A[largest]) then
       largest:=r;
    (*Now we save the value i to check properly the termination
     condition of repeat until loop
     The value of i will be modified soon in the if statement *)
    save:=i;
    if largest <> i then
    begin
      temp:=A[i];
      A[i]:=A[largest];
      A[largest]:=temp;
      i:=largest;
    end;
    until largest = save;
    (*Why i used repeat until istead of while ?
     because body of the called procedure will be executed 
     at least once *)
end;
(*代码TP兼容*)
常数maxDim=1000;
类型TELM=整数;
TArray=远程通信的数组[1..maxDim]
过程heapify(变量A:TArray;i,heapsize:integer);
变量l,r,最大,保存:整数;
温度:远程;
(*i-违反堆属性的节点索引
l-索引为i的节点左子节点的索引
r-索引为i的节点的右子节点的索引
最大-三重态(i,l,r)中最大元素的指数
save-用于保存i值的辅助变量
temp-用于交换的辅助变量*)
开始
重复
l:=2*i;
r:=2*i+1;
如果(la[i]),那么
最大:=l
其他的
最大:=i;
如果(ra[最大]),则
最大:=r;
(*现在我们保存值i以正确检查终端
重复直到循环的条件
i的值将很快在if语句中修改*)
保存:=i;
如果我
开始
温度:=A[i];
A[i]:=A[最大];
A[最大]:=温度;
i:=最大值;
结束;
直到最大=保存;
(*为什么我一直重复到现在?
因为将执行被调用过程的主体
至少一次*)
结束;
还有一件事,在沃思的算法+数据结构=程序中

可以找到没有递归的sift过程
但是我们应该引入布尔变量或break来消除goto语句

为什么在最后一行使用else和break?为了检查边界条件-如果元素位于正确的位置,那么它就不应该再执行heapify并退出循环。我想如果我们在
(* Code TP compatible *)
const maxDim = 1000; 
type TElem = integer;
     TArray = array[1..maxDim]of TElem

procedure heapify(var A:TArray;i,heapsize:integer);
var l,r,largest,save:integer;
    temp:TElem;
(*i - index of node that violates heap property
  l - index of left child of node with index i
  r - index of right child of node with index i
  largest - index of largest element of the triplet (i,l,r) 
  save - auxiliary variable to save the value of i 
  temp - auxiliary variable used for swapping *)   
begin
  repeat
    l:=2*i;
    r:=2*i + 1;
    if(l <= heapsize) and (A[l] > A[i]) then
       largest:=l
    else 
       largest:=i;
    if(r <= heapsize) and (A[r] > A[largest]) then
       largest:=r;
    (*Now we save the value i to check properly the termination
     condition of repeat until loop
     The value of i will be modified soon in the if statement *)
    save:=i;
    if largest <> i then
    begin
      temp:=A[i];
      A[i]:=A[largest];
      A[largest]:=temp;
      i:=largest;
    end;
    until largest = save;
    (*Why i used repeat until istead of while ?
     because body of the called procedure will be executed 
     at least once *)
end;