如何获取 树形结构表中某一个节的所有子节点
比如有一张实现数型结构的表。create table tree
( no char(4),
name char(10),
fatherNo char(4)
)表的纪录为: No Name fatherNo
1 aaa 0
2 bbb 1
3 ccc 2
4 ddd 2
0 eee 3
6 fff 1
对应的树应该为
1
2
3
0
4
6现在我想获取 No为 2 的所有子节点的No编号。
算下来应该为(3,4,0)但是如何通过程序获取呢,数据结构里介绍过二叉树的遍历,但是这里的树不是二叉树啊。
希望朋友们提供这个算法。
比如有一张实现数型结构的表。create table tree
( no char(4),
name char(10),
fatherNo char(4)
)表的纪录为: No Name fatherNo
1 aaa 0
2 bbb 1
3 ccc 2
4 ddd 2
0 eee 3
6 fff 1
对应的树应该为
1
2
3
0
4
6现在我想获取 No为 2 的所有子节点的No编号。
算下来应该为(3,4,0)但是如何通过程序获取呢,数据结构里介绍过二叉树的遍历,但是这里的树不是二叉树啊。
希望朋友们提供这个算法。
-----------------------------------------------------------------------------递归function GetChildNo(iNo: Integer): string;
function HasChild(iNo: Integer): Boolean;
begin
Result := False;
with TQuery.Create(nil) do
begin
DataBaseName := 'xxxx';
try
SQL.Add('select 1 from Tree where FatherNo=:pFatherNo');
ParamByName('pFatherNo').AsInteger := iNo;
Open;
Result := Not IsEmpty;
finally
Free;
end;
end;
end;
begin
Result := '';
with TQuery.Create(nil) do
begin
DataBaseName := 'xxxx';
try
SQL.Add('select No from Tree where FatherNo =:pFatherNo');
ParamByName('pFatherNo').AsInteger := iNo;
Open;
while not Eof do
begin
if HasChild(FieldByName('No').AsInteger) then
GetChildNo(FieldByName('No').AsInteger)
else
begin
if Result = '' then
Result := IntToStr(FieldByName('No').AsInteger)
else Result := Result + ',' +
IntToStr(FieldByName('No').AsInteger);
end;
end;
finally
Free;
end;
end;
end;用法: str := GetChildNo(2), 结果: str =
'3,4,5';
你有两个错误1:楼上没有各位兄弟
讲了那么多
其实都是wolfbound(wolfbound)一个人2:你狂个叼啊?
你既然“我对树的研究比你们深入的多”
又何必发帖来问?
既然“不过感觉应该还有效率更高的方法”
那就虚心点
----------------------------------------------
靠,什么玩意?
既然你对树的研究比我们深入的多
那就不告诉你
自己慢慢去找吧
高手
function TmainForm.Getsubnode(currnode : ttreenode) : string;
var
num,i:integer;
cs : array[1..10] of string; procedure getchildnode(curr :ttreenode);
var
nd:ttreenode;
mynum:integer;
begin
mynum:=0;
cs[num]:=curr.Text+'下有:';
nd:=curr.getFirstChild;
while nd<>nil do
begin
mynum:=mynum+1;
if nd.getFirstChild<>nil then
begin
num:=num+1;
getchildnode(nd);
num:=num-1;
end;
nd:=nd.getNextSibling;
end;
cs[num]:=cs[num]+inttostr(mynum)+'个节点'+chr(13)+chr(13);
end;
BEGIN num:=1;
getchildnode(currnode);
result:='';
for i := 1 to 10 do
begin
if cs[i]<>'' then
result:=result+cs[i];
end;
if result='' then
result:='没有下级节点';
END;QQ:549711582,请加我。
首先,请大家仔细看一下我的问题。其实我这里根本不存在一个TreeView,给你的条件就是一个表。这个表的结构是树型的方式;
现在要做的是: 给你某一条记录,把该条记录下所对应的子纪录找出来;说白了,我的命题不是研究TreeView的ttreenode之类;
我把我3年前作的一个DbTReeView控件的雏形期的TreeView和DB关联的技巧程序公布,希望对大家有所帮助。该程序能够自动绑定数据表,各个节点可以任意调动位置,位置调动后,db的数据跟着相应改变;http://www.sjflower.cn/dmx/dai/treedrag.rar程序没有第三方控件,保证能打开编译,希望能让大家对TreeView的认识更加的深入。
不好意思,我前面回复时没细看你的问题用以下方法试试
表的纪录为:
在表中加一字段,如下
No Name fatherNo Nolist
1 aaa 0 001,000,
2 bbb 1 002,001,000,
3 ccc 2 003,002,001,000,
4 ddd 2 004,002,001,000,
5 ddd 3 005,003,002,001,000,
6 fff 1 006,001,000, id:=2;
q3:=tadoquery.Create(self);
q3.Connection:=dmform.ADOConnection1;
q3.SQL.Clear;
q3.SQL.Add('select * from tree where Nolist like '''+'%'+format('%.10d',[id])+',%''');
q3.Prepared;
q3.Open;
如果需要写Nolist字段内容的代码,请与QQ:549711582联系