JavaScript之BST

自己尝试用js实现了数据结构的二叉查找树。

// node
function Node(data) {
    this.data = data;
    this.lc = null;
    this.rc = null;
}

// BST
function BST() {
    this.root = null;
}  
//======================create root node======================
BST.prototype.create = function(num) {
    this.root = new Node(num);
} 
// ======================add tree node=========================
BST.prototype.insert = function(root, num) {
    var node = new Node(num);

    if(root.data < node.data) {
        if(!root.rc) 
            root.rc = node;
        else 
            this.insert(root.rc, num);
    } else {
        if(!root.lc) 
            root.lc = node;
        else 
            this.insert(root.lc, num);
    }
}

var bst = new BST();
var arr = [5,3,6,7,4,1,8];

// create root node
bst.create(arr[0]); 
// create tree
for(var i = 1; i < arr.length; i++) { 
    bst.insert(bst.root, arr[i]);
} 

console.log(bst.root);

第二种

// node
function Node(data) {
    this.data = data;
    this.lc = null;
    this.rc = null;
}

// BST
function BST() {
    this.root = null;
} 

// add tree node
BST.prototype.insert = function(num, bst) {
    var node = new Node(num);  
    var isRootTree = (bst && bst.hasOwnProperty('root')); // 判断传入的是一棵树还是一个节点
    var root = isRootTree ? bst.root : bst;

    if(isRootTree && !root) { // 如果传入的参数为树且该树的root为null 
            bst.root = node; // 初始化root,不能用root = node,
            // 因为这样不会改变bst.root,而是另root变量重新指向了一个新node节点
    } else {
        var target = root.data < num ? 'rc' : 'lc'; // 避免bst为null带来的error
        root[target] == null ? root[target] = node : this.insert(num, root[target]);
     }
}; 

var bst = new BST();
var arr = [5,9,6,7,4,1,8];

for(var i = 0; i < arr.length; i++) { 
    bst.insert(arr[i], bst);
} 
console.log(bst.root);

第三种  通过迭代

// node
function Node(data) {
    this.data = data;
    this.left = null;
    this.rifht = null;
}

// BST
function BST() {
    this.root = null;
}
BST.prototype.insert = function(data){
    var node = new Node(data, null, null);
    if(this.root == null){
        this.root = node;
    }
    else{
        var currNode = this.root;
        var parent; 
        
        while(true){
            parent = currNode;
            if(data < currNode.data){
                currNode = currNode.left;
                if(currNode == null){
                    parent.left = node;
                    break;
                }
            }
            else{
                currNode = currNode.right;
                if(currNode == null){
                    parent.right = node;
                    break;
                }
            }
        }
    }
};

BST.prototype.orderVisit = function(root) {
  if(root) {
      this.orderVisit(root.left);
      console.log(root.data);      
      this.orderVisit(root.right);
  }  
};

var arr = [5,4,6,3,7,2,8];
var bst = new BST();

for(var i = 0; i < arr.length; i++)
    bst.insert(arr[i]);

bst.orderVisit(bst.root);

 

原文地址:https://www.cnblogs.com/wind-lanyan/p/7493026.html