code: private void encodeExpr(int idx, int bprev, StringBuilder sb) {
          avisit[idx] = numVisit++;
          sb.append(aexpr[idx]);
          final int d = deg[idx];
  
          int remain = d;
          for (int j = 0; j < d; j++) {
              int nbr = atomAdj[idx][j];
              int bidx = bondAdj[idx][j];
  
              // ring open/close
              if (rbnds[bidx] < 0) {
                  // open
                  final int rnum = chooseRingNumber();
                  if (rnum > 9) sb.append('%');
                  sb.append(rnum);
                  rbnds[bidx] = rnum;
              } else if (rbnds[bidx] > 0) {
                  // close
                  final int rnum = rbnds[bidx];
                  releaseRingNumber(rnum);
                  if (rnum > 9) sb.append('%');
                  sb.append(rnum);
              }
  
              if (mode == MODE_EXACT && avisit[nbr] == 0 ||
                  bidx == bprev ||
                  rbnds[bidx] != 0)
                  remain--;
          }
  
          for (int j = 0; j < d; j++) {
              int nbr = atomAdj[idx][j];
              int bidx = bondAdj[idx][j];
              if (mode == MODE_EXACT && avisit[nbr] == 0 ||
                  bidx == bprev ||
                  rbnds[bidx] != 0)
                  continue; // ignored
              remain--;
              if (avisit[nbr] == 0) {
                  // peripheral bond
                  if (remain > 0) sb.append('(');
                  sb.append(bexpr[bidx]);
                  sb.append(mol.getAtom(nbr).isAromatic() ? 'a' : '*');
                  if (remain > 0) sb.append(')');
              } else {
                  if (remain > 0) sb.append('(');
                  sb.append(bexpr[bidx]);
                  encodeExpr(nbr, bidx, sb);
                  if (remain > 0) sb.append(')');
              }
          }
      }
docs: Recursively encodes a SMARTS expression into the provides
  string builder.
  
  @param idx   atom index
  @param bprev previous bond
  @param sb    destition to write SMARTS to