a-foundation/src/main/java/com/ajjpj/afoundation/collection/immutable/ARedBlackTreeMap.java
private static <K,V> Tree<K,V> append (Tree<K,V> tl, Tree<K,V> tr) {
        if (tl == null) return tr;
        if (tr == null) return tl;

        if (isRedTree (tl) && isRedTree (tr)) {
            final Tree<K,V> bc = append (tl.right, tr.left);
            return isRedTree (bc) ?
                    new RedTree<> (bc.key, bc.value, new RedTree<> (tl.key, tl.value, tl.left, bc.left), new RedTree<> (tr.key, tr.value, bc.right, tr.right)) :
                    new RedTree<> (tl.key, tl.value, tl.left, new RedTree<> (tr.key, tr.value, bc, tr.right));
        }
        if (isBlackTree (tl) && isBlackTree (tr)) {
            final Tree<K,V> bc = append (tl.right, tr.left);
            return isRedTree (bc) ?
                    new RedTree<> (bc.key, bc.value, new BlackTree<> (tl.key, tl.value, tl.left, bc.left), new BlackTree<> (tr.key, tr.value, bc.right, tr.right)) :
                    balanceLeft (tl.key, tl.value, tl.left, new BlackTree<> (tr.key, tr.value, bc, tr.right));
        }
        if (isRedTree (tr)) {
            return new RedTree<> (tr.key, tr.value, append (tl, tr.left), tr.right);
        }
        if (isRedTree (tl)) {
            return new RedTree<> (tl.key, tl.value, tl.left, append (tl.right, tr));
        }
        throw new IllegalStateException ("invariant violation: unmatched tree on append: " + tl + ", " + tr);
    }
private static <K,V> Tree<K,V> append (Tree<K,V> tl, Tree<K,V> tr) {
        if (tl == null) return tr;
        if (tr == null) return tl;

        if (isRedTree (tl) && isRedTree (tr)) {
            final Tree<K,V> bc = append (tl.right, tr.left);
            return isRedTree (bc) ?
                    new RedTree<> (bc.key, bc.value, new RedTree<> (tl.key, tl.value, tl.left, bc.left), new RedTree<> (tr.key, tr.value, bc.right, tr.right)) :
                    new RedTree<> (tl.key, tl.value, tl.left, new RedTree<> (tr.key, tr.value, bc, tr.right));
        }
        if (isBlackTree (tl) && isBlackTree (tr)) {
            final Tree<K,V> bc = append (tl.right, tr.left);
            return isRedTree (bc) ?
                    new RedTree<> (bc.key, bc.value, new BlackTree<> (tl.key, tl.value, tl.left, bc.left), new BlackTree<> (tr.key, tr.value, bc.right, tr.right)) :
                    balanceLeft (tl.key, tl.value, tl.left, new BlackTree<> (tr.key, tr.value, bc, tr.right));
        }
        if (isRedTree (tr)) {
            return new RedTree<> (tr.key, tr.value, append (tl, tr.left), tr.right);
        }
        if (isRedTree (tl)) {
            return new RedTree<> (tl.key, tl.value, tl.left, append (tl.right, tr));
        }
        throw new IllegalStateException ("invariant violation: unmatched tree on append: " + tl + ", " + tr);
    }
private static < K , V > Tree < K , V > append ( Tree < K , V > tl , Tree < K , V > tr ) { if ( tl == null ) return tr ; if ( tr == null ) return tl ; if ( isRedTree ( tl ) && isRedTree ( tr ) ) { final Tree < K , V > bc = append ( tl . right , tr . left ) ; return isRedTree ( bc ) ? new RedTree <> ( bc . key , bc . value , new RedTree <> ( tl . key , tl . value , tl . left , bc . left ) , new RedTree <> ( tr . key , tr . value , bc . right , tr . right ) ) : new RedTree <> ( tl . key , tl . value , tl . left , new RedTree <> ( tr . key , tr . value , bc , tr . right ) ) ; } if ( isBlackTree ( tl ) && isBlackTree ( tr ) ) { final Tree < K , V > bc = append ( tl . right , tr . left ) ; return isRedTree ( bc ) ? new RedTree <> ( bc . key , bc . value , new BlackTree <> ( tl . key , tl . value , tl . left , bc . left ) , new BlackTree <> ( tr . key , tr . value , bc . right , tr . right ) ) : balanceLeft ( tl . key , tl . value , tl . left , new BlackTree <> ( tr . key , tr . value , bc , tr . right ) ) ; } if ( isRedTree ( tr ) ) { return new RedTree <> ( tr . key , tr . value , append ( tl , tr . left ) , tr . right ) ; } if ( isRedTree ( tl ) ) { return new RedTree <> ( tl . key , tl . value , tl . left , append ( tl . right , tr ) ) ; } throw new IllegalStateException ( "invariant violation: unmatched tree on append: " + tl + ", " + tr ) ; }
This method combines two separate sub-trees into a single (balanced) tree. It assumes that both subtrees are
balanced and that all elements in 'tl' are smaller than all elements in 'tr'. This situation occurs when a
node is deleted and its child nodes must be combined into a resulting tree.
This method combines two separate sub - trees into a single ( balanced ) tree . It assumes that both subtrees are balanced and that all elements in tl are smaller than all elements in tr . This situation occurs when a node is deleted and its child nodes must be combined into a resulting tree .