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

数据结构和算法学习之二叉排序树

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

数据结构和算法学习之二叉排序树

package com.atguigu.binarysorttree;

import java.util.concurrent.locks.ReentrantReadWriteLock;


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();//1,2, 3,5,7,9,10,12
//        测试删除叶子节点
        binarySortTree.delNode(2);

        binarySortTree.delNode(12);
        //测试删除只有一颗子树的节点
        binarySortTree.delNode(7);
        binarySortTree.delNode(3);
        binarySortTree.delNode(10);
        binarySortTree.delNode(5);
        binarySortTree.delNode(9);
        binarySortTree.delNode(1);
        System.out.println("root=" + binarySortTree.getRoot());
        System.out.println("删除后:");
        binarySortTree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree {
    private Node root;

    public Node getRoot() {
        return root;
    }

    
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        //此时target指向最小节点
        delNode(target.value);
        return target.value;
    }

    //删除节点
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            //考虑单个节点的问题
            if (root.left == null && root.right == null && root.value == value) {
                root = null;
                return;
            }
            //找到要删除的节点
            Node targetNode = search(value);
            if (targetNode == null) {//没有找到
                return;
            }
            //找到要删除节点的父节点
            Node parentNode = searchParent(value);
            //此种写法要单独考虑父节点为空的情况
            if (parentNode == null) {
                //情况一:targetNode是叶子节点情况
                if (targetNode.left == null && targetNode.right == null) {
                    root = null;
                    return;
                }
                //情况二:targetNode有两颗子树的情况
                else if (targetNode.left != null && targetNode.right != null) {
                    int rightTreeMinValue = delRightTreeMin(targetNode.right);
                    targetNode.value = rightTreeMinValue;
                    return;
                }
                //情况三:targetNode有一颗子树情况
                else {
                    if (targetNode.left != null) {
                        root = targetNode.left;
                    }
                    if (targetNode.right != null) {
                        root = targetNode.right;
                    }
                    return;
                }


            }

            if (parentNode.left != null && parentNode.left.value == value) {//targetNode是父节点的左子节点
                //情况一:targetNode是叶子节点情况
                if (targetNode.left == null && targetNode.right == null) {
                    parentNode.left = null;
                }
                //情况二:targetNode有两颗子树的情况
                if (targetNode.left != null && targetNode.right != null) {
                    int rightTreeMinValue = delRightTreeMin(targetNode.right);
                    targetNode.value = rightTreeMinValue;
                }

                //情况三:targetNode有一颗子树情况
                if ((targetNode.left == null && targetNode.right != null) || (targetNode.right == null && targetNode.left != null)) {
                    if (targetNode.left != null) {//说明targetNode的子节点是左子节点
                        parentNode.left = targetNode.left;
                    }
                    if (targetNode.right != null) {//说明targetNode的子节点是右子节点
                        parentNode.left = targetNode.right;
                    }
                }

            } else if (parentNode.right != null && parentNode.right.value == value) {//targetNode是父节点的右子节点
                //情况一:targetNode是叶子节点情况
                if (targetNode.left == null && targetNode.right == null) {
                    parentNode.right = null;
                }
                //情况二:targetNode有两颗子树的情况
                if (targetNode.left != null && targetNode.right != null) {
                    int rightTreeMinValue = delRightTreeMin(targetNode.right);
                    targetNode.value = rightTreeMinValue;
                }

                //情况三:targetNode有一颗子树情况
                if ((targetNode.left == null && targetNode.right != null) || (targetNode.right == null && targetNode.left != null)) {
                    if (targetNode.left != null) {//说明targetNode的子节点是左子节点
                        parentNode.right = targetNode.left;
                    }
                    if (targetNode.right != null) {
                        parentNode.right = targetNode.right;
                    }
                }


            }


        }
    }

    //查找要删除的节点
    public Node search(int value) {
        if (root == null) {
            System.out.println("此树为空,查找失败");
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要删除节点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            System.out.println("此树为空,查找失败");
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //添加节点
    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();
        }


    }

}

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) {//判断左子树是否为空,如果为空则返回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 && value == this.left.value) || (this.right != null && value == this.right.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.right == null) {
                this.right = node;
            } else {
                //如果不是空则继续向右递归添加
                this.right.add(node);
            }
        } else {//添加节点比当前节点小,挂到左侧
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }

        }
    }

    //中序遍历
    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/1006723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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