java实现有序二叉树基本操作

 有序二叉树作为二叉树的一种,特点是左子树存储的数据<父节点存储的数据<右子树存储的数据,对这种二叉树进行中序遍历就能得到数据的升序序列,相反,如果想得到数据的降序序列,则子树存储的数据>父节点存储的数据>右子树存储的数据.

有序二叉树的优点是:不仅添加删除元素效率较高,查找效率也较高,类似于二分查找法。

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int arr[]={7,3,10,12,5,1,9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        //遍历数组添加元素到树中
        for(int data:arr){
            binarySortTree.add(new Node(data)); //添加元素
        }
        //先序遍历
        binarySortTree.preOrder();
        System.out.println("---------------");
        //中序遍历
        binarySortTree.midOrder();   //有序二叉树中序遍历才是输出有序数组
        System.out.println("---------------");
        //后序遍历
        binarySortTree.lastOrder();
        System.out.println("----------------");
        //查找节点
        System.out.println(binarySortTree.search(12));
        System.out.println("---------------");
        //删除节点
        binarySortTree.delNode(7);
        //删除后中序遍历
        binarySortTree.midOrder();

    }
}
class BinarySortTree{
    private Node root;
    public void add(Node node){
        if(root==null){
            root=node;
        }else{
            root.add(node);
        }
    }
    //中序遍历
    public void midOrder(){
        if(root!=null){
            root.midOrder();
        }else {
            System.out.println("树为空");
        }
    }
    //先序遍历
    public void preOrder(){
        if(root!=null){
            root.preOrder();
        }else {
            System.out.println("树为空");
        }
    }
    //后序遍历
    public void lastOrder(){
        if(root!=null){
            root.lastOrder();
        }else {
            System.out.println("树为空");
        }
    }
    //查找节点
    public Node search(int value){
        if(root==null){
            System.out.println("树为空");
            return null;
        }
        return root.search(value);
    }
    //删除节点
    public void delNode(int value){
        if(root==null){
            System.out.println("树为空");
            return;
        }
        //找到要删除的节点
        Node targetNode=search(value);
        if(targetNode==null){
            System.out.println("节点不存在");
            return;
        }
        if(root.left==null&&root.right==null){
            //当前树只有一个根节点,且就是要被删除的
            root=null;
            return;
        }
        //找父节点
        Node parent=searchParent(value);

        //如果要删除的节点是叶子节点
        if(targetNode.left==null&&targetNode.right==null){
            //判断targetNode是parent的哪个节点
            if(parent.left!=null&&parent.left==targetNode){
                parent.left=null;
            }else if(parent.right!=null&&parent.right==targetNode){
                parent.right=null;
            }
        }else if(targetNode.left!=null&&targetNode.right!=null){ //如果要删除的节点有两个子树
            //在右子树中找到最小的一个元素替换掉要删除的那个节点
            targetNode.value=delTreeMin(targetNode.right);
        }else {//删除只有一颗子树的节点

            if(targetNode.left!=null){
                if(parent!=null) {
                    //判断targetNode 是parent的哪个节点
                    if (parent.left == targetNode) {
                        parent.left = targetNode.left;
                    } else {
                        parent.right = targetNode.left;
                    }
                }else{
                    root=targetNode.left;
                }
            }else{
                if(parent!=null) {
                    //判断targetNode 是parent的哪个节点
                    if (parent.left == targetNode) {
                        parent.left = targetNode.right;
                    } else {
                        parent.right = targetNode.right;
                    }
                }else{
                    root=targetNode.right;
                }
            }
        }

    }
    //查找节点的父节点
    public Node searchParent(int value){
        if(root==null){
            System.out.println("树为空");
            return null;
        }
        if(root.value==value){
            return null;
        }
        return root.searchParent(value);
    }
    //删除以node为根的排序二叉树的最小值并返回
    public int delTreeMin(Node node){
        Node target=node;
        if(target==null){
            throw new NullPointerException();
        }

        while(target.left!=null){
            target=target.left; //向左遍历
        }
        //删除最小节点
        delNode(target.value);
        return target.value;
    }

}
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }
    //查找节点并返回
    public Node search(int value){
        if(value==this.value){
            return this;
        }else if(value<this.value){ //向左递归
            if(this.left==null){
               return null;
            }
            return this.left.search(value);

        }else{ //向右递归
            if(this.right==null){
                return null;
            }
            return this.right.search(value);
        }
    }
    //查找节点的父节点并返回
    public Node searchParent(int value){
        if((this.left!=null&&this.left.value==value)||(this.right!=null&&this.right.value==value)){
            return this;
        }else{
            if(value<this.value&&this.left!=null){
                //向左递归
                return this.left.searchParent(value);
            }else if(value>=this.value&&this.right!=null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }

    }
    //添加节点
    public void add(Node node){
        if(node==null){
            return;
        }
        //判断传入的节点的值和当前
        if(node.value<this.value){
            if(this.left==null){ //为空直接添加
                this.left=node;
            }else{
                this.left.add(node); //不为空递归添加
            }
        }else{
            if(this.right==null){
                this.right=node;
            }else{
                this.right.add(node);
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    //中序遍历
    public void midOrder(){
        if(this.left!=null){
            this.left.midOrder();
        }

        System.out.println(this);

        if(this.right!=null){
            this.right.midOrder();
        }
    }
    //先序遍历
    public void preOrder(){

        System.out.println(this);

        if(this.left!=null){
            this.left.preOrder();
        }

        if(this.right!=null){
            this.right.preOrder();
        }
    }
    //后序遍历
    public void lastOrder(){
        if(this.left!=null){
            this.left.lastOrder();
        }

        if(this.right!=null){
            this.right.lastOrder();
        }

        System.out.println(this);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值