private OptionScanNode buildTreeFromNodeList(List<OptionScanNode> nodeList,int treeNodeType,OptionScanNode lastScanNode){
  if (nodeList.size() == mDegree) {
    nodeList.add(lastScanNode);
    List<OptionScanNode> children=nodeList.subList(0,nodeList.size() - 2);
    OptionScanNode lastChild=createTree(nodeList.subList(nodeList.size() - 2,nodeList.size()),treeNodeType);
    children.add(lastChild);
    return createTree(children,treeNodeType);
  }
 else   if (nodeList.size() < mDegree) {
    nodeList.add(lastScanNode);
    return createTree(nodeList,treeNodeType);
  }
 else {
    List<OptionScanNode> subtrees=new ArrayList<>();
    int elemNum=nodeList.size() / mDegree;
    int elemRemainder=nodeList.size() % mDegree;
    int startIndex=0, endIndex=0;
    List<OptionScanNode> subtreeNodes;
    while (startIndex < nodeList.size()) {
      endIndex=(elemRemainder > 0) ? endIndex + elemNum + 1 : endIndex + elemNum;
      elemRemainder--;
      subtreeNodes=new ArrayList<>(nodeList.subList(startIndex,endIndex));
      if ((subtreeNodes.size() == 1) && (endIndex < nodeList.size())) {
        subtrees.add(subtreeNodes.get(0));
      }
 else {
        subtrees.add(buildTreeFromNodeList(subtreeNodes,treeNodeType,lastScanNode));
      }
      startIndex=endIndex;
    }
    return createTree(subtrees,treeNodeType);
  }
}

private OptionScanNode buildTreeFromNodeList ( List < OptionScanNode > nodeList , int treeNodeType , OptionScanNode lastScanNode ) { if ( nodeList . size ( ) == mDegree ) { nodeList . add ( lastScanNode ) ; List < OptionScanNode > children = nodeList . subList ( 0 , nodeList . size ( ) - 2 ) ; OptionScanNode lastChild = createTree ( nodeList . subList ( nodeList . size ( ) - 2 , nodeList . size ( ) ) , treeNodeType ) ; children . add ( lastChild ) ; return createTree ( children , treeNodeType ) ; } else if ( nodeList . size ( ) < mDegree ) { nodeList . add ( lastScanNode ) ; return createTree ( nodeList , treeNodeType ) ; } else { List < OptionScanNode > subtrees = new ArrayList < > ( ) ; int elemNum = nodeList . size ( ) / mDegree ; int elemRemainder = nodeList . size ( ) % mDegree ; int startIndex = 0 , endIndex = 0 ; List < OptionScanNode > subtreeNodes ; while ( startIndex < nodeList . size ( ) ) { endIndex = ( elemRemainder > 0 ) ? endIndex + elemNum + 1 : endIndex + elemNum ; elemRemainder -- ; subtreeNodes = new ArrayList < > ( nodeList . subList ( startIndex , endIndex ) ) ; if ( ( subtreeNodes . size ( ) == 1 ) && ( endIndex < nodeList . size ( ) ) ) { subtrees . add ( subtreeNodes . get ( 0 ) ) ; } else { subtrees . add ( buildTreeFromNodeList ( subtreeNodes , treeNodeType , lastScanNode ) ) ; } startIndex = endIndex ; } return createTree ( subtrees , treeNodeType ) ; } }
builds an n - ary tree from a list of nodes to be included in the tree .
