unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ComCtrls, StdCtrls;
type
PTNodeInfo=^TNodeInfo;
TNodeInfo=record
id,preId:string;
end;
type
TArr=array of TNodeInfo;
type
TForm1 = class(TForm)
Button1: TButton;
tree: TTreeView;
Query1: TADOQuery;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure CreateTree(var arr:TArr);
procedure InitArray(var arr:TArr);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.InitArray(var arr:TArr);
var
Query:TADOQuery;
i:integer;
sql:string;
begin
Query:=TADOQuery.Create(nil);
Query.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/实验/递归父母表示/新建 Microsoft Office Access 应用程序.mdb;Persist Security Info=False';
sql:= 'select * from table1';
Query.Close;
Query.SQL.Clear;
Query.SQL.Add(sql);
Query.Open;
Query.First;
SetLength(arr,Query.RecordCount);
i:=0;
while not Query.Eof do
begin
arr[i].id:=Query.fields[0].AsString;
arr[i].preId:=Query.Fields[1].AsString;
Inc(i);
Query.Next;
end;
end;
procedure TForm1.CreateTree(var arr:TArr);
var
p:pTNodeInfo;
procedure CreateTree(Pre:PTNodeInfo;preNode:TTreeNode;var arr:TArr);
var
pInfo:pTNodeInfo;
node:TTreeNode;
i:integer;
begin
i:=length(arr)-1;
while i>=0 do
begin
new(pInfo);
pInfo:=@arr[i];
if pInfo.preId=pre.id then
begin
node:=tree.Items.AddChild(preNode,pInfo.id);
node.Data:=pInfo;
CreateTree(pInfo,node,arr);
end;
i:=i-1;
end;
end;
begin
new(p);
p.id:='000';
createTree(p,nil,arr);
tree.FullExpand;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
arr:TArr;
i:Integer;
begin
InitArray(arr);
CreateTree(arr);
memo1.Clear;
for i:=0 to tree.Items.Count-1 do
memo1.Lines.Add(ptnodeinfo(tree.Items[i].data).Id+' '+ptnodeinfo(tree.Items[i].data).preId);
end;
end.