Einzelnen Beitrag anzeigen

tigerman33

Registriert seit: 30. Jul 2005
Ort: München
423 Beiträge
 
Delphi 2005 Professional
 
#8

Re: Build Heap And Heapsort Without Array

  Alt 12. Mai 2006, 18:10
Hi Silvia,

I'm not sure if this will solve your problem, but if I read your code right you neglect to determine which one is the larger of the child nodes before you swap with the root. That might look something like this:

Delphi-Quellcode:
procedure TBinTree.BuildHeap;
var Larger: TBinTree;
begin
  // Lazy evaluation of boolean expressions needs to be switched on for this to work
  Larger := Left;
  if not Assigned(Larger) then
    Larger := Right else
    if Assigned(Right) and (Left.Value < Right.Value) then
      LargerSon := Right;

  if Assigned(Larger) and (Value < Larger.Value) then begin
    Exchange(Larger,self);
    Larger.BuildHeap;
  end;
end;
If that doesn't do it, we'll need to look again.
Christian
Der Computer hilft mir, Probleme zu lösen, die ich ohne Computer nicht hätte.
  Mit Zitat antworten Zitat