Einzelnen Beitrag anzeigen

MPeters

Registriert seit: 20. Nov 2022
9 Beiträge
 
#10

AW: Freepascal AVLTree, Binären Baum allgemein verstehen?

  Alt 21. Mär 2023, 12:51
Ich habe das Problem jetzt so hier gelöst.

Delphi-Quellcode:
unit ugentree;

interface

uses classes;

type
  TCompareStrFunc = function(s1,s2: String): Integer;
  TCompareIntFunc = function(i1,i2: Integer): Integer;
  TCompareFunc = function(p1,p2: String): Integer;

  TNode = class;

  TNodeData = class(TObject)
    FKey: Integer;
    FKeyStr: String;
    FData: Pointer;

    constructor Create(aKey: Integer; aKeyStr: String);
    property Key: Integer read FKey write FKey;
    property KeyStr: String read FKeyStr write FKeyStr;
   // property Data: Pointer read FData write FData;
   // property Node: TNode read FNode write FNode;
  end;

  TNodes = class(TList)
  private
    function GetNodes(Index: Integer): TNode;
  public
    function Add(aNode: TNode): Integer;
    property Nodes[Index: Integer]: TNode read GetNodes;
  end;

  TNode = class(TObject)
  private
    FCompare: TCompareFunc;
    FCaption: String;
    FParent: TNode;
    FSubnodes: TNodes;

    function GetCount: Integer;
    function GetNodes(Index: Integer): TNode;
  public
    constructor Create(aParent: TNode; CompareFunc: TCompareFunc; aCaption: String);
    destructor Destroy; override;

    procedure Add(ParentNode: TNode; aNode: TNode);
    procedure AddSubnode(aParentNode: TNode; aNode: TNode); //Neuen Sub Knoten hinzufügen

    property Caption: String read FCaption write FCaption;
    property Nodes[Index: Integer]: TNode read GetNodes; //Die Blätter
    property Count: Integer read GetCount; //Anzahl Blätter
    property Parent: TNode read FParent write FParent;
  end;

function CompareInt(a,b: Integer): Integer;
function CompareStr(s,t: String): Integer;

implementation

function CompareInt(a,b: Integer): Integer;
begin
  if a<b then Result := -1 else
  if a=b then Result := 0 else
  Result := +1;
end;

function CompareStr(s,t: String): Integer;
begin
  if s<t then Result := -1 else
  if s=t then Result := 0 else
  Result := +1;
end;

{ TNode }

procedure TNode.Add(ParentNode: TNode; aNode: TNode);
begin
  ParentNode.Nodes[ParentNode.FSubNodes.Count-1].AddSubNode(ParentNode,aNode);
end;

procedure TNode.AddSubnode(aParentNode: TNode; ANode: TNode);
begin
  aNode.Parent := aParentNode;
  FSubNodes.Add(aNode);
end;

constructor TNode.Create(aParent: TNode; CompareFunc: TCompareFunc; aCaption: String);
var a: Pointer; b: TNode;
begin
  inherited Create;
  FParent := aParent;
  FCaption := aCaption;
  FSubnodes := TNodes.Create; //Zeiger auf Subnodes als Liste realisiert
  FCompare := @CompareFunc;
end;

destructor TNode.Destroy;
begin
  FSubnodes.Free;
  inherited;
end;

function TNode.GetCount: Integer;
begin
  Result := FSubnodes.Count;
end;

function TNode.GetNodes(Index: Integer): TNode;
begin
  if Index < 0 then Result := NIL;
  if Index >= FSubnodes.Count then Result := NIL;

  if (Index < FSubnodes.Count) and (Index >= 0) then
    Result := FSubNodes.Nodes[Index];
end;


{ TNodes }

function TNodes.Add(aNode: TNode): Integer;
begin
  Result := inherited Add(Pointer(aNode));
end;

function TNodes.GetNodes(Index: Integer): TNode;
begin
  Result := TNode(Items[Index]);
end;

{ TNodeData }

constructor TNodeData.Create(aKey: Integer; aKeyStr: String);
begin
  inherited Create;

  FKeyStr := aKeyStr;
  FKey := aKey;
end;

end.

Delphi-Quellcode:
unit Utreeform;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ugentree, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    lblComponents: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    function CreateNodes: TNode;
  end;

var
  Form2: TForm2;

  Root: TNode;
  myFirstNode: TNode;
  mySecondNode: TNode;
  MyThirdNode: TNode;

implementation

{$R *.dfm}

function TForm2.CreateNodes: TNode;
var
  Data: TNodeData; Node: TNode;
begin
  Root.AddSubNode(nil,TNode.Create(Root,CompareStr,'TButton'));

  Root.AddSubnode(nil,TNode.Create(Root,CompareStr,'TEdit'));


  //Data.KeyStr := 'TEdit';

  Root.AddSubnode(nil,TNode.Create(Root,CompareStr,'TGrid'));

  //Node := TNode.Create(CompareStr);

  //Data.KeyStr := 'TGrid';

  Root.AddSubnode(nil,TNode.Create(Root,CompareStr,'TComboBox'));

  Node := TNode.Create(Root,CompareStr,'Zusätzlich');
  Node.AddSubNode(Root,TNode.Create(Root,CompareStr,'TPanel'));
  Node.AddSubnode(Root,TNode.Create(Root,CompareStr,'TTree'));
  Node.AddSubnode(Root,TNode.Create(Root,CompareStr,'TListbox'));
  Result := Node;
end;


procedure TForm2.FormCreate(Sender: TObject);
var i: Integer; D: TNodeData; N: TNode;
begin
  N:=CreateNodes();

  Memo1.Lines.Add(N.Caption); //Zusätzlich
  //Memo1.Lines.Add(N.Parent.Caption); //Standard
  //Ausgabe muss später rekursiv erfolgen
  for i := 0 to N.Count-1 do
  begin
    Memo1.Lines.Add(N.Nodes[i].Caption); //auch hier rekursiv,
                                          //N.Nodes enthält das zuletzt formulierte,
                                          //N.Parent.Nodes das vorherige
  end;

  Memo1.Lines.Add(N.Parent.Caption);
  for i := 0 to N.Parent.Count-1 do
  begin
    Memo1.Lines.Add(N.Parent.Nodes[i].Caption);
  end;

end;

initialization
   Root := TNode.Create(nil,CompareStr,'Standard');

finalization

end.
Funktioniert so erst mal, Danke an Euch alle!

Jetzt guck ich mir Fietes Quellcode noch mal genauer an.
  Mit Zitat antworten Zitat