src/main/java/Smaller.java
/**
     * Insert the next "root" of the sorted tree.
     * As a side effect populates the array of counts of smaller ints.
     *
     * The unsorted array is parsed from right to left.
     *
     * @param root current root of the tree
     * @param num int value at this index of the unsorted array
     * @param result n array of counts of smaller ints, with elements to right of index already populated
     * @param sum initially 0, but contains the count so far as this is recursively called
     * @param index of the element we are processing in the unsorted array
     * @return a new root for the rest of the tree
     */
Insert the next "root" of the sorted tree.
As a side effect populates the array of counts of smaller ints.

The unsorted array is parsed from right to left.


Insert the next " root " of the sorted tree . As a side effect populates the array of counts of smaller ints . The unsorted array is parsed from right to left .
private static Smaller insert(Smaller root, int num, int[] result, int sum, int index) {
        if (root == null) { 
            result[index] = sum;
            return new Smaller(num);
        }
        if (root.val == num) { 
            root.dup++;
            result[index] = sum + root.count; 
        } else if (root.val > num) { 
            root.count++;
            root.left = insert(root.left, num, result, sum, index); 
        } else {
            root.right = insert(root.right, num, result, sum + root.count + root.dup, index);
        }
        return root;
    }
private static Smaller insert ( Smaller root , int num , int [ ] result , int sum , int index ) { if ( root == null ) { result [ index ] = sum ; return new Smaller ( num ) ; } if ( root . val == num ) { root . dup ++ ; result [ index ] = sum + root . count ; } else if ( root . val > num ) { root . count ++ ; root . left = insert ( root . left , num , result , sum , index ) ; } else { root . right = insert ( root . right , num , result , sum + root . count + root . dup , index ) ; } return root ; }
Insert the next "root" of the sorted tree.
Insert the next " root " of the sorted tree .
// no root below here so make a "zero" count leaf for this number // check for duplicates // result count is sum so far and the count of this subtree // new one is bigger than here in the tree // so fill in the smaller tree // fill in the bigger tree