###### 求二叉树的算法

1、题：

User_idParent_idLorR

1-1-1

211

312

421

522

631

......

P_GetNewNode (@User_id int, @Node_id int output, @LorR int output)

AS

......

set @Node_id = ...

set @LorR = ...

GO

create table BTree(UserID int, ParentID int, LorR int)

go—为了便于测试，我多写了几行

insert BTree 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, 4, 1

union all select 8, 6, 1

union all select 9, 6, 2

union all select 10, 7, 1

union all select 11, 8, 1

union all select 12, 8, 2

union all select 13, 9, 2

union all select 14, 10, 2

Go

create function fnGetSubNodes(@UserID int)—得到子节点个数

returns int as

begin

declare @Total int, @SubNodeL int, @SubNodeR int

set @Total=0

select @SubNodeL=UserID from BTree where ParentID=@UserID and LorR=1

select @SubNodeR=UserID from BTree where ParentID=@UserID and LorR=2

if @SubNodeL is not null

set @Total=1+dbo.fnGetSubNodes(@SubNodeL)

if @SubNodeR is not null

set @Total=@Total+1+dbo.fnGetSubNodes(@SubNodeR)

return @Total

end

go

--得到最深的节点层数，通过递归去找最深的值，如果左右都有子节点，再判断谁的子节点层次深，总之每次在递归返回都是最深的节点值（写这段我也差点被搅晕了，后来才在每上IF上加了注释，为了便于以后阅读，又加了些）

create function fnGetSubLayers(@UserID int, @Layer int)

returns varchar(100) as

begin

declare @result varchar(100)

set @result=cast(@Layer as varchar)+'|'+cast(@UserID as varchar)

declare @NID int, @NID2 int, @LL varchar(10), @LR varchar(10)

select @NID=UserID from BTree where ParentID=@UserID and LorR=1

select @NID2=UserID from BTree where ParentID=@UserID and LorR=2

if @NID is not null

begin

set @LL=dbo.fnGetSubLayers(@NID,@Layer+1)

if @NID2 is not null

begin

set @LR=dbo.fnGetSubLayers(@NID2,@Layer+1)

if(convert(int,left(@LL,charindex('|',@LL)-1))>=convert(int,left(@LR,charindex('|',@LR)-1)))—在两个都不为空的情况下再判断哪个层深@result就等于哪个返回值

set @result=@LL

else

set @result=@LR

end

else—当右边为空时，@result等于左边返回值

set @result=@LL

end

else if@NID2 is not null—如果左为空，而右不为空，调用并返回右边值

begin

set @LR=dbo.fnGetSubLayers(@NID2,@Layer+1)

set @result=@LR

end

return @result

end

go

create procedure spGetNewNode(@UserID int, @NodeID int output, @LorR int output)

AS

BEGIN

declare @SubNodeL int, @SubNodeR int, @Layer varchar(100)

select @Layer='0|'+cast(@UserID as varchar)

select @SubNodeL=UserID from BTree where ParentID=@UserID and LorR=1

select @SubNodeR=UserID from BTree where ParentID=@UserID and LorR=2

if @SubNodeL is not null

if @SubNodeR is not null

if(dbo.fnGetSubNodes(@SubNodeL)<=dbo.fnGetSubNodes(@SubNodeR))

set @Layer=dbo.fnGetSubLayers(@SubNodeL,0)

else

set @Layer=dbo.fnGetSubLayers(@SubNodeR,0)

else—如果右边为空，调用左边和上面的类似

set @Layer=dbo.fnGetSubLayers(@SubNodeL,0)

else if @SubNodeR is not null

set @Layer=dbo.fnGetSubLayers(@SubNodeR,0)

select @NodeID=convert(int,right(@Layer,len(@Layer)-charindex('|',@Layer)))

select @LorR=LorR from BTree where UserID=@NodeID

END

GO

--可以显示一下

declare @UserID int, @NodeID int, @LorR int

set @UserID=1

print('UserIDNodeIDLorR')

while @UserID<=14

begin

exec dbo.spGetNewNode @UserID, @NodeID output, @LorR output

print(cast(@UserID as varchar)+''+cast(@NodeID as varchar)+''+cast(@LorR as varchar))

set @UserID=@UserID+1

end

go

--到最后你也可以删掉所用的东东

drop function dbo.fnGetSubLayers

drop function dbo.fnGetSubNodes

drop procedure dbo.spGetNewNode

drop table BTree

Email:cmh-163@163.com

#### 二叉树深度的算法

2012-02-15 11:35:57

#### 经典算法学习——求二叉树叶子节点的个数

2016-10-02 18:27:03

#### 经典算法学习——求二叉树的高度

2016-10-02 21:10:39

#### java 求二叉树最大深度算法

2016-03-16 00:14:52

#### 二叉树遍历算法 （递归的、非递归的中序、前序、后序遍历 和 层次遍历 以及 求二叉树的宽度和深度）

2015年09月22日 158KB 下载

#### 算法学习 - 求二叉树的宽度

2015-03-11 18:17:24

#### 关于二叉树宽度的算法包括递归和非递归

2015-11-16 23:09:39

#### 二叉树求深度的递归的详细分析

2011-05-25 20:59:00

#### 二叉链表作存储结构，设计求二叉树高度的算法

2011年02月17日 1KB 下载

#### 计算二叉树的高度的递归和非递归实现

2014-06-29 15:38:44