zoukankan      html  css  js  c++  java
  • js 二叉搜索树

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

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

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

     输出结果:

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

    转自http://www.cnblogs.com/yjmyzz/archive/2013/05/19/3087832.html

  • 相关阅读:
    关于cookie的一点知识
    一点简单的关于ASP.NET下载
    一个小小小问题
    复习linq
    拾遗一些关于存储过程
    一般处理程序(下)
    复习一下递归
    复习JS和jQuery
    一点关于Ajax和一个等待图标的显示
    C#开发BIMFACE系列14 服务端API之批量获取转换状态详情
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4699394.html
Copyright © 2011-2022 走看看