java实现二叉排序树

java实现二叉排序树

二叉排序树(Binary Sort Tree)或者是一颗空树;或者是具有如下性质的二叉树:

(1) 若它的左子树不空,则 左子树上所有结点的值 均小于它的根结点的值;

(2) 若它的右子树不空,则 右子树上所有结点的值 均大于它的根结点的值;

(3) 它的 左、右子树又分别为二叉排序树。

下图中的这颗二叉树就是一颗典型的二叉排序树:

在这里插入图片描述

初始时是 无序序列
在这里插入图片描述
上面构造出的二叉排序树的中序遍历结果.

下面是java的代码实现,具有删除、查询、添加等功能.
//二叉排序树
public class BinarySortTree {

    public static void main(String[] args) {

        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinaryTree binaryTree = new BinaryTree();
        for (int i : arr) {
            binaryTree.add(new Node(i));
        }
        binaryTree.infixOrder();

        //删除叶子节点测试
//        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
//        binaryTree.delNode(2);
//        binaryTree.delNode(7);
//        binaryTree.infixOrder();
    }
}

class BinaryTree {
    private Node root;

    public BinaryTree() {
    }

    public BinaryTree(Node root) {
        this.root = root;
    }

    //查找待删除结点
    public Node sreach(int value) {
        if (root == null) {
            return null;
        } else {
            return root.sreach(value);
        }
    }

    //查找待删除结点的父节点
    public Node sreachParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.sreachParent(value);
        }
    }

    /**
     * 查找以当前结点为根结点的二叉树的最小值,并删除该结点
     *
     * @param node 需要查找的根节点
     * @return 返回以node为根节点的二叉树的最小值
     */
    public int delRightTreeMin(Node node) {
        Node target = node.right;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    /**
     * 查找当前结点为根节点的二叉树的最大值,并删除该结点
     *
     * @param node 需要查找的二叉树的根节点
     * @return 返回以node为根节点的二叉树的最大值
     */
    public int delLeftTreeMax(Node node) {
        Node target = node.left;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    public void delNode(int value) {
        if (root == null) {
            return;
        }
        Node target = sreach(value);
        Node parent = sreachParent(value);
        if (target != null && parent != null) {
            //判断当前结点是不是叶子结点
            if (target.left == null && target.right == null) {
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (target.left != null && target.right != null) {
                target.value = delRightTreeMin(target);
            } else {
                if (parent.left != null && parent.left.value == value && target.left != null) {
                    parent.left = target.left;
                } else if (parent.left != null && parent.left.value == value && target.right != null) {
                    parent.left = target.right;
                } else if (parent.right != null && parent.right.value == value && target.right != null) {
                    parent.right = target.right;
                } else if (parent.right != null && parent.right.value == value && target.left != null) {
                    parent.right = target.left;
                }
            }
        } else if (target != null && parent == null) {
            //判断当前结点是不是叶子结点
            if (target.left == null && target.right == null) {
                target = null;
            } else if (target.left != null && target.right != null) {
                target.value = delRightTreeMin(target);
            } else {
                if (target.left != null) {
                    target = target.left;
                } else if (target.right != null) {
                    target = target.right;
                }
            }
        }


    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("二叉树为空,中序遍历失败!");
        } else {
            root.infixOrder(root);
        }
    }
}

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

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

    //查找当前结点
    public Node sreach(int value) {
        if (this.value == value) {
            return this;
        } else {
            if (this.left != null && value < this.value) {
                return this.left.sreach(value);
            } else if (this.right != null && this.value < value) {
                return this.right.sreach(value);
            } else {
                return null;
            }
        }
    }

    //查找当前结点的父节点
    public Node sreachParent(int value) {
        if (this.left == null && this.right == null) {
            return null;
        } else {
            if (this.left != null && this.left.value == value) {
                return this;
            } else if (this.right != null && this.right.value == value) {
                return this;
            } else if (this.left != null && this.value > value) {
                return this.left.sreachParent(value);
            } else if (this.right != null && this.value < value) {
                return this.right.sreachParent(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);
            }
        }
    }

    //中序遍历二叉树
    public void infixOrder(Node root) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            root.left.infixOrder(root.left);
        }
        System.out.println(root);
        if (root.right != null) {
            root.right.infixOrder(root.right);
        }
    }

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
排序(Binary Search Tree,BST)是一种特殊的二,它满足以下条件: - 左子中的所有节点的值均小于根节点的值; - 右子中的所有节点的值均大于根节点的值; - 左右子也分别为二排序Java代码实现排序: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class BinarySearchTree { private TreeNode root; // 根节点 // 插入节点 public void insert(int val) { if (root == null) { // 如果根节点为空,直接插入 root = new TreeNode(val); return; } TreeNode cur = root; while (cur != null) { if (val < cur.val) { // 小于当前节点,往左子找 if (cur.left == null) { cur.left = new TreeNode(val); return; } cur = cur.left; } else { // 大于等于当前节点,往右子找 if (cur.right == null) { cur.right = new TreeNode(val); return; } cur = cur.right; } } } // 查找节点 public boolean search(int val) { TreeNode cur = root; while (cur != null) { if (val == cur.val) { return true; } else if (val < cur.val) { cur = cur.left; } else { cur = cur.right; } } return false; } // 删除节点 public void delete(int val) { root = deleteNode(root, val); } private TreeNode deleteNode(TreeNode root, int val) { if (root == null) { // 没有找到要删除的节点 return null; } if (val < root.val) { // 继续在左子中删除 root.left = deleteNode(root.left, val); } else if (val > root.val) { // 继续在右子中删除 root.right = deleteNode(root.right, val); } else { // 找到要删除的节点 if (root.left == null) { // 左子为空或者左右子都为空 return root.right; } else if (root.right == null) { // 右子为空 return root.left; } else { // 左右子都不为空 TreeNode minNode = findMin(root.right); // 找到右子中最小的节点 root.val = minNode.val; // 用右子中最小的节点代替要删除的节点 root.right = deleteNode(root.right, minNode.val); // 删除右子中最小的节点 } } return root; } // 找到以root为根的二搜索的最小节点 private TreeNode findMin(TreeNode root) { while (root.left != null) { root = root.left; } return root; } } ``` 以上代码实现了二排序的基本操作:插入节点、查找节点和删除节点。其中删除节点需要考虑三种情况:左子为空、右子为空和左右子都不为空。对于第三种情况,需要在右子中找到最小的节点代替要删除的节点,然后再删除右子中最小的节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值