关闭

构建树(孩子兄弟表示法)

1824人阅读 评论(0) 收藏 举报
构建树(孩子兄弟表示法)

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;
type
  PTNode=^TNode;
  TNode=record
  id:string;
  fID,nID:string;
  hasCreate:boolean;
  fChild:PTNode;
  nSibling:PTNode;
end;
type
  TArr=array of TNode;
type
  TTree=class(TObject)
  root,cur:PTNode;
  constructor create; overload;
  constructor create(var arr:TArr); overload;
  function FindNode(s:string;p:PTNode):boolean;
end;
type
  TForm1 = class(TForm)
    Button1: TButton;
    conn: TADOConnection;
    Query: TADOQuery;
    Memo1: TMemo;
    procedure DisPlayFChild(p:PTNode);
    procedure LoadData(var arr:Tarr);
    function  GetNode(var arr:Tarr;s:string):PTNode;
    procedure Button1Click(Sender: TObject);
  
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.displayFChild(p:PTnode);
begin
  if p.fChild<>nil then begin memo1.Lines.Add(p.fchild.id); end;
end;


function TTree.FindNode(s:string;p:PTNode):boolean;
var
  pn:PTNode;
begin
  result:=false;
  if p.id=s then
    begin
      result:=true;
      cur:=p;
      exit;
    end;
  pn:=p.fChild;
  while ((pn<>nil)and (FindNode(s,pn)=false)) do
    begin
      pn:=pn.nSibling;
    end;
end;

function  TForm1.GetNode(var arr:Tarr;s:string):PTNode;
var
i,n:integer;
begin
  result:=nil;
  n:=Length(arr);
  for i:=0 to n-1 do
  begin
    if (arr[i].id=s)  then
    begin result:=@arr[i]; exit; end;
  end;
end;
procedure TForm1.LoadData(var arr:TArr);              //load data from database
var
i:integer;
begin
  Query.Close;
  Query.SQL.Clear;
  Query.SQL.Add('select * from table2');
  Query.Open;
  Query.First;
  i:=Query.RecordCount;
  setLength(arr,i);
  i:=0;
  while not Query.Eof  do
  begin
    arr[i].id:=Query.Fields[0].AsString;
    arr[i].fID:=Query.Fields[1].AsString;
    arr[i].nID:=Query.Fields[2].AsString;
    arr[i].hasCreate:=false;
    i:=i+1;
    Query.Next;
  end;
  Query.Close;
end;
constructor TTree.create;
begin
  root:=nil;
end;
constructor TTree.create(var arr:TArr);             //create a tree from arr
var
i,j,n:integer;
p:PTNode;
begin
  i:=0;
  n:=Length(arr);
  while i<n do
  begin
    j:=0;
    while j<n do
    begin
      if (arr[j].id='root') and (arr[j].hasCreate=false) then
        begin
          new(root);
          root:=@arr[j];
          root.fChild:=nil;
          root.nSibling:=nil;
          cur:=root;
          i:=i+1;
          arr[j].hasCreate:=true;
        end;
      if arr[j].hasCreate=true  then
        begin
          if arr[j].fid<>'none'  then
            begin
              p:=Form1.GetNode(arr,arr[j].fID);
              if p.hasCreate=false then
                begin
                  arr[j].fChild:=p;
                  p.hascreate:=true;
                  p.fchild:=nil;
                  p.nsibling:=nil;
                  i:=i+1;
                end;
            end;
          if arr[j].nid<>'none' then
            begin
              p:=Form1.GetNode(arr,arr[j].nID);
              if p.hascreate=false then
                begin
                  arr[j].nSibling:=p;
                  p.hascreate:=true;
                  p.fchild:=nil;
                  p.nsibling:=nil;
                  i:=i+1;
                end;
            end;
        end;
      j:=j+1;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
arr:TArr;
aTree:TTree;
begin
  LoadData(arr);
  aTree:=TTree.create(arr);
  memo1.Lines.Add(atree.root.fchild.fchild.nsibling.nsibling.id);          //test
  aTree.Free;
end;

end.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9642次
    • 积分:140
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章存档
    最新评论