二叉搜索树代码

二叉搜索树简介:
一个根-下面子节点最多为2 也可为空
插入时左边小右边大
二叉搜索树 具有3个属性 数据、左节点、右节点 类似于双向链表,如果学过链表的同学会更容易理解二叉树

查询的时候具有有序性 有3种类型
前序:根左右
中序:左根右
后序:左右根

下面的代码 删除时 会删除所有的子节点

代码

package com.tuziyoukexing.rabbit.test.erchashu;

/**
 * Desciption:
 * Created by tuziyoukexing on 2019-09-03
 * email 1242062779@qq.com
 */
public class BinaryTree {

    Node root;
    transient int size;

    public void add(int data) {
        if (this.root == null)
            this.root = new Node(data);
        else
            this.root.add(data);
        size++;
    }

    public void afterPrint() {
        this.root.afterPrint();
    }

    public void remove(int data) {
        this.root.remove(root, data);
    }

    private static class Node {
        Integer data;
        Node left;
        Node right;


        public Node(int data) {
            this.data = data;
        }

        public void add(int data) {
            if (this.data > data) {
                if (this.left == null)
                    this.left = new Node(data);
                else
                    this.left.add(data);
            } else if (this.data < data) {
                if (this.right == null)
                    this.right = new Node(data);
                else
                    this.right.add(data);
            }

        }

        public void afterPrint() {
            if (this.left != null) {
                this.left.afterPrint();
            }
            System.out.print(this.data + " ");
            if (this.right != null) {
                this.right.afterPrint();
            }
        }

        public Node remove(Node node, int data) {
            if (node == null)
                return null;

            if (node.data > data) {//e<node.e (被删除元素e小于当前节点值e)
                node.left = remove(node.left, data);
                return node;
            }
            if (node.data < data) {//e>node.e (被删除元素e大于当前节点值e)
                node.right = remove(node.right, data);
                return node;
            } else {
                //待删除节点左子树为空情况
                if (node.left == null) {
                    Node rightNode = node.right;
                    node.right = null;
                    node.data = null;
                    return rightNode;
                }

                //待删除节点右子树为空情况
                if (node.right == null) {
                    Node leftNode = node.left;
                    node.left = null;
                    node.data =null;
                    return leftNode;
                }


                //左右子树均不为空
                //方法:找到比待删除节点大的最小节点,即待删除节点右子树的最小节点
                //用这个节点顶替待删除节点的位置
                Node successor = (Node) minimum(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.left = node.right = null;
                node = null;
            }

            return null;
        }

    }


    // 寻找二分搜索树的最小元素
    public int minimum() {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty");
        }

        Node ninNode = minimum(root);
        return ninNode.data;
    }

    // 返回以node为根的二分搜索树的最小值所在的节点
    public static Node minimum(Node node) {
        if (node.left == null) {
            return node;
        }

        //返回相应的节点的左子树的最小值
        return minimum(node.left);
    }

    public int removeMin() {
        int ret = minimum();//获取最小元素
        root = removeMin(root);

        return ret;
    }

    // 删除掉以node为根的二分搜索树中的最小节点
    // 返回删除节点后新的二分搜索树的根
    private static Node removeMin(Node node) {

        // 递归的终止条件,当前节点没有左子树了,那么就是最小节点了
        // 如果是最小节点,我们要做的是删除当前节点,但是当前节点很可能是有右子树的
        // 我们先把该节点的右子树节点保存,然后就删除掉该右子树节点,最后把右子树节点返回即可
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null; //左节点为空了,让右子树也为空,相当于脱离了树
            return rightNode;//返回右子树是为了后面的绑定操作
        }

        // 没有递归到底的情况,那么就递归调用其左子树,这个调用的过程会返回被删除节点的右子树,
        //将返回的右子树重新绑定到上一层的node的左节点上就相当于彻底删除了那个元素
        node.left = removeMin(node.left);

        return node;// 删除后,根节点依然是node,返回即可
    }

}

好了这就是java的二叉搜索树

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值