private String toStringTree(ASTNode rootNode) {
    Deque<ASTNode> stack = new ArrayDeque<ASTNode>();
    stack.push(this);

    while (!stack.isEmpty()) {
      ASTNode next = stack.peek();
      if (!next.visited) {
        if (next.parent != null && next.parent.getChildCount() > 1 &&
                next != next.parent.getChild(0)) {
          rootNode.addtoMemoizedString(" ");
        }

        next.rootNode = rootNode;
        next.startIndx = rootNode.getMemoizedStringLen();

        // Leaf
        if (next.children == null || next.children.size() == 0) {
          String str = next.toString();
          rootNode.addtoMemoizedString(next.getType() != HiveParser.StringLiteral ? str.toLowerCase() : str);
          next.endIndx =  rootNode.getMemoizedStringLen();
          stack.pop();
          continue;
        }

        if ( !next.isNil() ) {
          rootNode.addtoMemoizedString("(");
          String str = next.toString();
          rootNode.addtoMemoizedString((next.getType() == HiveParser.StringLiteral || null == str) ? str :  str.toLowerCase());
          rootNode.addtoMemoizedString(" ");
        }

        if (next.children != null) {
          for (int i = next.children.size() - 1 ; i >= 0 ; i--) {
            stack.push((ASTNode)next.children.get(i));
          }
        }

        next.visited = true;
      } else {
        if ( !next.isNil() ) {
          rootNode.addtoMemoizedString(")");
        }
        next.endIndx = rootNode.getMemoizedStringLen();
        next.visited = false;
        stack.pop();
      }

    }

    return rootNode.getMemoizedSubString(startIndx, endIndx);
  }
Tag this ASTNode with information about the object from which this node
originated.
parser/src/java/org/apache/hadoop/hive/ql/parse/ASTNode.java