Javascript二叉树和二叉查找树

树是一种在计算机中广泛应用的非线性数据结构,数据以层次结构存储(hierarchical),磁盘的文件目录就是典型的树结构。和字典不同,树支持对数据进行有序存储。
二叉树是最典型的树结构,其它类型的树和其原理相似。相比线性结构,二叉树的优势在于高效插入、删除和查找。


二叉树的js实现

function Node(data, left, right) {
    this.data = data;
    this.left = left;
    this.right = right;
    this.show = show;
}

function show() {
    return this.data;
}

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

function insert(data) {
    var n = new Node(data, null, null);
    if (this.root == null) {
         this.root = n;
     } 
    else {
         var current = this.root;
         var parent;
         while (true) {
             parent = current;
             if (data < current.data ) { // 小于根,左子树
                 current = current.left;
                 if (current == null) {
                     parent.left = n;
                     break;
                 }
             }
             else {  // 右子树
                     current = current.right;
                     if (current == null) {
                         parent.right = n;
                         break;
                     }
              }
         } // end while
    }  // end else
}

function inOrder(node) {
    if (!(node==null)) {
        inOrder(node.left);
        print(node.show());
        inOrder(node.right);
    }
}

function preOrder(node) {
    if (!(node == null)) {
        print(node.show());
        preOrder(node.left);
        preOrder(node.right);
    }
}

function postOrder(node) {
   if (!(node == null)) {
      postOrder(node.left);
      postOrder(node.right);
      print(node.show());
   }
}

function getMin() {
    var cur = this.root;
    while(! (cur.left == null)) cur = cur.left;
    return cur.data;
}
 
function getMax() {
    var cur = this.root;
    while(!(cur.right==null)) cur = cur.right;
    return cur.data;
}
      
function find(data) {
    var cur = this.root;
    while(cur.data != data) {
        if (data < cur.data) {
            cur = cur.left;
        }
        else {
            cur = cur.right;
        }

        if (cur == null) {
            return null;
        }
    }
}
// 删除节点
function remove(data) {
    this.root = removeNode(this.root, data);
}
// 递归实现
function removeNode(node, data) {
    if (node == null) {
        return null;
    }
    if (data == node.data) {
        //没有孩子
        if (node.left == null && node.right == null) {
            return null;
        }
        // 只有左儿子
        if (node.left == null) {
            return node.right;
        }
        // 只有右儿子
        if (node.right == null) {
            return node.left;
        }
        // 有左右孩子
     var tmp = getSmallest(node.right);
        node.data = tmp.data;
        node.right = removeNode(node.right, tmp.data);
        return node;
    }
    else if (data < node.data) {
        node.left = removeNode(node.left, data);
        return node;
    }
    else {
        node.right = removeNode(node.right, data);
        return node;
    }
}

function getSmallest(node) {
    var cur = node;
    while (cur.left != null) { cur = cur.left; }
    return cur;
}

注意树的算法很多是递归实现的,尤其注意树的节点删除算法,要清楚的考虑三种情况,即删除的节点是否包含左右孩子。

你可能感兴趣的:(Javascript二叉树和二叉查找树)