src/main/java/org/organicdesign/fp/collections/PersistentTreeMap.java
@SuppressWarnings("unchecked")
    private static <K, V> Node<K,V> append(Node<? extends K,? extends V> left,
                                   Node<? extends K,? extends V> right) {
        if (left == null)
            return (Node<K,V>) right;
        else if (right == null)
            return (Node<K,V>) left;
        else if (left instanceof PersistentTreeMap.Red) {
            if (right instanceof PersistentTreeMap.Red) {
                Node<K,V> app = append(left.right(), right.left());
                if (app instanceof PersistentTreeMap.Red)
                    return red(app.getKey(), app.getValue(),
                               red(left.getKey(), left.getValue(), left.left(), app.left()),
                               red(right.getKey(), right.getValue(), app.right(), right.right()));
                else
                    return red(left.getKey(), left.getValue(), left.left(),
                               red(right.getKey(), right.getValue(), app, right.right()));
            } else
                return red(left.getKey(), left.getValue(), left.left(), append(left.right(), right));
        } else if (right instanceof PersistentTreeMap.Red)
            return red(right.getKey(), right.getValue(), append(left, right.left()), right.right());
        else //black/black
        {
            Node<K,V> app = append(left.right(), right.left());
            if (app instanceof PersistentTreeMap.Red)
                return red(app.getKey(), app.getValue(),
                           black(left.getKey(), left.getValue(), left.left(), app.left()),
                           black(right.getKey(), right.getValue(), app.right(), right.right()));
            else
                return balanceLeftDel(left.getKey(), left.getValue(), left.left(),
                                      black(right.getKey(), right.getValue(), app, right.right()));
        }
    }
@SuppressWarnings("unchecked")
    private static <K, V> Node<K,V> append(Node<? extends K,? extends V> left,
                                   Node<? extends K,? extends V> right) {
        if (left == null)
            return (Node<K,V>) right;
        else if (right == null)
            return (Node<K,V>) left;
        else if (left instanceof PersistentTreeMap.Red) {
            if (right instanceof PersistentTreeMap.Red) {
                Node<K,V> app = append(left.right(), right.left());
                if (app instanceof PersistentTreeMap.Red)
                    return red(app.getKey(), app.getValue(),
                               red(left.getKey(), left.getValue(), left.left(), app.left()),
                               red(right.getKey(), right.getValue(), app.right(), right.right()));
                else
                    return red(left.getKey(), left.getValue(), left.left(),
                               red(right.getKey(), right.getValue(), app, right.right()));
            } else
                return red(left.getKey(), left.getValue(), left.left(), append(left.right(), right));
        } else if (right instanceof PersistentTreeMap.Red)
            return red(right.getKey(), right.getValue(), append(left, right.left()), right.right());
        else //black/black
        {
            Node<K,V> app = append(left.right(), right.left());
            if (app instanceof PersistentTreeMap.Red)
                return red(app.getKey(), app.getValue(),
                           black(left.getKey(), left.getValue(), left.left(), app.left()),
                           black(right.getKey(), right.getValue(), app.right(), right.right()));
            else
                return balanceLeftDel(left.getKey(), left.getValue(), left.left(),
                                      black(right.getKey(), right.getValue(), app, right.right()));
        }
    }
@ SuppressWarnings ( "unchecked" ) private static < K , V > Node < K , V > append ( Node < ? extends K , ? extends V > left , Node < ? extends K , ? extends V > right ) { if ( left == null ) return ( Node < K , V > ) right ; else if ( right == null ) return ( Node < K , V > ) left ; else if ( left instanceof PersistentTreeMap . Red ) { if ( right instanceof PersistentTreeMap . Red ) { Node < K , V > app = append ( left . right ( ) , right . left ( ) ) ; if ( app instanceof PersistentTreeMap . Red ) return red ( app . getKey ( ) , app . getValue ( ) , red ( left . getKey ( ) , left . getValue ( ) , left . left ( ) , app . left ( ) ) , red ( right . getKey ( ) , right . getValue ( ) , app . right ( ) , right . right ( ) ) ) ; else return red ( left . getKey ( ) , left . getValue ( ) , left . left ( ) , red ( right . getKey ( ) , right . getValue ( ) , app , right . right ( ) ) ) ; } else return red ( left . getKey ( ) , left . getValue ( ) , left . left ( ) , append ( left . right ( ) , right ) ) ; } else if ( right instanceof PersistentTreeMap . Red ) return red ( right . getKey ( ) , right . getValue ( ) , append ( left , right . left ( ) ) , right . right ( ) ) ; else //black/black { Node < K , V > app = append ( left . right ( ) , right . left ( ) ) ; if ( app instanceof PersistentTreeMap . Red ) return red ( app . getKey ( ) , app . getValue ( ) , black ( left . getKey ( ) , left . getValue ( ) , left . left ( ) , app . left ( ) ) , black ( right . getKey ( ) , right . getValue ( ) , app . right ( ) , right . right ( ) ) ) ; else return balanceLeftDel ( left . getKey ( ) , left . getValue ( ) , left . left ( ) , black ( right . getKey ( ) , right . getValue ( ) , app , right . right ( ) ) ) ; } }
Node<K,V> concat(Node<K1,V1> left, Node<K2,V2> right){
Node<K V > concat ( Node<K1 V1 > left Node<K2 V2 > right ) {