這篇文章給大家分享的是有關(guān)怎么用VBS模擬二叉樹的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

數(shù)據(jù)結(jié)構(gòu)知識:
二叉樹中序便歷可以用來做排序
而VBS里面恰恰就沒有現(xiàn)成的排序方法,因此我寫了一個用VBS的二叉樹,來解決排序問題,中序便歷就是排序。大家可以參考原理,應(yīng)用到自己的程序中。
<SCRIPT LANGUAGE="vbScript">
class node
public data
public Lnode
public Rnode
sub insert(newData)
if newData<data then
if IsEmpty(Lnode) then
set Lnode=new node
Lnode.data = newData
else
Lnode.insert newData
end if
else
if IsEmpty(Rnode) then
set Rnode=new node
Rnode.data = newData
else
Rnode.insert newData
end if
end if
end sub
end class
class tree
public root
sub insertNode(newData)
if IsEmpty(root) then
set root=new node
root.data=newData
else
root.insert newData
end if
end sub
sub preOrderTraversal'前序便歷
preOrder root
document.write "<br/>"
end sub
sub inOrderTraversal '中序便歷
inOrder root
document.write "<br/>"
end sub
sub postOrderTraversal'后序便歷
postOrder root
document.write "<br/>"
end sub
Private sub preOrder(N)
if IsEmpty(N) then exit sub
document.write " " & N.data
preOrder N.Lnode
preOrder N.Rnode
end sub
Private sub inOrder(N)
if IsEmpty(N) then exit sub
inOrder N.Lnode
document.write " " & N.data
inOrder N.Rnode
end sub
Private sub postOrder(N)
if IsEmpty(N) then exit sub
postOrder N.Lnode
postOrder N.Rnode
document.write " " & N.data
end sub
end class
'調(diào)用示例
set T=new tree
document.write "插入節(jié)點"
arr=array(39,69,94,47,50,72,55,41,97,73)
for i=0 to 9
document.write " " & arr(i)
T.insertNode arr(i)
next
document.write "<br/>"
document.write "前序便歷"
T.preOrderTraversal
document.write "中序便歷"
T.inOrderTraversal
document.write "后序便歷"
T.postOrderTraversal
</SCRIPT>
插入節(jié)點 39 69 94 47 50 72 55 41 97 73
前序便歷 39 69 47 41 50 55 94 72 73 97
中序便歷 39 41 47 50 55 69 72 73 94 97
后序便歷 41 55 50 47 73 72 97 94 69 39
改寫成sort(arr)函數(shù)
<SCRIPT LANGUAGE="vbScript">
class node
public data
public Lnode
public Rnode
sub insert(newData)
if newData<data then
if IsEmpty(Lnode) then
set Lnode=new node
Lnode.data = newData
else
Lnode.insert newData
end if
else
if IsEmpty(Rnode) then
set Rnode=new node
Rnode.data = newData
else
Rnode.insert newData
end if
end if
end sub
end class
class tree
public root
public Arr
private index
sub insertNode(newData)
if IsEmpty(root) then
set root=new node
root.data=newData
index=0
else
root.insert newData
end if
end sub
sub inOrderTraversal '中序便歷
inOrder root
end sub
Private sub inOrder(N)
if IsEmpty(N) then exit sub
inOrder N.Lnode
Arr(index)= N.data
index=index+1
inOrder N.Rnode
end sub
end class
function sort(arr)
set T=new tree
T.Arr=arr
for each a in arr
T.insertNode a
next
T.inOrderTraversal
sort=T.Arr
end function
'-------以上是sort函數(shù)部分------
'-------以下是調(diào)用示例------
'隨便一個數(shù)組
arr=array(39,69,94,47,50,72,55,41,97,73)
'顯示數(shù)組內(nèi)容
for each a in arr
document.write a & " "
next
document.write "<br/>"
'排序處理
arr=sort(arr)
'顯示排序后的結(jié)果
for each a in arr
document.write a & " "
next
</SCRIPT>
輸出結(jié)果:
39 69 94 47 50 72 55 41 97 73
39 41 47 50 55 69 72 73 94 97
感謝各位的閱讀!關(guān)于“怎么用VBS模擬二叉樹”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章標(biāo)題:怎么用VBS模擬二叉樹-創(chuàng)新互聯(lián)
瀏覽地址:http://www.yijiale78.com/article40/dodgeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站維護、微信小程序、App開發(fā)、企業(yè)建站、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容