public InternalNode(int d, Node p0, int k1, Node p1, Node n, Node p) {

    super(d, n, p);
    ptrs[0] = p0;
    keys[1] = k1;
    ptrs[1] = p1;
    lastindex = 1;

    if (p0 != null) p0.setParent(new Reference(this, 0, false));
    if (p1 != null) p1.setParent(new Reference(this, 1, false));
  }
  void left(Node node) {
    String str = node.getState();
    String parent = node.getParent();
    Node newState = new Node();
    int a = str.indexOf("0");
    char temp;
    char[] s = str.toCharArray();
    String newStr;

    if (a != 0) {
      temp = s[a - 1];
      s[a - 1] = '0';
      s[a] = temp;
      newStr = this.gString(s);
      newState.setState(newStr);
      newState.setParent(str);
      // newState.setLevel(node.getLevel()+1);
      // newState.setHeuristic(newState.calculateHeuristic(goal));
      // newState.setFvalue(newState.getHeuristic() + newState.getLevel());
      add(newState, map.get(str) + 1);
      // add(s,map.get(str)+1);
      if (newStr.equals("www0bbb")) {
        System.out.println(
            "Solution found after searching through " + map.get(newStr) + " states of the tree");
        printSolution();
        System.exit(0);
      }
    }
  }
  public static void main(String args[]) {

    String str =
        "bbb0www"; // "087465132";		054832761			// Initial Board State as a String with 0 as the
                   // Blank Space

    Node top = new Node();
    Node node = new Node();
    System.out.println("Start state: " + str);
    /*for(int i=0;i<str.length();i++){
    	if(i%3==0){
    		System.out.println("");
    	}
    	System.out.print(str.charAt(i)+"\t");
    }*/
    SlidingTileDFS e = new SlidingTileDFS();
    node.setState(str);
    node.setParent(null);

    e.add(node, 0);

    // e.s.push(str);
    while (!(e.s.empty())) {
      top = e.s.pop();
      // node.setState(top);
      // node.setParent(null);
      e.left(top); // Move the blank space up and add new state to queue
      e.leftjump1(top);
      e.leftjump2(top);
      e.right(top); // Move the blank space down
      e.rightjump1(top); // Move left
      e.rightjump2(top); // Move right and remove the current node from Queue
    }
    System.out.println("Solution doesn't exist");
  }
  public void insert(int val, Node ptr) {
    // find correct position to insert
    int p = this.findKeyIndex(val);

    // if node is not full
    if (!full()) {
      // if val is larger than last key, increment p
      if (val > this.getKey(this.getLast())) {
        p++;
      }
      // insertion for simple case
      this.insertSimple(val, ptr, p);
    } else {
      // create a neighbor node with val and ptr
      Node split = new InternalNode(degree, null, val, ptr, this.getNext(), this);
      // set split to be next node of current node
      this.setNext(split);
      // get val to be inserted into parent
      int parentVal = this.redistribute();

      // if current node has a parent, insert value into parent
      if (this.getParent() != null) {
        this.getParent().getNode().insert(parentVal, split);
        // if no parent for current node, create a new root
      } else {
        Node newRoot = new InternalNode(degree, this, parentVal, split, null, null);
        // set parent references
        this.setParent(new Reference(newRoot, 0, false));
        split.setParent(new Reference(newRoot, 1, false));
      }
    }
  }
  /**
   * parse sentence and generate .trees file
   *
   * @param en
   * @param align
   * @param out
   */
  public static void parse(String en, String align, String out, boolean verbose) {

    // use alignments?
    boolean use_alignments = true;
    if (align.startsWith("no_align")) {
      use_alignments = false;
      System.err.println("Not using alignments.");
    } else {
      System.err.println("Using alignments from " + align);
    }

    // setup stanfordparser
    String grammar = "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz";
    String[] options = {"-outputFormat", "wordsAndTags, typedDependencies"};
    LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
    TreebankLanguagePack tlp = lp.getOp().langpack();
    java.util.function.Predicate<java.lang.String> punctuationFilter = x -> true;

    GrammaticalStructureFactory gsf =
        new edu.stanford.nlp.trees.EnglishGrammaticalStructureFactory(punctuationFilter);

    // read document
    Iterable<List<? extends HasWord>> sentences;
    Reader r = new Reader(en);
    String line = null;
    List<List<? extends HasWord>> tmp = new ArrayList<List<? extends HasWord>>();
    while ((line = r.getNext()) != null) {
      Tokenizer<? extends HasWord> token =
          tlp.getTokenizerFactory().getTokenizer(new StringReader(line));
      List<? extends HasWord> sentence = token.tokenize();
      tmp.add(sentence);
    }
    sentences = tmp;

    // set up alignment file reader
    Reader alignment = new Reader();
    if (use_alignments) {
      alignment = new Reader(align);
    }

    // set up tree file writer
    Writer treeWriter = new Writer(out);

    // parse
    long start = System.currentTimeMillis();
    // System.err.print("Parsing sentences ");
    int sentID = 0;
    for (List<? extends HasWord> sentence : sentences) {
      Tree t = new Tree();
      // t.setSentID(++sentID);
      System.err.println("parse Sentence :" + sentence + "...");
      // System.err.print(".");
      System.err.println("-----------------------------------------------------------------------");
      edu.stanford.nlp.trees.Tree parse = lp.parse(sentence);
      // parse.pennPrint();

      // List for root node and lexical nodes
      List<Node> loneNodes = new LinkedList<Node>();
      List<Node> governingNodes = new LinkedList<Node>();

      // ROOT node
      Node root = new Node(true, true);
      root.setTag("ROOT");
      t.setRoot(root);
      loneNodes.add(root);
      governingNodes.add(root);

      // tagging

      int counter = 0;
      String surface = "";
      String tag = "";

      for (TaggedWord tw : parse.taggedYield()) {
        Node n = new Node();
        Node governingNode = new Node();
        n.setNodeID(++counter);
        surface = tw.value();
        tag = tw.tag();
        if (surface.startsWith("-LRB-")) {
          surface = "(";
        } else if (surface.startsWith("-RRB-")) {
          surface = ")";
          // } else if (surface.startsWith("-LSB-")){
          //    surface = "[";
          // } else if (surface.startsWith("-RSB-")){
          //    surface = "]";
          // } else if (surface.startsWith("-LCB-")){
          //    surface = "{";
          // } else if (surface.startsWith("-RCB-")){
          //    surface = "}";
        } else if (surface.startsWith("''")) {
          surface = "\"";
        }
        tag = tag.replaceAll("#", "-NUM-");
        surface = surface.replaceAll("&", "-AMP-");
        surface = surface.replaceAll("#", "-NUM-");
        surface = surface.replaceAll(">", "-GRE-");
        surface = surface.replaceAll("=", "-EQU-");
        n.setInitialLexicalIndex(counter);
        governingNode.setInitialLexicalIndex(counter);
        n.setSurface(surface);
        // System.out.print("("+tw.value()+" : ");
        n.setTag(tag);
        governingNode.setTag("_" + tag);
        governingNode.setLabel("_gov");
        // System.out.print(tw.tag()+")");
        loneNodes.add(n);
        governingNodes.add(governingNode);
        governingNode.setChild(n);
      }

      // System.out.println("");

      // t.setSentLength(t.getNodes().size() - 1);
      // List<Node> loneNodes = new LinkedList<Node>();
      Node[] nodes = new Node[2000];
      // labeling
      int depIndex;
      int govIndex;
      String[] depInfo;
      String[] govInfo;
      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      List<TypedDependency> tdl = gs.typedDependencies(false);
      // List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
      for (TypedDependency td : tdl) {
        depIndex = td.dep().index();
        govIndex = td.gov().index();
        // System.out.println("Index1:"+depIndex);
        // System.out.println("Index2:"+govIndex);
        // if (nodes[depIndex] == null){
        //	System.out.println("Making node!");
        //	nodes[depIndex] = new Node();
        // }
        // if (nodes[govIndex] == null){
        //	System.out.println("Making node!");
        //	nodes[govIndex] = new Node();
        // }
        Node dep = loneNodes.get((depIndex));
        Node gov = governingNodes.get((govIndex));
        Node depcopy = governingNodes.get((depIndex));
        Node govcopy = loneNodes.get((govIndex));
        dep.setLabel(td.reln().toString());
        depcopy.setLabel(td.reln().toString());
        govcopy.setLabel("head");
        // System.out.println(td.toString());
        govInfo = td.gov().toString().split("/");
        depInfo = td.dep().toString().split("/");
        // System.out.println(td.gov().toString());
        // System.out.println(td.dep().toString());
        // dep.setSurface(depInfo[0]);
        // dep.setTag(depInfo[1]);
        gov.setChild(governingNodes.get(depIndex));
        governingNodes.get(depIndex).setParent(gov);
        // gov.setChild(dep);
        dep.setParent(governingNodes.get(depIndex));
      }
      // t.setRoot(nodes[0]);

      // Collapse tree to remove unneeded governing nodes:

      Node gov;
      Node dep;
      Node parent;
      List<Node> children;

      for (int i = 1; i < governingNodes.size(); i++) { // start with index 1 to skip root
        gov = governingNodes.get(i);
        dep = loneNodes.get(i);
        if (gov.getChildren().size() <= 1) {
          int k = 0;
          parent = gov.getParent();
          children = parent.getChildren();

          for (Node n : children) {
            if (n == gov) {
              gov.getParent().replaceChild(k, dep);
              dep.setParent(gov.getParent());
            }
            k++;
          }
        }
      }
      // Mark head nodes with appropriate label:
      int k = 0;
      for (Node n : loneNodes) {
        if (k != 0) {
          if (n.getLabel() == n.getParent().getLabel()) {
            n.setLabel("head");
          }
        } else {
          n.setLabel("null");
        }
        k++;
      }
      // Sort lexical children of each governing node in lexical order

      for (Node n : governingNodes) {
        n.sortChildrenByInitialIndex();
      }

      // combine with alignment
      if (use_alignments) {
        t.initialize(alignment.readNextAlign());
      } else {
        t.initializeUnaligned();
      }

      // write tree to file
      treeWriter.write(t);

      // print tree to console

      System.out.println(t.toSentence());
      if (verbose) {
        System.err.println(t.toString());
        // t.recursivePrint();
      }
      System.err.println("#######################################################################");
    }
    long stop = System.currentTimeMillis();
    System.err.println("...done! [" + (stop - start) / 1000 + " sec].");

    treeWriter.close();
  }
 public Node addChild(Node node) {
   myChildElements.add(node);
   node.setParent(this);
   return node;
 }