BinarySearchTree

//创建内部类Node,Node代表的就是树节点
public static class Node{
    int key;
    Node leftChild;
    Node rightChild;

    public Node(int key){
        this.key=key;
        this.leftChild=null;
        this.rightChild=null;
    }

    public Node(int key,Node leftChild,Node rightChild){
        this.key=key;
        this.leftChild=leftChild;
        this.rightChild=rightChild;
    }


    public int getKey(){
        return key;
    }

    @Override
    public boolean  equals(Object o){
        Node node=(Node)o;
        return getKey() == node.key;
    }

    public Node left(){
        return this.leftChild;
    }

    public Node right(){
        return this.rightChild;
    }
}

//成员变量以及构造函数
public Node root;
public BinarySearchTree(){
    root=null;
}

public boolean isEmpty(){
    return root == null ? true : false;
}

public void makeEmpty(){
    this.root=null;
}

public boolean contains(int target,Node node){
    if(node == null || isEmpty()){
        return false;
    }
    if(target<node.key){
        return contains(target, node.leftChild);
    }else if(target>node.key){
        return contains(target,node.rightChild);
    }else{
        return true;
    }
}

public Node findMin(Node node){
    if(node==null || isEmpty()){
        return null;
    }
    if(node!=null && node.leftChild!=null) {
        return findMin(node.leftChild);
    }else{
        return node;
    }
}

public Node findMax(Node node){
    if(node==null || isEmpty()){
        return null;
    }
    //这就是返回条件(递归结束条件)
    if(node.rightChild == null)
        return node;
    return findMax(node.rightChild);
}

//整体思路就是插入哪一个节点,就返回哪一个节点
public Node insert(int key,Node node){

    if(node==null){
        //用于真实生成节点(递归结束条件)
        return new Node(key,null,null);
    }else if(key < node.key){
        //用于建立与左节点间的关联
        node.leftChild=insert(key, node.leftChild);
    }else if(key > node.key){
        //用于建立与右节点间的关联
        node.rightChild=insert(key, node.rightChild);
    }else ;

    return node;
}

public void insert(int key){
    root=insert(key,root);
}
//思路与插入思路差不多,删除那个节点就返回哪一个节点
public Node remove(int key,Node node){

    if(node == null) return null;
    if(key < node.key){
        node.leftChild=remove(key, node.leftChild);
    }else if(key > node.key){
        node.rightChild=remove(key, node.rightChild);
    }
    //左右子树均非空
    else if(node.leftChild != null && node.rightChild != null){
        //找到要移动的节点并替换掉,该过程出现新一轮的树枝生成过程
        node.key=findMin(root.rightChild).key;
        //这里负责新树枝的生成,因为这里要移动的是该节点的右分支,所以其实永远不会有删除动作发生,只会发生一点树枝的移动动作
        node.rightChild=remove(node.key, node.rightChild);
    }
    //左子树或者右子树为空,此时node节点即为要删除的节点或者不存在的删除节点
    else{
        node = (node.rightChild == null) ? node.leftChild : node.rightChild;
    }

    return node;
}

public void remove(int key){
    root=remove(key, root);
}

以上就简单介绍了二人查找数的基本操作,当然用的是递归实现,但是也可以用到非递归的方法,这里不再给出。

转载于:https://my.oschina.net/u/2288283/blog/639802

以下是Java实现BinarySearchTree的delete方法的示例代码: ``` public class BinarySearchTree { private Node root; private class Node { private int key; private Node left, right; public Node(int key) { this.key = key; } } public void delete(int key) { root = delete(root, key); } private Node delete(Node x, int key) { if (x == null) return null; if (key < x.key) { x.left = delete(x.left, key); } else if (key > x.key) { x.right = delete(x.right, key); } else { if (x.left == null) return x.right; if (x.right == null) return x.left; Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; } return x; } private Node min(Node x) { if (x.left == null) return x; return min(x.left); } private Node deleteMin(Node x) { if (x.left == null) return x.right; x.left = deleteMin(x.left); return x; } } ``` 在该实现中,delete方法首先调用delete(Node x, int key)方法,传入根节点和待删除的键。 delete(Node x, int key)方法递归遍历二叉搜索树,查找要删除的键所对应的节点。如果找到了该节点,就执行删除操作。如果节点有左右子树,就用它的右子树中的最小值节点来替代这个节点,然后删除右子树中的最小值节点。 delete(Node x, int key)方法还调用了min(Node x)和deleteMin(Node x)方法。min(Node x)方法返回以节点x为根的子树中的最小节点,deleteMin(Node x)方法删除以节点x为根的子树中的最小节点,并返回删除后的子树。 注意:以上代码仅供参考,未经完整测试,可能存在错误,请谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值