如何在Java中操作二叉搜索树

发布时间:2021-05-24 16:01:12 作者:Leah
来源:亿速云 阅读:157

如何在Java中操作二叉搜索树?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一、二叉搜索树插入元素

     class Node {
        int val;
        Node left;
        Node right;

        Node(int val) {
            this.val = val;
        }
    }
    public void insert(int key) {
        Node node = new Node(key);
        if (this.root == null) {
            root = node;
        }
        Node cur = root;
        Node parent = null;
        while (cur != null) {
            if (cur.val == key) {
                //System.out.println("元素已经存在");
                return;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                parent = cur;
                cur = cur.right;
            }
        }
        if (key > parent.val) {
            parent.right = node;
        } else {
            parent.left = node;
        }

    }

二、搜索指定节点

 public boolean search(int key) {
        Node cur = root;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }

        return false;
    }

三、删除节点方式一

 public void removenode1(Node parent, Node cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.right) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root.left = cur;
            } else if (cur == parent.right) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
        } else {
            Node tp = cur;
            Node t = cur.right;
            while (t.left != null) {
                tp = t;
                t = t.left;
            }
            if (tp.left == t) {
                cur.val = t.val;
                tp.left = t.right;
            }
            if (tp.right == t) {
                cur.val = t.val;
                tp.right = t.right;
            }
        }

    }

    public void remove(int key) {
        Node cur = root;
        Node parent = null;
        while (cur != null) {
            if (cur.val == key) {
                removenode1(parent, cur);
              //removenode2(parent, cur);
                return;
            } else if (key > cur.val) {
                parent = cur;
                cur = cur.right;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

四、删除节点方式二

 public void removenode2(Node parent, Node cur) {

        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.right) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root.left = cur;
            } else if (cur == parent.right) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
        } else {
            Node tp = cur;
            Node t = cur.left;
            while (t.right != null) {
                tp = t;
                t = t.right;
            }
            if (tp.right == t) {
                cur.val = t.val;
                tp.right = t.left;
            }
            if (tp.left == t) {
                cur.val = t.val;
                tp.left = t.left;
            }
        }

    }

五、运行结果

 /**
 * user:ypc;
 * date:2021-05-18;
 * time: 15:09;
 */
class TestBinarySearchTree {
    public static void main(String[] args) {
        int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for (int i = 0; i < a.length; i++) {
            binarySearchTree.insert(a[i]);
        }
        binarySearchTree.inOrderTree(binarySearchTree.root);
        System.out.println();
        binarySearchTree.preOrderTree(binarySearchTree.root);
        binarySearchTree.remove(7);
        System.out.println();
        System.out.println("方法一删除后");
        binarySearchTree.inOrderTree(binarySearchTree.root);
        System.out.println();
        binarySearchTree.preOrderTree(binarySearchTree.root);
    }
}

如何在Java中操作二叉搜索树
如何在Java中操作二叉搜索树

java基本数据类型有哪些

Java的基本数据类型分为:1、整数类型,用来表示整数的数据类型。2、浮点类型,用来表示小数的数据类型。3、字符类型,字符类型的关键字是“char”。4、布尔类型,是表示逻辑值的基本数据类型。

关于如何在Java中操作二叉搜索树问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Django框架模型操作之数据库如配置
  2. 如何在Python中操作列表

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:怎么在Java中实现哈希表

下一篇:如何在Spring中使用@Transactional注解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》