数据结构与算法的JavaScript描述——二叉树和二叉查找树

数据结构与算法的JavaScript描述——二叉树和二叉查找树

说明:以下部分均为《数据结构与算法的JavaScript描述》学习内容及笔记。

1、为什么要用二叉树?

  • 二叉树查找、删除、添加非常快。

2、实现二叉树

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

3、BST类

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;
                }
            }
        }
    }
}
3.1 中序遍历
function inOrder(node){
    if(!(node==null)){
        inOrder(node.left);
        print(node.show() + ",");
        inOrder(node.right);
    }
}
3.2 前序遍历
function preOrder(node){
    if(!(node==null)){
        preOrder(node.left);
        print(node.show() + " ");
        preOrder(node.right);
    }
}
3.3 后序遍历
function postOrder(node){
    if(!(node==null)){
        postOrder(node.left);
        print(node.show() + ",");
        postOrder(node.right);
    }
}

4、二叉查找树上进行查找

4.1 查找最大值和最小值
function getMin(){
    var current=this.root;
    while(!(current.left==null)){
        current=current.left;
    }
    return current.data;
}
function getMax(){
    var current=this.root;
    while(!(current.right==null)){
        current=current.right;
    }
    return current.data;
}
4.2 查找给定值
function find(data){
    var current=this.root;
    while(current!=null){
        if(current.data==data){
            return current;
        }else if(data<current.data){
            current=current.left;
        }else{
            current=current.right;
        }
    }
    return null;
}

5、二叉查找树上进行删除

function remove(data){
    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 tempNode=getSmallest(node.right);
        //替换
        node.data=tempNode.data;
        //删除右节点中用来替换的那个子节点
        node.right=removeNode(node.right, tempNode.data);
        return node;
    }else if(data<node.data){
        node.left==removeNode(node.left,data);
        return node;
    }else{
        node.right=removeNode(node.right,data);
    }
}
//返回该节点的最小子节点
function getSmallest(node){
    var current=node;
    while(!(current.left==null)){
        current=current.left;
    }
    return current;
}

猜你喜欢

转载自blog.csdn.net/liyuxing6639801/article/details/79835170