二叉排序树-java语言描述

二叉排序树

1、概念

​ 对于二叉排序树的任何一个非叶子节点,当前节点的左节点小于当前节点,当前节点的右节点大于当前节点

​ 注意:如果有与当前节点相同的值的节点时,则该节点放在当前节点的左右节点都可以

2、对二叉排序树的操作
2.1、添加节点
	/**
     * 添加节点
     *
     * @param node 需要添加的值
     */
    public void add(Nodes node) {
        if (node == null) {
            return;
        }
        //判断传入的节点与当前节点的关系来决定如何插入
        if (node.getValue() < this.value) {
            //如果当前节点的左节点为空
            if (this.getLeft() == null) {
                this.setLeft(node);
            } else {
                this.getLeft().add(node);
            }
        } else {
            //如果当前节点的右节点为空
            if (this.getRight() == null) {
                this.setRight(node);
            } else {
                this.getRight().add(node);
            }
        }
    }
2.2、删除节点
	/**
     * 查找要删除的节点
     *
     * @param value 要查找的值
     * @return 返回被找到的值
     */
    public Nodes search(int value) {
        if (this.getValue() == value) {
            return this;
        } else if (this.getValue() > value) {     //向左子节点查找
            if (this.getLeft() == null) {
                return null;
            } else {
                //向左子树递归查找
                return this.getLeft().search(value);
            }
        } else {                                 //向右子节点查找
            if (this.getRight() == null) {
                return null;
            } else {
                //向右子树递归查找
                return this.getRight().search(value);
            }
        }
    }

    /**
     * 查找要删除节点的父节点
     *
     * @param value 要查找的节点的值
     * @return 返回被查找到的节点的父节点
     */
    public Nodes searchParent(int value) {
        //当前节点的子节点就是要删除的节点
        if ((this.getLeft() != null && this.getLeft().getValue() == value) ||
                (this.getRight() != null && this.getRight().getValue() == value)) {
            return this;
        } else {
            //当前节点的值大于value,且当前节点的左节点不为空
            if (this.getValue() > value && this.getLeft() != null) {
                return this.getLeft().searchParent(value);
                //当前节点的值小于value,且当前节点的右节点不为空
            } else if (this.getValue() < value && this.getRight() != null) {
                return this.getRight().searchParent(value);
                //没有找到符合条件的
            } else {
                return null;
            }
        }
    }

    /**
     * @param nodes 传入的节点当作该二叉树的根节点
     * @return 返回以 nodes为根节点的二叉排序树的最小节点的值
     */
    public int treeMin(Nodes nodes) {
        Nodes target = nodes;
        //循环查找左子节点
        while (target.getLeft() != null) {
            target = target.getLeft();
        }
        //删除这个值最小的节点
        deleteNode(target.getValue());
        return target.getValue();
    }

	//删除节点
    public void deleteNode(int value) {
        if (root == null) {
            return;
        } else {
            //1、先找到需要删除的节点
            Nodes target = search(value);

            //如果没找到需要删除的节点
            if (target == null) {
                return;
            }
            //只有一个节点
            if (root.getLeft() == null && root.getRight() == null) {
                root = null;
                return;
            }

            //2、找到需要删除节点的父节点
            Nodes parent = searchParent(value);

            //判断要删除的节点位于该二叉树的哪个位置
            //1、要删除的节点是叶子节点
            if (target.getLeft() == null && target.getRight() == null) {
                //判断target是它父节点的左子节点还是右子节点
                if (parent.getLeft() != null && parent.getLeft().getValue() == value) {
                    parent.setLeft(null);
                } else if (parent.getRight() != null && parent.getRight().getValue() == value) {
                    parent.setRight(null);
                }
            }
            //2、要删除的节点有一个子树
            if ((target.getLeft() != null && target.getRight() == null) ||
                    (target.getLeft() == null && target.getRight() != null)) {
                //判断target的子节点是否有子节点
                if (target.getLeft() != null) {
                    //判断target是parent的左子节点还是右子节点
                    if (parent.getLeft().getValue() == value) {
                        parent.setLeft(target.getLeft());
                    } else {
                        parent.setRight(target.getLeft());
                    }
                } else {
                    //判断target是parent的左子节点还是右子节点
                    if (parent.getLeft().getValue() == value) {
                        parent.setLeft(target.getRight());
                    } else {
                        parent.setRight(target.getRight());
                    }
                }
            }
            //3、要删除的节点有两个子树
            if (target.getLeft() != null && target.getRight() != null) {
                //这个是用的3实现的
                int minValue = treeMin(target.getRight());
                target.setValue(minValue);
            }
        }
    }

​ 下面的这些操作与我之前文章当中 树-java语言描述 之中是一样的。

2.3、前序遍历
2.4、中序遍历
2.5、后序遍历
2.6、前序查找
2.7、中序查找
2.8、后序查找
3、完整的源码
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] array = {7, 3, 10, 1, 5, 9, 12, 2};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < array.length; i++) {
            binarySortTree.add(new Nodes(array[i]));
        }
        //遍历
        System.out.print("前序遍历:");
        binarySortTree.preOrder();
        System.out.println();
        System.out.print("中序遍历:");
        binarySortTree.middleOrder();
        System.out.println();
        System.out.print("后序遍历:");
        binarySortTree.afterOrder();
        System.out.println();
        System.out.println("-------------------");

        //删除
//        System.out.print("删除前:");
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.print("删除后:");
//        binarySortTree.deleteNode(2);
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.println("-------------------");

//        System.out.print("删除前:");
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.print("删除后:");
//        binarySortTree.deleteNode(1);
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.println("-------------------");

