2 SQLServer与算法:
有如下表,记录了分左右区的2叉树记录。其中:LorR字段,-1 表示顶级,1 表示左边,2表示右边;Parent_id子段,-1 表示顶级,其它表示上级父ID;User_id字段值是唯一的。
User_id Parent_id LorR
1 -1 -1
2 1 1
3 1 2
4 2 1
5 2 2
6 3 1
......
请书写一个存储过程,返回给定User_id 的左、右区中小区(网络点数少一点的区,一样多则左边优先)的最下一个新节点(如有左右2个空节点,左边优先)。
格式:
P_GetNewNode (@User_id int, @Node_id int output, @LorR int output)
AS
......
set @Node_id = ...
set @LorR = ...
GO
------解决方案--------------------
请书写一个存储过程,返回给定User_id 的左、右区中小区(网络点数少一点的区,一样多则左边优先)的最下一个新节点(如有左右2个空节点,左边优先)。
--------
啥意思?
------解决方案--------------------
/*测试环境*/
create table Tree (
[User_id] int,
Parent_id int,
LorR int
)
insert into Tree select 1,-1,-1
union all select 2,1,1
union all select 3,1,2
union all select 4,2,1
union all select 5,2,2
union all select 6,3,1
union all select 7,3,2
union all select 8,4,1
union all select 9,4,2
/*存储过程*/
create proc P_GetNewNode (@User_id int)
AS
select * into #temp from Tree where [User_id][email protected]_id
while @@rowcount=1
begin
insert #temp select b.*
from #temp a,tree b
where a.[user_id]=b.parent_id
and b.[user_id] not in (select [user_id] from #temp)
and not exists (select 1 from Tree where Parent_id=b.Parent_id
and [User_id] <b.[User_id])
end
select * from #temp
向下找所有子树杈中,左或右最小的一个节点
找到底
/*结果*/
4 2 1
8 4 1
2 1 1
------解决方案--------------------
如果这颗树是有规律的,即User_id顺序递增,树左边一定大于右边的话,那么 welove1983(啊哈啊哈) 所写的已经解决最重要的问题.
------解决方案--------------------
2000还是2005?
------解决方案--------------------
create table Tree (
[User_id] int,
Parent_id int,
LorR int
)
insert into Tree select 1,-1,-1
union all select 2,1,1
union all select 3,1,2
union all select 4,2,1
union all select 5,2,2
union all select 6,3,1
union all select 7,3,2
union all select 8,4,1
union all select 9,4,2
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create proc sp_tree (@uid int)
as
begin
if exists (select name from tempdb.dbo.sysobjects where id=object_id( 'tempdb..#temp1 '))
drop table #temp1
if exists (select name from tempdb.dbo.sysobjects where id=object_id( 'tempdb..#temp1 '))
drop table #temp2
select *,fldlevel=1 into #temp1 from tree where [user_id][email protected]
select *,fldlevel=1 into #temp2 from tree where [user_id][email protected]
declare @i int
set @i=1
while @i <10
begin
if not exists (select * from #temp1 where [email protected]) break
insert into #temp1
select
a.*,
@i+1
from
tree a inner join (select * from #temp1 where [email protected]) b on
b.parent_id=a.[user_id]
set @[email protected]+1
end
set @i=1
while @i <10
begin
if not exists (select * from #temp2 where [email protected]) break
insert into #temp2
select
a.*,
@i+1
from
tree a inner join (select * from #temp2 where [email protected]) b on