I understand the difference between (Prefix) Trie, a Suffix Trie and a Suffix Tree and I am trying to write Java code for both. What is the Java representation/structure of the SuffixTrieNode and SuffixTreeNode class?
SuffixTrie representation:
class SuffixTrie{
SuffixTrieNode root;
class SuffixTrieNode{
SuffixTrieNode[] links;
}
}
SuffixTree representation:
class SuffixTree{
SuffixTreeNode root;
class SuffixTreeNode{
SuffixTreeNode[] links;
}
}
Thanks!!
A suffix trie uses a trie data structure. It is the simplest way to build a suffix tree:Suffix tree and Tries. What is the difference?.
Related
As the title say, is there a way to find the children of children node when listen or visit a node in ANTLR.
For example: (use grammars-v4-java lexer and parse rule)
First, I take a java file to grammar tree.
grun Java compilationUnit -gui Example.java
// Example.java
public class Example {
String name = "test";
void call(){
String name1 = "test";
}
}
and the grammar tree is
Then I try to use java to extends the baseListerner to listen enterClassDeclaration node. So I can get the ClassDeclarationContext node. I want to find the ClassDeclarationContext node's children of children that the child type is LocalDeclarationContext.
In this example:
public class MyListener extends JavaParserBaseListener {
#Override
public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
// find the children of children by ctx
List<ParserRuleContext> contexts = findChildContextBy(ctx, LocalVariableDeclarationContext.class);
super.enterClassDeclaration(ctx);
}
}
The variable contexts should has two elements. name and name1
I do not want to find the children one layer by one layer. emmm, Is there have a convenient way?
For a given parse tree it's easy to look up specific child nodes (at any nesting level) using ANTLR4's XPath implementation.
You can trigger that search from either the full parse tree return by the called parser rule or within a listener/visitor method for the particular subtree, for example:
List<ParseTreeMatch> matches = XPath.findAll(ctx, "//localVariableDeclaration", parser);
The return matches are instances of LocalVariableDeclarationContext (if any matched).
Note: the linked page describe two search utilities, parse tree matching and XPath, which can be used individually or together.
I am trying to create a tournament bracket using linked lists where each match of the tournament is a node that has 2 parent nodes. Is this possible?
Not sure about linked list. I think you need a class Team and a class Match. The match will have 2 fields, Team1 and Team2, a field round (top 16, top8 etc) and a field NextMatch that is an instance of Match, which will be the next round. All these Matches could be placed inside an ArrayList. Or this is how I would do it.
The structure you are describing is a tree, not a list.
While it is theoretically possible to use as list to model an array, and then use the array to model an arbitrary data structure, the result would be a complicated, inefficient mess.
A better idea would be model the matches using a custom Match class; e.g. something like this:
public class Match {
private Player player1;
private Player player2;
private Match leadupMatch1; // null in first round
private Match leadupMatch2; // null in first round
private Match winnersMatch;
private Match losersMatch; // if needed
private Result result;
...
}
public class Player {
...
}
public enum Result {
NONE,
PLAYER_1_WINS,
PLAYER_2_WINS
}
I have a XML file resulted from an input java file. I also have xPath expressions for the XML file.
I need a function that receives one xPath expression and return its java element (in the abstract syntax tree). I tried the below code:
First extract XML element based on the input xPath expression.
XPath xPath = XPathFactory.newInstance().newXPath();
String query = "//unit[1]/unit[1]/class[1]/block[1]/function[6]"; //a method
Node node = (Node) xPath.compile(query).evaluate(XmlDocument, XPathConstants.NODE);
However, I do not know how to link extracted XML node to Java element in the source code.
PS:
The reslut should be a node in the abstract syntax tree. I have AST created by spoon. Therefore, in the above example, I want to extract related CtMethodImpl.
node.getTextContent() is not the answer as it is possible that there is more than one instance with the similar text content.
To the best of my knowledge there is no 'direct' way of doing this.
This: "//unit[1]/unit[1]/class[1]/block[1]/function[6]" is what we call a signature in the sense that it uniquely identifies an element (somehow).
What I would do is to create a spoon processor and go through the entire AST checking each element to see if it matches the signature.
public class ProcessorExample <E extends CtElement> extends AbstractProcessor<E> {
HashMap<String, Node> nodes;
//Sets your XML Nodes here, sorted by signature
public void setNodes(HashMap<String, Node> nodes) {
this.nodes = nodes;
}
#Override
public void process(E element) {
if (nodes.containsKey(signature(element))) {
Node n = nodes.get(signature(element));
//YOU FOUND IT!
}
}
private String signature(E element) {
//YOU MUST PROVIDE THIS IMPLEMENTATION
//TO MATCH YOUR "//unit[1]/unit[1]/class[1]/block[1]/function[6]"
//KIND OF SIGNATURE
return null;
}
}
If I have a String[] such as { "root", "two", "1" } how can I get the DefaultMutableTreeNode from my JTree that is represented by this "path"?
For example if my JTree looks like this:
root
one
1
2
two
1 <-- I want this node
2
Assume all nodes are DefaultMutableTreeNodes.
First, you need to fetch the tree model with getModel() method. Once you have the model, it has the getRoot() method, to fetch the root of the tree. After that, you can follow with calls to getChild(Object parent, int index) and check if any of the children has the same name as the one provided in the String array. If you find such one, you can again call getChild(Object parent, int index), etc... until you arrive at the last String from the array. Then you have the corresponding tree node. You need to actually cast the tree nodes to DefaultMutableTreeNode, as the TreeModel uses Object as the tree elements (for pre-1.7 Java).
How can I represent a tree structure like below, in java?
"root"
| |
"leaf1" "leaf2"
| |
"leaf3" "leaf4"
|
"leaf5"
Are there any built-in or custom classes anyone can refer me to?
EDIT: I need to be able to easily traverse through nodes.
There is no generic tree type in the Java class libraries, or in either Guava or Apache Commons Collections.
The easiest solution is to implement the tree type yourself to do exactly what you require. The core functionality of a tree is trivial ... modulo that the details depend heavily on what the tree needs to contain and how your use-case requires it to behave.
(If you want to understand why there is no generic tree type, try to get your head around the discussion on this Guava issue - http://code.google.com/p/guava-libraries/issues/detail?id=174)
Following is simple binary tree, that would solve your purpose.
http://www.java2s.com/Code/Java/Collections-Data-Structure/BinaryTree.htm
Try this [very general though]:
public class Tree {
private Node root;
public Tree(String rootData) {
root = new Node();
root.data = rootData;
root.children = new ArrayList<Node>();
}
private class Node {
private String data;
private Node parent;
private List<Node> children;
}
}
It might not be part of the Collections API, but Swing's JTree TreeModel is certainly a generic tree implementation: https://docs.oracle.com/javase/7/docs/api/javax/swing/tree/TreeModel.html
Just make your own Node class:
Node {
T value;
Node left;
Node right;
}
For a more complex implementation see Java's N-ary tree DefaultMutableTreeNode