javascript:二叉搜索树 实现

二叉搜索树:顾名思义,树上每个节点最多只有二根分叉;而且左分叉节点的值 < 右分叉节点的值 。

特点:插入节点、找最大/最小节点、节点值排序 非常方便

  1 <script type="text/javascript">// <![CDATA[
  2 //打印输出
  3     function println(msg) {
  4         document.write(msg + " ");
  5     }
  6 
  7     //节点类
  8     var Node = function (v) {
  9         this.data = v; //节点值
 10         this.left = null; //左节点
 11         this.right = null; //右节点
 12     }
 13 
 14     //二叉搜索树类
 15     var BinarySearchTree = function () {
 16         this.root = null; //初始化时,根节点为空
 17         //插入节点
 18         //参数:v 为节点的值
 19         this.insert = function (v) {
 20             var newNode = new Node(v);
 21             if (this.root == null) {
 22                 //树为空时,新节点,直接成为根节点
 23                 this.root = newNode;
 24                 return;
 25             }
 26             var currentNode = this.root; //工作“指针”节点(从根开始向下找)
 27             var parentNode = null;
 28             while (true) {
 29                 parentNode = currentNode;
 30                 if (v < currentNode.data) {
 31                     //当前节点的值 > 目标节点的值                     
 32                     //应该向左插,工作节点移到左节点
 33                     currentNode = currentNode.left;
 34                     if (currentNode == null) {
 35                         //没有左节点,则新节点,直接成为左节点
 36                         parentNode.left = newNode;
 37                         return; //退出循环
 38                     }
 39                 }
 40                 else {
 41                     //否则向右插,工作节点移到右节点
 42                     currentNode = currentNode.right;
 43                     if (currentNode == null) {
 44                         parentNode.right = newNode;
 45                         return;
 46                     }
 47                 }
 48 
 49             }
 50         }
 51 
 52         //查找最小节点
 53         this.min = function () {
 54             var p = this.root; //工作节点    
 55             while (p != null && p.left != null) {
 56                 p = p.left;
 57             }
 58             return p;
 59         }
 60 
 61         //查找最大节点
 62         this.max = function () {
 63             var p = this.root; //工作节点    
 64             while (p != null && p.right != null) {
 65                 p = p.right;
 66             }
 67             return p;
 68         }
 69 
 70         //中序遍历
 71         this.inOrder = function (rootNode) {
 72             if (rootNode != null) {
 73                 this.inOrder(rootNode.left); //先左节点
 74                 println(rootNode.data); //再根节点
 75                 this.inOrder(rootNode.right); //再右节点
 76             }
 77         }
 78 
 79         //先序遍历
 80         this.preOrder = function (rootNode) {
 81             if (rootNode != null) {
 82                 println(rootNode.data); //先根
 83                 this.preOrder(rootNode.left); //再左节点
 84                 this.preOrder(rootNode.right); //再右节点
 85             }
 86         }
 87 
 88         //后序遍历
 89         this.postOrder = function (rootNode) {
 90             if (rootNode != null) {
 91                 this.postOrder(rootNode.left); //先左节点
 92                 this.postOrder(rootNode.right); //再右节点
 93                 println(rootNode.data); //再根节点
 94             }
 95         }
 96     }
 97 
 98 
 99     //以下是测试
100     var bTree = new BinarySearchTree();
101     //《沙特.算法设计技巧与分析》书上图3.9 左侧的树
102    
103     bTree.insert(6);
104     bTree.insert(3);
105     bTree.insert(8);
106     bTree.insert(1);
107     bTree.insert(4);
108     bTree.insert(9);
109    
110     println('中序遍历:')
111     bTree.inOrder(bTree.root);
112 
113     println("<br/>");
114 
115     println("先序遍历:");
116     bTree.preOrder(bTree.root);
117 
118     println("<br/>");
119 
120     println("后序遍历:");
121     bTree.postOrder(bTree.root);
122 
123     println("<br/>");
124     var minNode = bTree.min();
125     println("最小节点:" + (minNode == null ? "不存在" : minNode.data));
126 
127     println("<br/>");
128     var maxNode = bTree.max();
129     println("最大节点:" + (maxNode == null ? "不存在" : maxNode.data));
130 // ]]></script>

 

 输出结果:

中序遍历: 1 3 4 6 8 9
先序遍历: 6 3 1 4 8 9
后序遍历: 1 4 3 9 8 6
最小节点:1
最大节点:9

posted @ 2013-05-19 23:35  菩提树下的杨过  阅读(1268)  评论(0编辑  收藏  举报