How to get a child tree node using the name and not the range of the node in the child node list .
i found this method but it uses the position of the element in the children list:
selectedNode.getChildren().get(i).
Many thanks
The collection you receive by calling getChildren() is a standard Java collection IIRC and is not indexed by name. The only ways I can think of realizing this is to create your own Node implementation or to iterate over the collection (which I think is the easiest solution).
public Node getNodeByName(String name)
{
for (Node n : selectedNode.getChildren())
{
if (name.equals(n.getName())
{ return n; }
}
return null;
}
Related
Firstly, i am sorry but i don't speak english very well. Secondly, i have a problem with nodes which are put in a gridpane. In fact, if the focus is taken by the first one wich is located on the top left side, when i push the tab key, the focus is not taken by the other which is located on the right.
People ask me to use the traversalEngine abstract class in order to solve this problem. Nevertheless, when i try to implement an engine object, it doesn't work if i put the parameters which are shown everywhere on the web:
TraversalEngine engine = new TraversalEngine(gridPane, false) {
It ask me to remove the parameters. If i do it, i don't have access to the trav method. In fact, it is the getRoot method which appears and can be implemented :
TraversalEngine engine = new TraversalEngine() {
#Override
protected Parent getRoot() {
// TODO Auto-generated method stub
return null;
}
}
Is there something which can be make in order to solve this problem ?
Thanks you for your help
Vinz
The traversal order for focusing nodes in a parent is the order in which they occur in the child list. Assuming every child contains at most one focusable node you could simply add the children line by line or reorder the children.
This could be done programmatically of course, but adding the children in the correct order in the first place would be more efficient...
public static int getColumnIndex(Node n) {
Integer i = GridPane.getColumnIndex(n);
return i == null ? 0 : i;
}
public static int getRowIndex(Node n) {
Integer i = GridPane.getRowIndex(n);
return i == null ? 0 : i;
}
grid.getChildren().sort(Comparator.comparingInt(ContainingClass::getRowIndex).thenComparingInt(ContainingClass::getColumnIndex));
So my question is as follows:
When I run the code for this insert helper method, and I am positive my new node method is correct as it works for instantiating a Binary Search Tree, no nodes are inserted. Why can't I use this certain implementation? What's going wrong here?
I know how to use the other insert implementation where one would check for the left and right nodes of the root and whether or not they are null, but can not figure out the problem of this more elegant possibility. The answer to this will help me in creating other functions that go beyond the scope of the insert function.
btw yes I have another function calling this helper function
Thanks!!!!!
//INSERT METHODS
void BinarySearchTree::insert(int data, struct node* root) {
//If root is null make new node there
if (!root) {
root = new node(data);
}
else if (root -> data > data) {
insert(data, root -> left);
}
else {
insert(data, root -> right);
}
}
The variable root is a parameter, which only has local visibility for that one method call. Meaning root = new node(data) will indeed create a new node, but that will only be pointed to by the parameter. Your method doesn't return anything and it doesn't actually know what it is supposed to do with that new root object of yours (it is NOT the same as any class variable you might have defined that is named the same).
So you create a new node, but can't use it outside that one method call. Which results in an empty tree.
As a side note for future questions: Include a tag for the programming language you are using. A lot of people use that as a filter, so you will actually get more people looking at this if you use the right tag.
I have a simple graph, with parents and children being vertices.
Parents have the relationship "isParentOf" to their children.
The vertices all have one property: the "familyName".
I want to use gremlin to match all the parents whose child's familyName is different from theirs.
Note: I cannot use the Groovy syntax of Gremlin. I must use pure Java code only.
The GremlinPipeline should look like this:
find all parents
follow the "isParentOf" relationship and get all the children
filter the children through a PipeFunction that compares the parent's "familyName" with the child's "familyName"
The problem is in the last step. How to retrieve the parent's "familyName", when this pipeline step only has access to (what is coming from the previous step, that is to say) the children?
My answer:
Accessing previous steps of a GremlinPipeline in a filter's PipeFunction is not possible. BUT it is possible if you use a PipesFunction instead (note the 's' !).
Let's look at the javadoc here:
public PipesFunction extends PipeFunction{
public AsMap getAsMap();
}
So you should setup the GremlinPipeline like this:
find all parents
name that step as "theParent"
follow the "isParentOf" relationship and get all the children
filter the children with a PipesFunction like this:
.filter(new PipesFunction<Vertex,Boolean>()
{
public Boolean compute(Vertex child) {
return parentHasDifferentFamilyName(child);
}
private Boolean parentHasDifferentFamilyName(child){
Vertex theParent = getAsMap().get("theParent");
String theParentFamilyName = theParent.getProperty("familyName");
String childFamilyName = child.getParameter("familyName");
return !(childFamilyName.equals(parentFamilyName));
}
})
Note: in the step 4, we could retrieve the "theParent" vertex thanks to the getAsMap() method, and thanks to the step 2 (that implicitly filled the "As" map).
I have a List for example
{ "in" , "out", "rec", "auth" }
... but the content of the list is not predictable.
When iterating the list list how can we know we have reached last element?
I want to apply different logic for the last element.
Example : List list = new ArrayList be the list, you need not traverse to get the last( element, you can get it by list.get(list.size()-1) and perform the logic you wanted.
The "classic" way to iterate through a Java List is to use List.iterator() to obtain an Iterator, then use the Iterator's methods to step through the list values.
This works with anything that implements Iterable, not just Lists.
// assuming myList implements Iterable<Type>
Iterator<Type> iterator = myList.iterator();
while(iterator.hasNext()) {
doSomethingWith(iterator.next())
}
Since JDK 1.5, there has been a shortcut in the language to achieve the same thing:
// assuming myList implements Iterable<Type>
for(Type item : myList) {
doSomethingWith(item);
}
However, while convenient in many situations, this syntax doesn't give you full access to all the information Iterator has.
If you want to treat the last element of the list specially, one method might be:
Iterator<Type> iterator = myList.iterator();
while(iterator.hasNext()) {
Type item = iterator.next();
if(iterator.hasNext() {
doSomethingWith(item);
} else {
// last item
doSomethingElseWith(item);
}
}
Your specific situation - creating a comma-separated string representation of the list, without a trailing comma:
Iterator<String> iterator = myList.iterator();
StringBuilder buffer = new StringBuilder();
while(iterator.hasNext()) {
buf.append(iterator.next());
if(iterator.hasNext() {
buf.append(",")
}
}
All this assumes that there's a reason you want to avoid using list.size().
You should consider using LinkedList instad of ArrayList. It has getLast() method.
I have a class called Node.
public Node
{
public int data;
public Node primaryNext;
public Node secondaryNext;
}
I have a Node root = null; And when the first value is received from the input, it then runs something like this.
root = new Node;
root.data = /*input*/ ;
root.primaryNext = null;
root.secondaryNext = null;
The next step is adding a new Node at the end of the list, by pointing root.primaryNext or root.secondaryNext to a new Node while filling the "pointer" tree by levels. So I need to do something like this:
GIF of the idea.
I think that this could be done using ||, &&, |, & operators applied to each level of nodes with a recursive method. So:
How do I operate in C# like the RPN?
If I can, which would be the best way to do it? I understand recursion pretty well, but I might not do the best possible method.
Thanks.
Suggestion:
One command to push a single node onto a stack.
Another command to take the two topmost nodes from the stack, combine them and push the result back on the stack.