AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Build Heap And Heapsort Without Array

Ein Thema von sk.Silvia · begonnen am 1. Mai 2006 · letzter Beitrag vom 13. Mai 2006
Antwort Antwort
Benutzerbild von DGL-luke
DGL-luke

Registriert seit: 1. Apr 2005
Ort: Bad Tölz
4.149 Beiträge
 
Delphi 2006 Professional
 
#1

Re: Build Heap And Heapsort Without Array

  Alt 1. Mai 2006, 15:42
A heap can be very easily organized into an array, but if you use binary heaps, this type should be able to help you:

Delphi-Quellcode:
type
  PHeapNode = ^THeapNode;
  THeapNode = record
    Parent,Left,Right: PHeapNode;
    Value: Integer;
  end;
With this you should be able to build a heap:

Delphi-Quellcode:
function RandomHeap(depth: Integer; BaseNode: PHeapNode = nil): THeapNode
var i: Integer;
begin
if (BaseNode = nil) then
  begin
    Result := New(THeapNode)
  end
else
  begin
    Result := BaseNode;
    Result.Value := random(100);
  end;

if depth > 0 then
  begin
    Result.Left := New(THeapNode);
    Result.Left.Parent := BaseNode;
    ReandomHeap(depth-1,Result.Left);
   
    Result.Right := New(THeapNode);
    Result.Left.Parent := BaseNode;
    RandomHeap(depth-1,Result.Right);
  end;
end;

procedure MaxSort(Heap: THeapNode);
begin
// too lazy to write that now xD
end;
maybe I left out some pointer conversions, but that should be it.
And don't forget to free all memory you assign!
(When you make the record a class, you can implement this in the class)
Lukas Erlacher
Suche Grafiktablett. Spenden/Gebrauchtangebote willkommen.
Gotteskrieger gesucht!
For it is the chief characteristic of the religion of science that it works. - Isaac Asimov, Foundation I, Buch 1
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 03:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz