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

二叉搜索树(BST)实现

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

二叉搜索树(BST)实现

二叉搜索树(BST)实现

简单的二叉树实现,涉及添加节点,删除结点,删除要考虑重组树

定义Node结点
@Data
public class Node {
    private T no;
    private Node left;
    private Node right;

    public Node(T no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }
}
定义树以及一些方法
public class BinarySearchTree {

    private static List list = new ArrayList<>();

    //添加结点,递归
    private static void add(Node root, Node node) {
        if (root != null) {
            Boolean flag = compare(root, node);
            if (flag) {
                if (root.getLeft() != null) {
                    add(root.getLeft(), node);
                } else {
                    root.setLeft(node);
                }
            } else {
                if (root.getRight() != null) {
                    add(root.getRight(), node);
                } else {
                    root.setRight(node);
                }
            }
        }
    }

    //数组生成树
    public static Node addByArray(int[] nums) {
    	//数组的第一个元素作为root结点
        Node root = new Node<>(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            Node node = new Node<>(nums[i]);
            add(root, node);
        }
        return root;
    }

    //重组子结点树
    public static void addByList(Node root) {
        for (Integer i : list) {
            Node node = new Node<>(i);
            add(root, node);
        }
    }

    //比较大小,确定新增元素在左/右子树上
    private static Boolean compare(Node root, Node node) {
        if (node.getNo() <= root.getNo()) {
            return true;
        } else
            return false;
    }
	
	//删除方法入口
    public static Node del(Node root, int value) {
        del(root, value, root);
        return root;
    }

    //因为BST树是单向的,所有需要有一个记录当前结点的前驱结点指针point,方便进行递归和将子树置为null
    private static void del(Node root, int value, Node point) {
        if (root.getNo() == value) {
            if (root.getLeft() != null || root.getRight() != null) {
                findNums(root, value);
                compareAndSet(point,value);
                addByList(point);
                //list记得清空,单机环境可不用清空
                list.clear();
            } else {
                compareAndSet(point,value);
            }
        } else {
            if (value < root.getNo()) {
                del(root.getLeft(), value, root);
            } else {
                del(root.getRight(), value, root);
            }
        }
    }

    //比较大小,确定左/右子树为null
    private static void compareAndSet(Node point, Integer value) {
        if (value < point.getNo()) {
            point.setLeft(null);
        } else {
            point.setRight(null);
        }
    }

    //先序遍历当前结点,找到当前结点所有的子结点,然后删除list中第一位元素
    //第一位元素就是要删除的结点,list中剩下的元素要结合point指针重新生成树
    private static void findNums(Node node, int num) {
        if (node != null) {
            list.add(node.getNo());
            if (node.getRight() != null) {
                findNums(node.getRight(), num);
            }
            if (node.getLeft() != null) {
                findNums(node.getLeft(), num);
            }
        }
        list = list.stream().filter(x -> x != num).collect(Collectors.toList());
    }


测试类
public class Client {

    public static void main(String[] args) {
        int[] nums = {7, 2, 8, 1, 4, 3, 5, 9, 10};
        Node node = BinarySearchTree.addByArray(nums);
        //中序遍历一定是升序排列
        BinaryTree.midIterator(node);
        System.out.println("---------------");
        //删除结点
        Node del = BinarySearchTree.del(node, 2);
        BinaryTree.midIterator(del);
    }
}

逃课技巧

其实删除一个结点再重组树没必要写的这么麻烦,既然是数组生成树,完全可以在Node结点中声明一个数组变量,在数组生成树的过程中,将原数组存入根(root)结点中,遍历原数组再删除指定元素,然后在用新数组重新生成树即可

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

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

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