//        System.out.print("删除前:");
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.print("删除后:");
//        binarySortTree.deleteNode(10);
//        binarySortTree.middleOrder();
//        System.out.println();
//        System.out.println("-------------------");
    }
}

//二叉排序树
class BinarySortTree {
    private Nodes root;

    //添加节点的办法
    public void add(Nodes nodes) {
        if (root == null) {
            root = nodes;
        } else {
            root.add(nodes);
        }
    }

    //前序遍历
    public void preOrder() {
        if (root != null) {
            root.preOrder();
        } else {
            System.out.println("二叉排序树为空,不能遍历!");
        }
    }

    //中序遍历
    public void middleOrder() {
        if (root != null) {
            root.middleOrder();
        } else {
            System.out.println("二叉排序树为空,不能遍历!");
        }
    }

    //后序遍历
    public void afterOrder() {
        if (root != null) {
            root.afterOrder();
        } else {
            System.out.println("二叉排序树为空,不能遍历!");
        }
    }

    //查找要被删除的节点
    public Nodes search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要被删除节点的父节点
    public Nodes searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    /**
     * @param nodes 传入的节点当作该二叉树的根节点
     * @return 返回以 nodes为根节点的二叉排序树的最小节点的值
     */
    public int treeMin(Nodes nodes) {
        Nodes target = nodes;
        //循环查找左子节点
        while (target.getLeft() != null) {
            target = target.getLeft();
        }
        //删除这个值最小的节点
        deleteNode(target.getValue());
        return target.getValue();
    }

    //删除节点
    public void deleteNode(int value) {
        if (root == null) {
            return;
        } else {
            //1、先找到需要删除的节点
            Nodes target = search(value);

            //如果没找到需要删除的节点
            if (target == null) {
                return;
            }
            //只有一个节点
            if (root.getLeft() == null && root.getRight() == null) {
                root = null;
                return;
            }

            //2、找到需要删除节点的父节点
            Nodes parent = searchParent(value);

            //判断要删除的节点位于该二叉树的哪个位置
            //1、要删除的节点是叶子节点
            if (target.getLeft() == null && target.getRight() == null) {
                //判断target是它父节点的左子节点还是右子节点
                if (parent.getLeft() != null && parent.getLeft().getValue() == value) {
                    parent.setLeft(null);
                } else if (parent.getRight() != null && parent.getRight().getValue() == value) {
                    parent.setRight(null);
                }
            }
            //2、要删除的节点有一个子树
            if ((target.getLeft() != null && target.getRight() == null) ||
                    (target.getLeft() == null && target.getRight() != null)) {
                //判断target的子节点是否有子节点
                if (target.getLeft() != null) {
                    //判断target是parent的左子节点还是右子节点
                    if (parent.getLeft().getValue() == value) {
                        parent.setLeft(target.getLeft());
                    } else {
                        parent.setRight(target.getLeft());
                    }
                } else {
                    //判断target是parent的左子节点还是右子节点
                    if (parent.getLeft().getValue() == value) {
                        parent.setLeft(target.getRight());
                    } else {
                        parent.setRight(target.getRight());
                    }
                }
            }
            //3、要删除的节点有两个子树
            if (target.getLeft() != null && target.getRight() != null) {
                //这个是用的3实现的
                int minValue = treeMin(target.getRight());
                target.setValue(minValue);
            }
        }
    }
}

//节点类
class Nodes {
    private int value;
    private Nodes left;
    private Nodes right;

    public Nodes(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Nodes getLeft() {
        return left;
    }

    public void setLeft(Nodes left) {
        this.left = left;
    }

    public Nodes getRight() {
        return right;
    }

    public void setRight(Nodes right) {
        this.right = right;
    }

    /**
     * 添加节点
     *
     * @param node 需要添加的值
     */
    public void add(Nodes node) {
        if (node == null) {
            return;
        }
        //判断传入的节点与当前节点的关系来决定如何插入
        if (node.getValue() < this.value) {
            //如果当前节点的左节点为空
            if (this.getLeft() == null) {
                this.setLeft(node);
            } else {
                this.getLeft().add(node);
            }
        } else {
            //如果当前节点的右节点为空
            if (this.getRight() == null) {
                this.setRight(node);
            } else {
                this.getRight().add(node);
            }
        }
    }

    //前序遍历
    public void preOrder() {
        System.out.print(this.getValue() + "->");
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 查找要删除的节点
     *
     * @param value 要查找的值
     * @return 返回被找到的值
     */
    public Nodes search(int value) {
        if (this.getValue() == value) {
            return this;
        } else if (this.getValue() > value) {     //向左子节点查找
            if (this.getLeft() == null) {
                return null;
            } else {
                //向左子树递归查找
                return this.getLeft().search(value);
            }
        } else {                                 //向右子节点查找
            if (this.getRight() == null) {
                return null;
            } else {
                //向右子树递归查找
                return this.getRight().search(value);
            }
        }
    }

    /**
     * 查找要删除节点的父节点
     *
     * @param value 要查找的节点的值
     * @return 返回被查找到的节点的父节点
     */
    public Nodes searchParent(int value) {
        //当前节点的子节点就是要删除的节点
        if ((this.getLeft() != null && this.getLeft().getValue() == value) ||
                (this.getRight() != null && this.getRight().getValue() == value)) {
            return this;
        } else {
            //当前节点的值大于value,且当前节点的左节点不为空
            if (this.getValue() > value && this.getLeft() != null) {
                return this.getLeft().searchParent(value);
                //当前节点的值小于value,且当前节点的右节点不为空
            } else if (this.getValue() < value && this.getRight() != null) {
                return this.getRight().searchParent(value);
                //没有找到符合条件的
            } else {
                return null;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值