求二叉树的ASP算法
如果是name1,则取出父是name1的所有节点。则是name2,name3,...name13
如果是name4,则取出父是name4的所有节点。则是name6,name7,name9,name10,name11,name12,name13
最多取10层,请大家给个好的思路取这些数据。
层次 名称 父节点 左右
1 name1
2 name2 name1 左
3 name3 name1 右
4 name4 name2 左
5 name5 name3 右
6 name6 name4 左
7 name7 name4 右
8 name8 name5 左
9 name9 name6 左
10 name10 name6 右
11 name11 name10 左
12 name12 name10 右
13 name13 name12
问题点数:20、回复次数:5Top
1 楼liuxiaoyi666(MSMVP 小猪妹荣誉马甲之八卦兔子)回复于 2006-03-12 17:45:28 得分 5
http://blog.csdn.net/liuxiaoyi666/archive/2005/05/17/375997.aspxTop
2 楼fangsky(成雨)回复于 2006-03-12 17:52:27 得分 0
多谢。可是跟我的要求不符合。
我只要把某个节点下的所有子节点取出即可。(10层以内)
比如上面的求name4下的字节点。那只要得到结果(列表的形式)
name6
name7
name9
name10
name11
name12
name13Top
3 楼zys59(三仙半)回复于 2006-03-12 18:39:10 得分 5
请问你的数据结构中有父节点吗?如果有就是双向二叉链表,那就好做得多了。Top
4 楼fangsky(成雨)回复于 2006-03-12 19:25:54 得分 0
结构就是这样比如,怎么用第归取得。 比如是1,则得到整棵树,如是21,则得到31,32,41,42,51,52,53,如是22,则得到33,34
1
21 22
31 32 33 34
41 42
51 52 53Top
5 楼superdullwolf(超级大笨狼,每天要自强,MVP)回复于 2006-03-12 19:34:36 得分 10
以下存成HTML看
<style>
body,td,th
{
font-size:12px;
vertical-align:top;
}
th
{
layout-flow:vertical-ideographic ;
}
</style>
<TABLE border=1>
<TR>
<TD colspan=3 align=center>两种树型结构SQL的对比.htm</TD>
</TR>
<TR>
<TH>名称</TH>
<TD>方法1:父子结构Tree1表</TD>
<TD>方法2:编码结构Tree2表</TD>
</TR>
<TR>
<TH>数据形式</TH>
<TD>
<xmp>
idpIdcontent
--------------------------------------
10节点
20节点
...
111节点
...
212节点
...
12112节点
...
1110110节点
</xmp>
</TD>
<TD>
<xmp>
idnodeCodecontent
--------------------------------------
1001节点
2002节点
...
11001001节点
...
112001001002节点
...
1104010010004节点
...
</xmp>
</TD>
</TR>
<TR>
<TH>建表语句</TH>
<TD>
<xmp>
CREATE TABLE [dbo].[Tree1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[pId] [int] NULL ,
[content] [varchar] (10) NULL
) ON [PRIMARY]
</xmp>
</TD>
<TD>
<xmp>
CREATE TABLE [dbo].[Tree2] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[nodeCode] [varchar] (120) NOT NULL ,
[content] [varchar] (10) NULL
) ON [PRIMARY]
</xmp>
</TD>
</TR>
<TR>
<TH>插入数据10+100+1000条,深度3,广度10</TH>
<TD>
<xmp>--truncate table Tree1
declare @E int--广度循环变量
declare @EE int--广度循环变量
declare @EEE int--广度循环变量
declare @pId int--父id
declare @content varchar(10)
set @content='节点'
--添加第1层10个节点
set @E=1
while @E<=10
begin
set @pId=0
insert tree1(pId,content) values(@pId,@content)
set @E=@E+1
end
--添加第2层100个节点
set @E=1
while @E<=10
begin
set @EE=1
while @EE<=10
begin
set @pId=@E
insert tree1(pId,content) values(@pId,@content)
set @EE=@EE+1
end
set @E=@E+1
end
--添加第3层1000个节点
set @E=1
while @E<=10
begin
set @EE=1
while @EE<=10
begin
set @EEE=1
while @EEE<=10
begin
set @pId=@E*10+ @EE
insert tree1(pId,content) values(@pId,@content)
set @EEE=@EEE+1
end
set @EE=@EE+1
end
set @E=@E+1
end
--select count(*) from tree1</xmp>
</TD>
<TD>
<xmp>--truncate table Tree2
declare @nodeCode varchar(30)
declare @content varchar(10)
declare @E int--广度循环变量
declare @EE int--广度循环变量
declare @EEE int--广度循环变量
set @content='节点'
--添加第1层10个节点
set @E=1
while @E<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @E=@E+1
end
--添加第2层100个节点
set @E=1
while @E<= 10
begin
set @EE=1
while @EE<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
set @nodeCode =@nodeCode + right(rtrim(str(1000 + @EE)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @EE=@EE+1
end
set @E=@E+1
end
--添加第3层1000个节点
set @E=1
while @E<= 10
begin
set @EE=1
while @EE<= 10
begin
set @EEE=1
while @EEE<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
set @nodeCode =@nodeCode + right(rtrim(str(1000 + @EEE)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @EEE=@EEE+1
end
set @EE=@EE+1
end
set @E=@E+1
end
select count(*) from tree2</xmp>
</TD>
</TR>
<TR>
<TH>得到节点深度和排序的办法</TH>
<TD>
<xmp>
用函数,本例是邹建写的,效率比较高。
create function get_Deep_tree1()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select [id],@l,right(10000+[id],4)
from [tree1] where [pid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)
from [tree1] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
return
end
go
</xmp>
</TD>
<TD>
<xmp>
节点深度就是编码长度/3
len(nodeCode)/3
排序只要order by nodeCode asc就可以得到
web叶面需要的
</xmp>
</TD>
</TR>
<TH>查询排序</TH>
<TD>
<xmp>
select a.id,a.content,b.[level]+1 深度
from [tree1] a,get_Deep_tree1() b
where a.[id]=b.[id]
order by b.sid
</xmp>
</TD>
<TD>
<xmp>select *,len(nodeCode)/3 深度 from tree2 order by nodeCode asc
</xmp>
</TD>
</TR>
<TR>
<TH>排序结果</TH>
<TD>
<xmp>
idcontent 深度
1节点1
11节点2
111节点3
..
120节点3
12节点2
...
1072节点3
</xmp>
</TD>
<TD>
<xmp>idnodeCode content深度
1001节点1
11001001节点2
111001001001节点3
...
210001010010节点3
2002节点1
21002001节点2
211002001001节点3
...
1088010008008节点3
...
1101010010001节点3
</xmp>
</TD>
</TR>
<TR>
<TH>查询时间</TH>
<TD>
<xmp>
30毫秒左右
</xmp>
</TD>
<TD>
<xmp>15毫秒左右
</xmp>
</TD>
</TR>
</TABLE>Top




