怎樣創建一個存盤二叉樹的佇列
uj5u.com熱心網友回復:
不知道你說的是不是SQL層面的處理,就當是吧
if object_id('tempdb..#tab')is not null drop table #tab
create table #tab(ID nvarchar(50),ParentID nvarchar(50),Name nvarchar(50))
insert into #tab(ID,ParentID,Name)
select '1','-','A' union all
select '2','1','B' union all
select '3','1','C' union all
select '4','2','D' union all
select '5','2','E' union all
select '6','3','F' union all
select '7','3','G' union all
select '8','4','H' union all
select '9','4','I' union all
select '10','8','J' union all
select '11','10','K'
;with t as(
select ID,ParentID,Name,1 as lv,cast(Name as nvarchar(max)) as path from #tab where ParentID='-'
union all
select a.ID,a.ParentID,a.Name,(t.lv+1) as lv,(t.path+'-'+a.Name) as path
from #tab a inner join t on a.ParentID=t.ID
)
select Name,lv,path from t
uj5u.com熱心網友回復:
不是,是用佇列進行二叉樹的層次遍歷轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/34482.html
標籤:新技術前沿
