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

二叉排序树的新增删除

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

二叉排序树的新增删除

二叉排序树

给你一个数列 (7, 3, 10, 12, 5, 1, 9, 2, 8, 11, 13),要求能够高效的完成对数据的查询和添加。

使用数组
数组未排序, 优点:直接在数组尾添加,速度快。 缺点:查找速度慢.
数组排序,优点:可以使用二分查找,查找速度快,缺点:为了保证数组有序,在添加新数据时,找到插入位置后,后面的数据需整体移动,速度慢。[示意图]

使用链式存储-链表不管链表是否有序,查找速度都慢,添加数据速度比数组快,不需要数据整体移动。

使用二叉排序树

二叉排序树介绍

二叉排序树:BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。
特别说明:如果有相同的值,可以将该节点放在左子节点或右子节点

public class BinarySortTree {
    public BinarySortNode root;

    public static void main(String[] args) {
        int arr[] = {7, 3, 10, 12, 5, 1, 9, 2, 8, 11, 13};
        BinarySortTree tree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            tree.addNode(new BinarySortNode(arr[i]));
        }
        System.out.println("中序遍历:");
        tree.infixOrder();
        tree.delNode(10);
        System.out.println("中序遍历:");
        tree.infixOrder();
        tree.delNode(7);
        System.out.println("中序遍历:");
        tree.infixOrder();
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("当前树为空!");
        } else {
            root.infixOrder();
        }
        System.out.println();
    }

    public void addNode(BinarySortNode node) {
        if (root == null) {
            root = node;
        } else {
            root.addNode(node);
        }
    }

    public BinarySortNode search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public BinarySortNode searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            // 寻找待删除节点,没找到直接返回
            BinarySortNode targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            // 删除的是根节点,且根节点没有子树
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            // 寻找待删除节点的父节点
            BinarySortNode parent = searchParent(value);
            // 删除叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                // 待删除节点是父节点的左子树,直接把父节点左子树置为null
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                    // 待删除节点是父节点的右子树,直接把父节点右子树置为null
                } else if (parent.right == null && parent.right.value == value) {
                    parent.right = null;
                }
                // 删除有两个叶子节点的节点
            } else if (targetNode.left != null && targetNode.right != null) {
                int i = delRightTreeMin(targetNode.right);
                targetNode.value = i;
                // 删除只有一个叶子节点的节点
            } else {
                // 只有左子树
                if (targetNode.left != null) {
                    // 父节点非根节点
                    if (parent != null) {
                        // 待删除节点是父节点的左子树,用待删除的节点的左子树代替待删除节点作为父节点的左子树
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            // 待删除节点是父节点的右子树,用孙子节点(待删除节点的子节点)代替父节点(待删除节点)作为爷爷节点(待删除节点的父节点)的右子树
                            parent.right = targetNode.left;
                        }
                        // 父节点是根节点
                    } else {
                        root = targetNode.left;
                    }
                    // 只有右子树,原理同只有左子树
                } else {
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }


    
    public int delRightTreeMin(BinarySortNode node) {
        BinarySortNode target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }
}

class BinarySortNode {


    public int value;
    public BinarySortNode left;
    public BinarySortNode right;

    
    public BinarySortNode searchParent(int value) {
        // 左(右)子树不为空且左(右)子树的值等于value
        if ((this.left != null && this.left.value == value) || this.right != null && this.right.value == value) {
            return this;
        } else {
            // 左子树不为空,且当前值比目标值大,说明应该往左子树寻找
            if (this.left != null && this.value > value) {
                return this.left.searchParent(value);
                // 右子树不为空,且当前值小于等于目标值,说明应该往右子树寻找
            } else if (this.right != null && this.value <= value) {
                return this.right.searchParent(value);
            } else {
                // 没找到
                return null;
            }
        }
    }

    
    public BinarySortNode search(int value) {
        if (this.value == value) {
            return this;
            // 当前节点值大于目标值,说明应该往左子树寻找
        } else if (this.value > value) {
            // 左子树为null,直接返回
            if (this.left == null) {
                return null;
            } else {
                // 从左子树递归寻找
                return this.left.search(value);
            }
            // 当前节点值小于目标值,说明应该往右子树寻找
        } else {
            // 右子树为null,直接返回
            if (this.right == null) {
                return null;
            } else {
                // 从右子树递归寻找
                return this.right.search(value);
            }
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.print(this.value + " ");
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void addNode(BinarySortNode node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.addNode(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.addNode(node);
            }
        }
    }

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

    public BinarySortNode() {
    }

    @Override
    public String toString() {
        return "BinarySortNode{" +
                "value=" + value +
                '}';
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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