不就是一棵树吗?
你如果是3为3为定长的就更简单了
//******建树之前先调用FreeNode******************************
//******建树调用CreateTree或CreateMyTree********************
//创建编码层次树 treeview:树控件名;Fdatabasename:数据库名,Fsqlstring:SQL语句
//fbm:编码,fmc:名称,level:层次,long:长度
procedure CreateTree(treeview:ttreeview;Fdatabasename,Fsqlstring,fbm,fmc:string;level,long:integer);Overload;
procedure CreateTree(treeview:ttreeview;Fdatabasename,Fsqlstring,fbm,fmc:string;level,long:integer);
var query:TQuery;
code,bh:string;
FNode
char;
begin
try
query:=TQuery.Create(nil);
except
exit;
end;
query.DatabaseName:=fdatabasename;
query.close;
query.sql.Clear;
query.sql.Add(fsqlstring+' order by '+fbm);
query.Prepare;
try
query.open;
except
exit;
end;
query.First;
while not query.Eof do
begin
bh:=query.FieldByName(Fbm).asstring;
code := query.FieldByName(Fmc).asstring ;
try
getmem(fnode,length(bh)+1);//得到指针的内存空间
except
exit;
end;
move(bh[1],fnode^,length(bh)+1);//指针指出结点对应的编码
if (length(bh)=long)and(level=1) then //第一层
begin
treeview.Items.AddObject(nil,code, fnode);
end;
if level in [2..10] then //其他层
begin
AddItem(treeview,bh,code,long,level,fnode);
end;
query.Next;
end;
query.free;
end;
//释放节点内存
procedure FreeNode(TREEVIEW:TTREEVIEW);
procedure FreeNode(TREEVIEW:TTREEVIEW);
VAR
I:INTEGER;
pnode
char;
begin
FOR I:=0 to treeview.Items.Count-1 do
begin
pnode:=pchar(treeview.Items
.Data);
Freemem(pnode);
end;
end;
如果你建的是结构树(就是有父亲的那种),再跟我说,我给你个例子