So, I have a tree data structure which should work fine but now I want to print the results. My problem is if the parent node has two children (right and left) which aren't null; what path should it take? If it takes lets say right then all of the left ones after that node get left out?
I want the tree to be in the correct order
Example of what it'll do if I do not order it correctly:
-----------------------------root---------------------------------------
-------------------value---------------skips this value-----------------
-------------value-------skips this value-- skips this value -- skips this value
----------value -------skips this value----- skips this value ---- skips this value
What I'd like it to do:
-----------------------------------root----------------------------------
---------------------------value-----------value-------------------------
----------------------value----value-----value---value-------------------
--------------------value--value--value-value-value-value-value
and so on(I'd continue on but it gets cluttered)
I'll need to place my nodes into a stack and order them in advance. Suggestions?
Related
I have a gremlin query which finds the vertices I want to archive but it is returning an empty array.
My graph is laid out in a way where a vertex can have multiple parents and children. When a vertex is archived then it needs to archive all of it's affected descendants that would be 'orphaned' by this process. If any of the descendants have a path back to a central vertex then it shouldn't archive it because it won't be 'orphaned'
g.V(itemId) // Find the item to delete.
.union( // Start a union to return
g.V(itemId), // both the item
g.V(itemId) // and its descendants.
.repeat(__.inE('memberOf').outV().store('x')) // Find all of its descendants.
.cap('x').unfold() // Unfold them.
.where(repeat(out('memberOf') // Check each descendant
.where(hasId(neq(itemId))).simplePath()) // to see if it has a path back that doesn't go through the original vertex
.until(hasId(centralId))) // that ends at the central vertex .
.aggregate('exception') // Aggregate these together.
.select('x').unfold() // Get all the descendants again.
.where(without('exception'))) // Remove the exceptions.
.property('deleted', true) // Set the deleted property.
.valueMap(true) // Rteurn the results.
When it finds that some of the descendants have path(s) back to the central vertex that don't go through the original vertex then it works and returns all the results that it should. However, if it can't find any descendants with paths back then in theory it should just return all of the descendants. Instead, it is returning an empty array. My guess is that it's getting stuck after that stage of the traversal because it is finding nothing and can therefore not move on to anything else.
How can I return all of the descendants if it finds nothing at that stage?
For an example of the graph please see my previous question.
Change the line:
.select('x').unfold()
To:
.cap('x').unfold()
Currently I have a structure that looks like:
Name,Type, RefName, RefType
The goal is that the user selects an element and a direction and then get a treestructure in a webpage (JSON data) that displays all elements matching his criteria. As you can imagine, going up is not hard. The tree structure gets narrower and thus performance is no issue. While going down more and more nodes get added and at a certain point it gets slow. At this moment the nodes are added to a HashSet and I go recursively trough them. My program structure for going down is:
private void BuildChildNodes(ElementRefItem element, int goalDepth, int currentDepth =0)
{
if (goalDepth <= currentDepth)
{
return;
}
currentDepth++;
var elements = refElementRepository.All().Where(x => x.ElementName == element.Name);
foreach (var refElement in elements)
{
var node = CreateNode(refElement.ElementRefName, refElement.ElementRefType);
BuildChildNodes(node, goalDepth, currentDepth);
element.ChildNodes.Add(node);
}
}
I am looking for tips to optimize this part of the code. Are there other ways of doing this faster?
(Actual data structure is a bit more complex, but for clarity it is easier to show this version)
Instead of performing a query every time I retrieve the data once (all data is refreshed once a day) and I build two HashSets; one for parent-child relations and one for child-parent relations. This makes it possible to find an element in the HashTable (witch is very fast) and therefor retrieving the whole tree part. In meta code it looks like this:
- Create new cache (bottom up, top down)
- Foreach CachedObject
- Check if items exist (both parent and child)
: yes -> select node
: no -> create node and insert it in the new cache
- add child to parent node
This gives a hashset of Parent objects. Now you can select a parent object and automagically you get your whole tree section.
I am trying to get all the elements in a selected "Xpath location" and add them to a comboBox/ drop down list.
I tried to select all these elements by using the Xpath query : /#* with the Select method from the XpathNodeIterator.
The problem is that it return an iterator which does not move forward, in fact, it says it has no childs and its not allowing me to convert the iterator neither to an xmlElement or node so that I could at least search inside them.
This is the code :
while (anIterator.MoveNext())
{
//im trying to select all nodes of selected path which and return them to an iterator
secondIterator = anIterator.Current.Select("/#*");
while (secondIterator.MoveNext())
{
aNode = new Nodes();
aNode.Name = anIterator.Current.MoveToFirstChild().ToString();
nodeList.Add(aNode);
}
nodeList.Add(aNode);
}
Any solution to get all the elements in the current node ?
This is definitely harder to answer without the XML file you're working with, and without you telling us what you're hoping to see, and what you're currently seeing.
Here are some observations:
The '#' character in your XPath suggests you're trying to get all of the attributes of the current Node. The '/' tells Path to start at the root of the DOM.
If you're trying to get all the elements under the current node, you might want to change to "./*"
The issue is the #, which indicates that you want to select the child attributes, not child elements. (See http://www.w3.org/TR/xpath/#path-abbrev).
Try anIterator.Current.Select("*") to get the child elements. If you want all descendant elements (not just immediate children), try anIterator.Current.Select(".//*")
I have a pretty large tree data structure that will get updated (nodes removed and added, etc). I have to traverse the tree using breadth first approach to visit all the nodes (to a certain breadth depth like 7) and put it a to a list. Then a function will loop through the nodes and return true if an information was found inside of the list.
This is taking a lot of time to loop through the nodes. I don't think I should map every single node with every single children node and then pull the node using Dictionary and get all its children (recursively). How should I do this? The fastest way is to map all nodes between each other like
Dictionary<Node, List<Node>>
node, list of all children (including children of children...) and then pull the nodes and get its like 2,000 children fast. But if a node is added or removed anywhere, all the dictionary nodes need to be updated (which seems like a hassle). The other way is to dynamically loop through it during run (which takes time). It is a matter of mapping everything together in the beginning during tree generation, or looping during run time.
What is the best way to handle this situation? Any idea, pointers, comments, any thing is helpful. Currently this operation takes about 25% of the program run time.
If you are just evaluating a predicate you can do that directly while traversing the tree, you don't have to put the full tree into a list first - unless you cache the list. Evaluating the predicate on the the tree nodes directly, you can short circuit / stop traversing early when you first find the information you are interested in.
Let's say I have a list of branches, how to find a list of tree list that are singly connected together? The below diagram should illustrate my point. The input is a list of branches in a single tree, as labeled, ie. 1, 2, 3 and so on
The process is as follows:
Create a function that accepts a tree node as a parameter
If the node has no children, print the value of the current node and return.
If the node has two children, end the current list of single node values, then recurse into the left node, then the right node
If the node has one child, add it to the list of values contained in the tree, then recurse into that node.
Continue.
According to image, there is a really simple solution.
Let's make a list, with elements, that are lists of the same type. Procedure will be called tree_lists(list, tree). All you need to do is:
Looking at current joint, you
have your list pointer on the first
element of list.
If there are
more than one child in current node: iterate through each
subtree, incrementing list pointer
and calling
tree_lists(list[i],current_subtree)
where i is list pointer and
current_subtree is current subtree
=)
If ony one child exists, just add this joint to the current list item and move on to next.
Of course, list pointer and list values must be somehow global and modified in recurion as well.