1232: 普通平衡树

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1.插入一个数x;
2.删除一个数x(若有多个相同的数,只删除一个);
3.查询x数的排名(排名定义为比当前数小的数的个数+1);
4.查询排名为x的数;
5.求x的前趋(前趋定义为小于x,且最大的数);
6.求x的后继(后继定义为大于x,且最小的数)。

输入

第一行为n(1 <= n <= 100000), 表示操作的个数,下面n行每行有两个数op(1 <= op <= 6)和x(-1e7 <= x <= 1e7),op表示操作的序号。

输出

对于操作 3、4、5、6 每行输出一个数,表示对应答案。

样例输入 Copy
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
样例输出 Copy
106465
84185
492737
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        BST bst = new BST();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n-- > 0) {
            int op = scanner.nextInt();
            int x = scanner.nextInt();
            switch (op) {
                case 1:
                    bst.insert(x);
                    break;
 
                case 2:
                    bst.delete(x);
                    break;
                case 3:
                    System.out.println(bst.getRank(x));
                    break;
 
                case 4:
                    System.out.println(bst.getByRank(x));
                    break;
                case 5:
                    System.out.println(bst.getPredecessor(x));
                    break;
 
                case 6:
                    System.out.println(bst.getSuccessor(x));
                    break;
 
            }
        }
    }
 
    static class TreeNode {
        int val;
        int size;  // 用于维护子树的节点数量
        TreeNode left;
        TreeNode right;
 
        public TreeNode(int val) {
            this.val = val;
            this.size = 1;  // 初始节点数量为1
            this.left = null;
            this.right = null;
        }
    }
 
    static class BST {
        TreeNode root;
 
        // 插入一个数x
        public void insert(int val) {
            root = insertNode(root, val);
        }
 
        private TreeNode insertNode(TreeNode root, int val) {
            if (root == null) {
                return new TreeNode(val);
            }
            if (val < root.val) {
                root.left = insertNode(root.left, val);
            } else {
                root.right = insertNode(root.right, val);
            }
            root.size++;  // 更新子树节点数量
            return root;
        }
 
        // 删除一个数x
        public void delete(int val) {
            root = deleteNode(root, val);
        }
 
        private TreeNode deleteNode(TreeNode root, int val) {
            if (root == null) {
                return root;
            }
            if (val < root.val) {
                root.left = deleteNode(root.left, val);
            } else if (val > root.val) {
                root.right = deleteNode(root.right, val);
            } else {
                if (root.left == null) {
                    return root.right;
                } else if (root.right == null) {
                    return root.left;
                }
                root.val = minValue(root.right);
                root.right = deleteNode(root.right, root.val);
            }
            root.size--;  // 更新子树节点数量
            return root;
        }
 
        private int minValue(TreeNode root) {
            int minVal = root.val;
            while (root.left != null) {
                minVal = root.left.val;
                root = root.left;
            }
            return minVal;
        }
 
        // 查询x数的排名
        public int getRank(int val) {
            return getRankOfNode(root, val);
        }
 
        private int getRankOfNode(TreeNode root, int val) {
            if (root == null) {
                return 0;
            }
            if (val < root.val) {
                return getRankOfNode(root.left, val);
            } else if (val > root.val) {
                return 1 + size(root.left) + getRankOfNode(root.right, val);
            } else {
                return 1 + size(root.left);
            }
        }
 
        private int size(TreeNode node) {
            return node != null ? node.size : 0;
        }
 
        // 查询排名为x的数
        public int getByRank(int rank) {
            return getByRankOfNode(root, rank);
        }
 
        private int getByRankOfNode(TreeNode root, int rank) {
            int leftSize = size(root.left);
            if (rank == leftSize + 1) {
                return root.val;
            } else if (rank <= leftSize) {
                return getByRankOfNode(root.left, rank);
            } else {
                return getByRankOfNode(root.right, rank - leftSize - 1);
            }
        }
 
        // 求x的前趋
        public int getPredecessor(int val) {
            return getPredecessorOfNode(root, val);
        }
 
        private int getPredecessorOfNode(TreeNode root, int val) {
            TreeNode predecessor = null;
            while (root != null) {
                if (val > root.val) {
                    predecessor = root;
                    root = root.right;
                } else {
                    root = root.left;
                }
            }
            return predecessor != null ? predecessor.val : Integer.MIN_VALUE;
        }
 
        // 求x的后继
        public int getSuccessor(int val) {
            return getSuccessorOfNode(root, val);
        }
 
        private int getSuccessorOfNode(TreeNode root, int val) {
            TreeNode successor = null;
            while (root != null) {
                if (val < root.val) {
                    successor = root;
                    root = root.left;
                } else {
                    root = root.right;
                }
            }
            return successor != null ? successor.val : Integer.MAX_VALUE;
        }
    }
 
}

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值