树数据结构高手请进!200分相送(200分)

W

winmax

Unregistered / Unconfirmed
GUEST, unregistred user!
在数据库里有以下结构:
1__17___18___20
| |___21
|___19___22
|___23
1为最高节点,17下面有两个节点(18,19),18下面又有两个节点(20,21),19下面又有两个节点(22,23).....
现在已知:节点17..
问题:怎样通过一个算法,找到17下面所属的所有节点内容呢?
具体就是:已知17,怎样通过一个算法找到18,19,20,21,22,23...
请大家帮帮忙,给出具体的算法给我,200分相送!!
 
select * from yourtable where 节点值 >17 order by 节点值
大概没有领会楼主的真正意图,请楼主再说明白些
 
楼上的朋友,如果真的能用你所说的方法select,那我就不用贴出来问大家了!!
希望懂得数据库树数理结构的高手解答!!
 
看来我是低手了,做数据库这么多年看来是白做了
期待高人回答此问题
关注
 
树的遍历。 下面是相似代码。
var
node: TTreeNode;
label ok;
begin
node := TreeView1.TopItem;
while Truedo
begin
if node.HasChildren then
node := node.GetFirstChild
else
begin
while node.IsLastdo
if node.Level = 0 then
goto ok
else
node := node.Parent;
node := node.GetNextSibling;
end;
end;
 
不能用select做到的
要用递归算法
我给你一个参考,可以显示目录树结构的
这个是在asp网页的目录分层的读取函数
表结构
Id,className,description,parentId

function listChildbook(id)
dim sRec,sSql,sNum,sTmp
set sRec=server.createobject("adodb.recordset")
sSql="select Id,className,description,parentId from st_book_type where parentId="
& id
sRec.open sSql,Conn,3,1
if not sRec.eof then
sNum=1
response.write &quot;<ul>&quot;
while not sRec.eof
sTmp=Conn.execute(&quot;select count(Id) as countSub from st_book_type where parentId=&quot;
& sRec(&quot;Id&quot;))
if sTmp(&quot;countSub&quot;)>0 then
response.write &quot;<li><A href=1.asp?ID=&quot;&sRec(&quot;id&quot;)&&quot;>&quot;
& sRec(&quot;className&quot;) & &quot;</A>&quot;
listChildbook(sRec(&quot;Id&quot;))
response.write &quot;</li>&quot;
else
response.write &quot;<li><A href=1.asp?ID=&quot;&sRec(&quot;id&quot;)&&quot;>&quot;
& sRec(&quot;className&quot;) & &quot;</A></li>&quot;
end if
sRec.movenext
sNum=sNum+1
wend
response.write &quot;</ul>&quot;
end if
sRec.close
set sRec=nothing
end function
 
其实你可以研究一下论坛的数据库的结构就明白了
每一版就是一个顶级,然后下面可以有很多层次结构
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3345171
这个帖子或许有点用
 
楼主说的是在数据库中,不是在treeview中
 
建立函数
Create Function IsParent (
@ID int,
@PID int
) Returns int
AS
begin
Declare @tid int
Set @tid = @ID
While IsNull(@tid,-1) <> -1
begin
Select @tid = parent From 表 Where keyid = @tid
if @tid = @PID
Return 1
End
Return 0
End
然后用
Select MC From 表 where IsParent(KeyID,你要查找的ID) = 1
游标好象效率不怎么好,我是没必要就不用它
 
用递归方便
 
这个应该更合适你,一定要用递归的
function GetAllChildID(id)
'取得FolderID为id的目录下所有子目录的FolderID,以半角逗号分开
dim arrID
arrID = id
Set rsdir = Conn.Execute(&quot;Select ID,ParentID from st_book_type where ParentID = &quot;
& id & &quot;&quot;)
if rsdir.eof and rsdir.bof then

set rsdir = nothing
GetAllChildID = arrID
exit function
else

while not rsdir.eof
arrID = arrID & &quot;,&quot;
& GetAllChildID(rsdir(&quot;ID&quot;))
rsdir.movenext
wend
end if
set rsdir = nothing
GetAllChildID = arrID
end function
 
楼主的数据表是怎么建的,列出来给大家看看,省得大家在这瞎忙活
 
我这里有段代码,是从数据库中选出数结构的,看看是否对你有用
procedure TForm1.OutRoot;
//显示根节点 key查询条件,key1关键字
var RootNode: TTreeNode;
begin
BDE1.Close;
BDE1.CommandText := ' select * from ' + 表名 + ' where ' + 父节点 + '=0 ' + 'order by XH';
BDE1.Open;
BDE1.First;
while not BDE1.Eofdo
begin
RootNode := TreeView1.Items.Add(nil, 节点名称);
OutSubTree(RootNode, 节点编码);
BDE1.Next;
end;
end;

procedure TForm1.OutSubTree(Node: TTreeNode;
str: string);
//显示子节点 Node节点,str父节点编码,key关键字
var ChildNode, Node1: TTreeNode;
Tree: TBDEClientDataset;
begin
Tree := TBDEClientDataset.Create(self);
Tree.DBConnection := Database1;
Tree.Close;
Tree.CommandText := ' select * from ' + 表名 + ' where ' + 父节点 + '=''' + Str + ''' order by XH ';
Tree.Open;
Tree.First;
while not Tree.Eofdo
begin
ChildNode := TreeView1.Items.AddChild(Node, Tree.节点名称);
//加入子节点
Node1 := ChildNode;
Str := Tree.节点编码;
OutSubTree(Node1, Str);
//递归的加入子节点
Tree.Next;
end;
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1198089
拿分来
 
典型的树的遍历问题.
自己看书去.
 
呵呵..问题已经解决..谢谢各位帮助!我没有用blue_morning的方法,不过还是很感激他提供的那页网页,我是从中得到解决办法的,我还是用递归算法来解决问题的,我测试过,速度还不错!有时间再在数据库写个存储过程,提高一下速度.呵呵.....也感激以上其它各位朋友的发言!!散分了!!!!
 

Similar threads

S
回复
0
查看
959
SUNSTONE的Delphi笔记
S
S
回复
0
查看
779
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
970
SUNSTONE的Delphi笔记
S
顶部