用VBS模拟二叉树,可以得到一个排序办法.

 更新时间:2007年03月05日 00:00:00   作者:  
数据结构知识:

二叉树中序便历可以用来做排序

而VBS里面恰恰就没有现成的排序方法,因此我写了一个用VBS的二叉树,来解决排序问题,中序便历就是排序。大家可以参考原理,应用到自己的程序中。

<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 "&nbsp;" & 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 "&nbsp;" & 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 "&nbsp;" & N.data   
 end sub
end class
'调用示例

set T=new tree

document.write  "插入节点"
arr=array(39,69,94,47,50,72,55,41,97,73)
for i=0 to 9
 document.write "&nbsp;" & arr(i)
 T.insertNode  arr(i) 
next
document.write "<br/>"
document.write  "前序便历"
T.preOrderTraversal 
document.write  "中序便历"
T.inOrderTraversal
document.write  "后序便历"
T.postOrderTraversal 
 </SCRIPT>

 

插入节点 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)函数 

 <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函数部分------
 '-------以下是调用示例------
 '随便一个数组
arr=array(39,69,94,47,50,72,55,41,97,73)
 '显示数组内容
for each a in arr 
  document.write  a & "&nbsp;"
next
document.write  "<br/>" 
 '排序处理
arr=sort(arr)
 '显示排序后的结果
for each a in arr 
  document.write  a & "&nbsp;"
next
 </SCRIPT>

输出结果:

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

相关文章

  • VBScript开发自动化测试脚本的方法分析

    VBScript开发自动化测试脚本的方法分析

    很多人都觉得微软的VBScript功能比较弱,如果从开发自动化测试的角度来讲,更是不可能了。从我对VBScript脚本的了解来看,对一个脚本是否可以做自动化测试需要这个测试人员具备两方面的知识,第一个,就是对VBScript脚本开发语言精通,要了解VBScript的核心技术如何更广泛的应用;第二个,就是要有自动化的测试思想,这个一定得是做过测试工作的能够了解的多一些。
    2008-03-03
  • 一段病毒常用的VBS代码

    一段病毒常用的VBS代码

    vbs病毒文件常用的代码,给这段代码不是想让大家做病毒,而是让大家学习一下它的代码,充分了解vbs技术。
    2009-09-09
  • 什么是 WSH(脚本宿主)的详细解释

    什么是 WSH(脚本宿主)的详细解释

    什么是 WSH(脚本宿主)的详细解释...
    2007-02-02
  • vbs操作txt文本文件常用方法与函数代码

    vbs操作txt文本文件常用方法与函数代码

    这篇文章主要介绍了vbs操作txt文本文件常用方法与函数代码,需要的朋友可以参考下
    2018-06-06
  • VBScript语法速查及实例说明

    VBScript语法速查及实例说明

    VBScript语法速查及实例说明...
    2006-11-11
  • vbs-toolkit VBSEdit 提供 免费的COM组件

    vbs-toolkit VBSEdit 提供 免费的COM组件

    VBSCRIPT 语法简单 强大 但是功能上明显不足 需要第三方的控制 e.g. COM 组件来扩展其功能. VBSEDIT 安装完之后就可以在安装目录下发现 免费提供的 COM 组件 vbs toolkit
    2018-06-06
  • VBS基础篇 - vbscript队列

    VBS基础篇 - vbscript队列

    VBS中的队列需要使用System.Collections.Queue,包含队列的添加元素(入队)、删除元素(出队)、遍历、统计元素个数、清空 ,需要的朋友可以参考下
    2018-05-05
  • 视频转换大师WinMPG Video Convert 6.63

    视频转换大师WinMPG Video Convert 6.63

    视频转换大师WinMPG Video Convert 6.63...
    2007-04-04
  • 用VBS模拟实现PHP的sha1_file函数效果代码

    用VBS模拟实现PHP的sha1_file函数效果代码

    用VBS模拟实现PHP的sha1_file函数效果代码,需要的朋友可以参考下。
    2011-01-01
  • vbscript 读取xml格式的配置文件

    vbscript 读取xml格式的配置文件

    最近一项目中,vbs脚本需要读取配置文件,本来考虑用ini来做配置文件,但是vbs里没有现成读写ini文件的支持,于是考虑用xml来做配置文件,使用xmldom来读取。写成个class使用起来应该方便一些。贴来备忘一下。
    2009-02-02

最新评论