栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现二叉排序树(BST)-------参考视频 韩顺平《java数据结构和算法》

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现二叉排序树(BST)-------参考视频 韩顺平《java数据结构和算法》

代码如下:

package com.wqc.binarysorttree;


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 i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        System.out.println("=====中序遍历=====");
        binarySortTree.infixOrder();
//        System.out.println("==查找要删除的结点==");
//        System.out.println(binarySortTree.search(2));
//        System.out.println("==查找要删除的父结点==");
//        System.out.println(binarySortTree.searchParent(7));
        System.out.println("=====删除结点======");
        binarySortTree.delete(2);
        binarySortTree.delete(5);
        binarySortTree.delete(9);
        binarySortTree.delete(12);
        binarySortTree.delete(7);
        binarySortTree.delete(3);
        Node root = binarySortTree.getRoot();
        System.out.println("此时根节点=" + root + " 根结点的左子结点=" + root.left + " 根结点的右子结点=" + root.right);
        binarySortTree.delete(10);
        binarySortTree.delete(1);
        System.out.println("=====删除后的中序遍历=====");
        binarySortTree.infixOrder();
    }
}

class BinarySortTree {
    Node root;

    //返回二叉树的根节点


    public Node getRoot() {
        return root;
    }

    //add 添加结点
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    //查找要删除的结点
    public Node search(int value) {
        if (root != null) {
            return root.search(value);
        } else {
            System.out.println("二叉排序树为空");
            return null;
        }
    }

    //查找要删除结点的父结点
    public Node searchParent(int value) {
        if (root != null) {
//            if (root.value == value) {//根结点没有父节点
                System.out.println("根节点没有父结点");
                return null;
            } else {
            return root.searchParent(value);
//            }
        } else {
            System.out.println("二叉排序树为空");
            return null;
        }
    }

    
    public int searchRightAndDeleteMin(Node node) {
        Node minNode = node.right;
        while (minNode.left != null) {
            minNode = minNode.left;
        }
        int temp = minNode.value;
        delete(temp);
        return temp;
    }

    
    public int searchLeftAndDeleteMin(Node node) {
        Node minNode = node.left;
        while (minNode.right != null) {
            minNode = minNode.right;
        }
        int temp = minNode.value;
        delete(temp);
        return temp;
    }

    //删除结点
    public void delete(int value) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {//删除的是根节点 而且这个二叉排序树只有根节点的情况
            root = null;
            return;
        }
        //1 删除叶子结点的情况  删除非叶子结点的这两个步骤一致
        //1.1 查找要删除的结点
        Node node = search(value);
        if (node == null) {//如果要删除的结点不存在 直接返回
            return;
        }
        //1.2 查找要删除结点的父节点
        Node parentNode = searchParent(value);
        if (node.left == null && node.right == null) { //删除的是叶子结点的情况
            //1.3 判断是父结点的左还是右
            if (parentNode.left == node) {
                parentNode.left = null;
            }
            if (parentNode.right == node) {
                parentNode.right = null;
            }
        } else if (node.left != null && node.right != null) { //删除的结点有两个子节点
            //找到删除结点的右子树的最小值(或左子树的最大值)并用临时变量保存起来  并把这个结点删除 最后用最小值替换要删除结点的值
//            int minValue = searchRightAndDeleteMin(node);
            int minValue = searchRightAndDeleteMin(node);
            node.value = minValue;

            //下面这段代码是我自己写的 错误示范
//            Node minNode = node.right;
//            while (minNode.left != null) {
//                minNode = minNode.left;
//            }
//            //当退出while循环 temp结点就是要删除结点的右子树最小的结点
//            //先判断删除的是不是根节点的情况
//            if (node == root) {//删除的是根结点 特殊情况
//                int temp = minNode.value;//保存最小结点的值
//                Node minParent = searchParent(minNode.value);
//                //删除最小结点
//                minParent.left = null;
//                node.value = temp;//替换要删除结点的值为最小结点的值
//                return;
//            }
//            //然后判断要删除的结点是父节点的左还是右 2种情况
//            if (parentNode.left == node) {
//                if (minNode.left == null) {
//                    minNode.left = node.left;
//                }
//                if (minNode.right == null && node.right != minNode) {
//                    minNode.right = node.right;
//                }
//                parentNode.left = minNode;
//            }
//            if (parentNode.right == node) {
//                if (minNode.left == null) {
//                    minNode.left = node.left;
//                }
//                if (minNode.right == null && node.right != minNode) {
//                    minNode.right = node.right;
//                }
//                parentNode.right = minNode;
//            }
        } else { //删除的结点只有一个子节点
            //一共4种情况  两大种(一大种分两小种)
            if(parentNode == null){//如果此时父节点为空  说明二叉排序树只剩下两个结点 删除的刚好是根节点
                Node temp = null;
                if(node.left != null){
                    temp = node.left;
                    node.left = null;
                }
                if(node.right != null){
                    temp = node.right;
                    node.right = null;
                }
                root = temp;
                return;
            }
            if (parentNode.left == node) {//如果删除的结点是父结点的左子结点的话
                if (node.left == null) {//删除的结点有右结点
                    parentNode.left = node.right;
                }
                if (node.right == null) {//删除的结点有左结点
                    parentNode.left = node.left;
                }
            }
            if (parentNode.right == node) {//如果删除的结点是父结点的右子结点的话
                if (node.left == null) {
                    parentNode.right = node.right;
                }
                if (node.right == null) {
                    parentNode.right = node.left;
                }
            }
        }
    }

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

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

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

    //增加结点到排序二叉树
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value > this.value) {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);//向右子树递归添加
            }
        } else {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        }
    }

    
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else if (this.left != null && value < this.value) {
            return this.left.searchParent(value);//向左递归查找
        } else if (this.right != null && value >= this.value) {//向右递归查找
            // 等于是因为可能有重复的值 也可能是根结点 继续向下递归只能返回空
            return this.right.searchParent(value);
        } else {
            return null;
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号