这个题要发到C语言区去...这是一个先序遍历二叉树的算法....数据结构用C写是这样struct tree { int id; int pid; char data; }Node;再构造 访问函数..
--用SQL來解,就簡單了,一個二叉樹的深度排序. --建立測試數據 GO create table tree(id int,pid int ,name varchar(02)) insert into tree select 1,0,'A' union all select 2,1,'B' union all select 3,4,'C' union all select 4,7,'D' union all select 5,2,'E' union all select 6,4,'F' union all select 7,2,'G' union all select 8,5,'H' union all select 9,0,'I' union all select 10,8,'J' Go --建立存儲過程 create procedure dbo.usp_test @pid int AS
begin set nocount on declare @level int ,@i int ,@flag int declare @stack table (pid int,id int, level int,row int , flag int,name varchar(02)) /*將開始層插入 */ insert into @stack(pid,id,level,row) select @pid,@pid,0,0 select @level = 1,@i=1,@flag=1 insert @stack select pid,id, @level,0,1,name from tree(nolock) where pid = @pid and id is not null
while @level > 0 begin if exists (select * from @stack where level = @level and flag=1) begin select @pid = min(id) from @stack where level = @level and flag=1 update @stack set flag =0 , row=@i where level = @level and id = @pid and flag =1 set @i = @i +1 insert @stack select pid,id, @level +1,0,1 ,name from tree(nolock) where pid = @pid and id is not null if @@rowcount > 0 select @level = @level + 1 end else begin select @level = @level - 1 end end declare @str varchar(50) set @str='' select @str=@str+'-'+name from @stack where name is not null order by row select stuff(@str,1,1,'') set nocount off end Go--執行存儲過程,查看父結點為0的子結點情況
exec dbo.usp_test 0--結果 ------------------------------ A-B-E-H-J-G-D-C-F-I --刪除測試環境和存儲過程 drop table tree drop proc usp_test
以PID 为关键点 读取一个数据向连表里加 如过连表里的相应位置(PID )的值已经被数据占了就放到第2 个连表里 所有数据读完了把两个连表连起来就行了
用集合也可以
{
int id;
int pid;
char data;
}Node;再构造
访问函数..
--建立測試數據
GO
create table tree(id int,pid int ,name varchar(02))
insert into tree
select 1,0,'A' union all
select 2,1,'B' union all
select 3,4,'C' union all
select 4,7,'D' union all
select 5,2,'E' union all
select 6,4,'F' union all
select 7,2,'G' union all
select 8,5,'H' union all
select 9,0,'I' union all
select 10,8,'J' Go
--建立存儲過程
create procedure dbo.usp_test
@pid int
AS
begin
set nocount on
declare @level int ,@i int ,@flag int
declare @stack table (pid int,id int, level int,row int , flag int,name varchar(02))
/*將開始層插入 */
insert into @stack(pid,id,level,row)
select @pid,@pid,0,0
select @level = 1,@i=1,@flag=1
insert @stack
select pid,id, @level,0,1,name
from tree(nolock)
where pid = @pid and id is not null
while @level > 0
begin
if exists (select * from @stack where level = @level and flag=1)
begin
select @pid = min(id)
from @stack
where level = @level and flag=1
update @stack set flag =0 , row=@i
where level = @level
and id = @pid and flag =1
set @i = @i +1
insert @stack
select pid,id, @level +1,0,1 ,name
from tree(nolock)
where pid = @pid and id is not null
if @@rowcount > 0
select @level = @level + 1
end
else
begin
select @level = @level - 1
end
end declare @str varchar(50)
set @str=''
select @str=@str+'-'+name from @stack where name is not null order by row
select stuff(@str,1,1,'')
set nocount off
end
Go--執行存儲過程,查看父結點為0的子結點情況
exec dbo.usp_test 0--結果
------------------------------
A-B-E-H-J-G-D-C-F-I
--刪除測試環境和存儲過程
drop table tree
drop proc usp_test
DataTable dt = new DataTable("RoadTree");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("PID", typeof(int));
dt.Columns.Add("Name", typeof(string)); DataRow row = null; //初始化四个父类记录
#region 初始化四个父类记录
row = dt.NewRow();
row["ID"] = 1;
row["PID"] = 0;
row["Name"] = "A";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = 2;
row["PID"] = 1;
row["RoadName"] = "B";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = 3;
row["PID"] = 4;
row["Name"] = "C";
dt.Rows.Add(row);
……………………//以上是将所有的数据存至DataSet数据集中
//以下采用递归算法BindNode,来遍历所有的结点,并将结点绑定到DropDownList中
BindNode(this.DropDownList1, "0", Set);
//-----------BindNode递归算法如下:--------------//
private void BindNode(DropDownList Nds, string pNodeId, DataSet ds)
{
DataView dvTree = new DataView(ds.Tables[0]); dvTree.RowFilter = "[PID] = " + pNodeId; foreach (DataRowView Row in dvTree)
{
ListItem tempNode = new ListItem();
tempNode.Value = Row["ID"].ToString(); if (pNodeId != "0")
tempNode.Text = "----" + Row["Name"].ToString();
else
{
tempNode.Text = Row["Name"].ToString();
tempNode.Attributes.Add("style", "background-color: #005A94;color:white;");
} Nds.Items.Add(tempNode); //建立该结点的子节点;如果该节点还有其它子节点,就进行递归调用
BindNode(Nds, Row["ID"].ToString(), ds);
}
}
还不如填充到arraylist中就可以得到了
我想问一下您:您是只专注SQL Server 2000的吗?还是另外还搞其它开发?我这样问你,是因为我现在只专注学SQL Server 2000,其它的开发语言我觉得很难学.想问一下这样有没有前途?