BreadthFirstSearch

  • Can we Implement Breadth First Serach in SQL server 2005

  • What exactly are the inputs and output?

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • Id ParentId

    1 0

    2 1

    3 1

    4 1

    5 4

    6 4

    7 6

    8 6

    9 2

    10 2

    11 10

    12 10

    this is the structure of tree.... 1st column is Id and second one is parentId

    I need result set as 1,4,6,7,8,5,2,10,11,12,9,3

  • You want the result as a comma-delimited string?


    N 56°04'39.16"
    E 12°55'05.25"

  • ya but the order should b like i mentioned only...

  • Check this...

    Declare @tMain Table (mid int, pid int)

    Insert into @tMain Values ( 1,0)

    Insert into @tMain Values ( 2,1)

    Insert into @tMain Values ( 3,1)

    Insert into @tMain Values ( 4,1)

    Insert into @tMain Values ( 5,4)

    Insert into @tMain Values ( 6,4)

    Insert into @tMain Values ( 7,6)

    Insert into @tMain Values ( 8,6)

    Insert into @tMain Values ( 9,2)

    Insert into @tMain Values ( 10,2)

    Insert into @tMain Values ( 11,10)

    Insert into @tMain Values ( 12,10)

    Insert into @tMain Values ( 13,12)

    Insert into @tMain Values ( 14,12)

    Insert into @tMain Values ( 15,14)

    Insert into @tMain Values ( 16,14)

    Insert into @tMain Values ( 17,16)

    --Select 'Insert into @tMain Values ( ' + Cast(mid as varchar(10)) + ',' + Cast(pid as varchar(10)) + ')'

    --from ALIEN_MISSILE

    Declare @mid int,@pid int, @vMax int

    Declare @t1 Table (mmid int)

    Set @mid = 1

    Set @pid = 0

    Declare @cnt int

    Set @cnt = 0

    while 1=1

    begin

    Set @cnt = @cnt + 1

    Set @vMax = 0

    Select @vMax = Max(mid) from @tMain where pid = @mid

    if Not Exists(Select * from @t1 where mmid = @mid)

    begin

    Insert into @t1

    Select @mid

    end

    if Exists(Select * from @tMain where pid = @vMax)

    begin

    if Not Exists(Select * from @t1 where mmid = @vMax)

    begin

    Insert into @t1

    Select @vMax

    end

    Set @mid = @vMax

    end

    else

    begin

    Insert into @t1

    Select mid from @tMain where pid = @mid and mid not in (Select mmid from @t1)

    print @mid

    Select @mid = pid from @tMain where mid = @mid

    while 1=1

    begin

    Set @cnt = @cnt + 1

    if Exists(Select top 1 mid from @tMain where pid = @mid and mid not in (Select mmid from @t1) order by mid)

    begin

    Select @mid = (Select top 1 mid from @tMain where pid = @mid and mid not in (Select mmid from @t1) order by mid)

    BREAK

    end

    else

    Select @mid = pid from @tMain where mid = @mid

    print @mid

    if Not Exists(Select mid from @tMain where mid not in (Select mmid from @t1 ))

    BREAK

    end

    if Not Exists(Select mid from @tMain where mid not in (Select mmid from @t1 ))

    BREAK

    end

    end

    Select * from @t1

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply