protected void resolveConflicts(HierNode<T> x) {
   boolean conflicted = false;
   Collection<HierNode<T>> nodes = new ArrayList<HierNode<T>>(getNodes());
   for (HierNode<T> y : nodes) {
     if (incomparable(x, y)) {
       //                System.out.println( " \t\tIncomparability between " + x + " and " + y );
       int sup = superset(y, x);
       if (sup == 0) {
         // System.out.println( " \t\tIncomparable, with same mask " + y );
         // can't use update mask here, or the already existing node would be removed
         x.setBitMask(increment(x.getBitMask(), freeBit(x)));
         propagate(y, freeBit(x, y));
       }
       if (sup > 0) {
         //                    System.out.println( " \t\tIncomparable, but as parent " + y );
         updateMask(x, increment(x.getBitMask(), freeBit(x)));
       }
       int sub = superset(x, y);
       if (sub > 0) {
         //                    System.out.println( " \t\tIncomparable, but as child " + y );
         modify(x, y);
         conflicted = true;
       }
     }
   }
   if (conflicted) {
     inheritMerged(x);
     resolveConflicts(x);
   }
 }
 protected void updateMask(HierNode<T> node, BitSet mask) {
   boolean in = node.getBitMask() != null && contains(node);
   if (in) {
     remove(node);
   }
   node.setBitMask(mask);
   if (in) {
     add(node);
   }
 }
  protected void encode(HierNode<T> node) {
    Collection<HierNode<T>> parents = node.getParents();
    // System.out.println( "Trying to encode " + node );
    switch (parents.size()) {
      case 0:
        BitSet zero = new BitSet();

        if (hasKey(zero)) {
          HierNode root = getNodeByKey(zero);

          if (root.getValue() != null) {
            fixedRoot = true;
            HierNode previousRoot = root;
            root = new HierNode((Object) null);
            root.addChild(previousRoot);
            previousRoot.addParent(root);

            root.setBitMask(zero);

            propagate(previousRoot, freeBit(root));
            add(root);
          }

          node.addParent(root);
          updateMask(node, increment(root.getBitMask(), freeBit(root)));

        } else {
          updateMask(node, new BitSet());
        }
        break;
      case 1:
        HierNode<T> parent = parents.iterator().next();
        updateMask(node, increment(parent.getBitMask(), freeBit(parent)));
        break;
      default:
        inheritMerged(node);
        // System.out.println( "
        // -----------------------------------------------------------------------------------------
        // " );
        resolveConflicts(node);
        break;
    }
  }