Thema: Delphi Pointer "Bäume"

Einzelnen Beitrag anzeigen

Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#4

Re: Pointer "Bäume"

  Alt 9. Mär 2004, 21:31
Hallo,
ist es das was Du meinst
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type

  PNode = ^TNode;
  TNode = Record
          Left : PNode;
          Right : PNode;
          Number : Integer;
          end;

  TSeachCallBack = procedure(Node : PNode) of object;

  TForm1 = class(TForm)
    Panel1: TPanel;
    PbNodes: TPaintBox;
    Edit1: TEdit;
    btnAdd: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    procedure btnAddClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
    FRoot : PNode;
    procedure AddToTree(aNumber: Integer; var Node: PNode);
    function SearchInTree(Node : PNode; aNumber : Integer; SCB : TSeachCallBack) : PNode;
    procedure DisposeNodes(Node : PNode);
    procedure SeachCallBack(Node : PNode);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AddToTree(aNumber: Integer;var Node: PNode);
begin
  If Node=Nil then
    begin
    New(Node);
    With Node^do
      begin
      Number:=aNumber;
      Left:=Nil;
      Right:=Nil;
      end;
    end
      else
        begin
        If aNumber<Node^.Number then
          AddToTree(aNumber,Node^.Left)
            else
              AddToTree(aNumber,Node^.Right);
        end;
end;

procedure TForm1.btnAddClick(Sender: TObject);
begin
  AddToTree(StrToInt(Edit1.Text),FRoot);
  Edit1.Clear;
end;

procedure TForm1.DisposeNodes(Node: PNode);
begin
  If Node^.Left<>Nil then
    DisposeNodes(Node^.Left);
  If Node^.Right<>Nil then
    DisposeNodes(Node^.Right);
  Dispose(Node);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  If FRoot<>Nil then
    DisposeNodes(FRoot);
end;

{SearchInTree findet alle Vorkommen von aNumber im Baum. Wenn aNumber gefunden
wird, wird die procedure SCB mit dem entsprechenden Node im Parameter aufgerufen}

function TForm1.SearchInTree(Node: PNode; aNumber: Integer;SCB : TSeachCallBack): PNode;
begin
  If Node<>Nil then
    begin
    If Node^.Number=aNumber then
      SCB(Node)
        else
          begin
          SearchInTree(Node^.Left,aNumber,SCB);
          SearchInTree(Node^.Right,aNumber,SCB);
          end;
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);

begin
  SearchInTree(FRoot,StrToInt(Edit1.Text),SeachCallBack);
end;

procedure TForm1.SeachCallBack(Node: PNode);
begin
  ShowMessage(IntToStr(Node^.Number));
end;

end.
I come from outer space to save the human race
  Mit Zitat antworten Zitat