According to wikipedia link, order statistic tree mainly offers 2 following functions:
Select(i) — find the i’th smallest element stored in the tree
Rank(x) – find the rank of element x in the tree, i.e. its index in the sorted list of elements of the tree
In order to find the rank/index, it is important to find the left subtree rank, and right subtree rank. After researching on character of order statistic tree, I conducted the following formula.
Let t_rank be rank of current tree, lr_num be number of left right subtree, r_num be number of right subtree, rr_num be right right subtree.
rank of left sub tree = t_rank – 1 – lr_num
rank of right sub tree = t_rank + r_num – rr_num
This following code is a try for order statistic binary tree. I didn’t write the construction for it, only wrote select and rank function.
- import util.Tree;
- public class OrderedStatisticTree {
- public static void main(String[] args) {
- Tree tree = Tree.getOrderStatisticBinaryTree();
- int rank = rank(tree, 17);
- System.out.println(rank);
- Tree index_tree = index(tree, 10);
- System.out.println(index_tree);
- }
- /*
- * Return the rank of value in tree
- */
- public static int rank(Tree tree, int value) {
- if (tree == null) {
- return -1;
- }
- int rank = tree.num;
- while(tree!=null){
- rank = rank – (tree.right == null ? 0 : tree.right.num);
- if (value == tree.value) {
- return rank;
- }
- if (value < tree.value) {
- rank = rank – 1;
- tree = tree.left;
- } else {
- rank = (tree.right == null) ? -1 : rank + tree.right.num;
- tree = tree.right;
- }
- }
- return -1;
- }
- /*
- * Return the tree, which ranks index.
- */
- public static Tree index(Tree tree, int index) {
- if (tree == null) {
- return null;
- }
- int curr_index = tree.num;
- while(tree!=null){
- curr_index = curr_index – (tree.right == null ? 0 : tree.right.num);
- if (curr_index == index) {
- return tree;
- }
- if (index < curr_index) {
- curr_index = curr_index – 1;
- tree = tree.left;
- } else {
- curr_index = (tree.right == null) ? 0 : curr_index + tree.right.num;
- tree = tree.right;
- }
- }
- return null;
- }
- }
Following auxiliary Tree class helps to create a order statistic tree.
- package util;
- public class Tree{
- public Tree(int value){
- this.value = value;
- }
- public Tree(String value){
- this.valueString = value;
- }
- public Tree(int value, int num){
- this.value = value;
- this.num = num;
- }
- public Tree left;
- public Tree right;
- public int value;
- public String valueString;
- /*
- * This is used for ordered statistics binary tree
- * To store the number under tree.
- */
- public int num; //
- public static Tree getOrderStatisticBinaryTree(){
- Tree t15 = new Tree(15, 15);
- Tree t10 = new Tree(10, 6);
- Tree t20 = new Tree(20, 8);
- Tree t8 = new Tree(8, 2);
- Tree t13 = new Tree(13, 3);
- Tree t17 = new Tree(17, 3);
- Tree t23 = new Tree(23, 4);
- Tree t7 = new Tree(7, 1);
- Tree t11 = new Tree(11, 1);
- Tree t14 = new Tree(14, 1);
- Tree t16 = new Tree(16, 1);
- Tree t18 = new Tree(18, 1);
- Tree t22 = new Tree(22, 2);
- Tree t24 = new Tree(24, 1);
- Tree t21 = new Tree(21, 1);
- t15.left = t10; t15.right = t20;
- t10.left = t8; t10.right = t13;
- t20.left = t17; t20.right = t23;
- t8.left = t7;
- t13.left = t11; t13.right = t14;
- t17.left = t16; t17.right = t18;
- t23.left = t22; t23.right = t24;
- t22.left = t21;
- return t15;
- }
- @Override
- public String toString(){
- return String.valueOf(value);
- }
- }