// Recursively construct a perfectly balanced BinarySearchTreeWithRank
    // by repeated insertions in O( N log N ) time.
    // t should be empty on the initial call.
    public static void BuildTree(BinarySearchTreeWithRank <int> t,
                                 int low, int high)
    {
        int center = (low + high) / 2;

        if (low <= high)
        {
            t.Insert(center);

            BuildTree(t, low, center - 1);
            BuildTree(t, center + 1, high);
        }
    }
    // Return the winner in the Josephus problem.
    // Search Tree implementation.
    public static int Jos2(int people, int passes)
    {
        BinarySearchTreeWithRank <int> t = new BinarySearchTreeWithRank <int>( );

        BuildTree(t, 1, people);

        int rank = 1;

        while (people > 1)
        {
            rank = (rank + passes) % people;
            if (rank == 0)
            {
                rank = people;
            }

            t.Remove(t.FindKth(rank));
            people--;
        }

        return(t.FindKth(1));
    }