一種效率極高的分類演算法

1、分類演算法常常表現為樹的表示和遍曆問題。那麼，請問：如果用資料庫中的一個Table來表達樹型分類，應該有幾個欄位？
2、如何快速地從這個Table恢複出一棵樹；
3、如何判斷某個分類是否是另一個分類的子類；
4、如何尋找某個分類的所有產品；
5、如何產生分類所在的路徑。
6、如何新增分類；

Create Table [Catalog](

[ID] [int] NOT NULL,

[Name] [nvarchar](50) NOT NULL,

[FatherID] [int] NOT NULL

);

select Name from catalog where FatherID=FID

<%

REM oConn---資料庫連接，調用GetChildren時已經開啟

REM FID-----當前分類的編號

Function GetChildren(oConn,FID)

strSQL = "select ID,Name from catalog where FatherID="&FID

set rsCatalog = oConn.Execute(strSQL)

%>

<UL>

<%

Do while not rsCatalog.Eof

%>

<LI><%=rsCatalog("Name")%>

<%

Loop

%>

</UL>

<%

rsCatalog.Close

End Function

%>

<%

REM oConn---資料庫連接，已經開啟

REM FID-----當前分類的編號

Function GetChildren(oConn,FID)

strSQL = "select Name from catalog where FatherID="&FID

set rsCatalog = oConn.Execute(strSQL)

%>

<UL>

<%

Do while not rsCatalog.Eof

%>

<LI><%=rsCatalog("Name")%>

<%=GetChildren(oConn,Catalog("ID"))%>

<%

Loop

%>

</UL>

<%

rsCatalog.Close

End Function

%>

<%

REM strConn--串連資料庫的字串，請根據情況修改

oConn.Open strConn

=GetChildren(oConn,-1)

oConn.Close

%>

Create Table Product(

[ID] [int] NOT NULL,

[Name] [nvchar] NOT NULL,

[FatherID] [int] NOT NULL

);

<%

Function GetAllID(oConn,FID)

Dim strTemp

If FID=-1 then

strTemp = ""

else

strTemp =","

end if

strSQL = "select Name from catalog where FatherID="&FID

set rsCatalog = oConn.Execute(strSQL)

Do while not rsCatalog.Eof

strTemp=strTemp&rsCatalog("ID")&GetAllID(oConn,Catalog("ID")) REM 遞迴調用

Loop

rsCatalog.Close

GetAllID = strTemp

End Function

REM strConn--串連資料庫的字串，請根據情況修改

oConn.Open strConn

FID = Request.QueryString("FID")

strSQL = "select top 100 * from Product where FatherID in ("&GetAllID(oConn,FID)&")"

set rsProduct=oConn.Execute(strSQL)

%>

<UL><%

Do while not rsProduct.EOF

%>

<LI><%=rsProduct("Name")%>

<%

Lo

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD