Using the VBS to simulate the binary tree, a sort method can be obtained. _vbs

Source: Internet
Author: User
Data structure Knowledge:

The sequence of the binary tree can be used to do the sorting

And the VBS inside exactly there is no ready-made sorting method, so I wrote a vbs with two fork tree, to solve the problem of sorting, the middle order is the calendar is sorted. You can refer to the principle, apply to your own program.

<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 ' Pre-order calendar
Preorder Root
document.write "<br/>"
End Sub
Sub Inordertraversal ' middle order calendar
Inorder Root
document.write "<br/>"
End Sub
Sub Postordertraversal ' The subsequent calendar
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
' Invoke the example

Set T=new tree

document.write "Insert Node"
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 "Pre-order Calendar"
T.preordertraversal
document.write "Middle order Calendar"
T.inordertraversal
document.write "The subsequent calendar"
T.postordertraversal
</SCRIPT>



Insert Node 39 69 94 47 50 72 55 41 97 73
Pre-order Calendar 39 69 47 41 50 55 94 72 73 97
Middle order Calendar 39 41 47 50 55 69 72 73 94 97
Subsequent calendar 41 55 50 47 73 72 97 94 69 39

Rewrite to sort (arr) function

<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 ' middle order calendar
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 all A in arr
T.insertnode A
Next
T.inordertraversal
Sort=t.arr
End Function
'-------above is the Sort function section------
'-------The following is the call example------
' A random array
Arr=array (39,69,94,47,50,72,55,41,97,73)
' Show array contents
For all A in arr
document.write A & ""
Next
document.write "<br/>"
' Sort processing
Arr=sort (arr)
' Show results after sorting
For all A in arr
document.write A & ""
Next
</SCRIPT>

Output results:

39 69 94 47 50 72 55 41 97 73
39 41 47 50 55 69 72 73 94 97

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.