二叉树的排序

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>二叉树排序</title>
  <script>
    var arr=[5,3,17,1,29,38,109,34,6];
    function Node(vv){
        this.value=vv;
        this.left=null;
        this.right=null;
        this.addNode=function(val){
            if(val<this.value){
                if(this.left!=null){
                    this.left.addNode(val);
                }else{
                    this.left=new Node(val);
                }
            }else{
                if(this.right!=null){
                    this.right.addNode(val);
                }else{
                    this.right=new Node(val);
                }
            }
        }
        this.getValue=function(){
            if(this.left!=null){
                this.left.getValue();
            }
            document.write(this.value+",");
            if(this.right!=null){
                this.right.getValue();
            }
        }
    }
    
    window.onload=function(){
        var len = arr.length;
        var node = new Node(arr[0]);
        for(var i=1;i<len;i++){
            node.addNode(arr[i]);
        }
        node.getValue();
    }
    
    //----递归-----n+m----------
    function add(sum,ii){
        if(ii<=100){
            sum += ii;
            ii++;
            add(sum,ii);
        }else{
            return sum;
        }
    }
  </script>
 </head>
 <body>
  
 </body>
</html>

 

posted @ 2017-08-29 14:17  开始战斗  阅读(246)  评论(0编辑  收藏  举报