I have a question concerning Abstract Syntax Trees generated with Boost Spirit Library.
I've found many informations about deleting nodes and subtrees in a Binary Search Tree, but I cannot find the same information for ASTs. I have a node in an AS-Tree, and this node is root to a subtree of a complete tree. Now I want to delete the node and all of its children.
I don't know how to do it, and the Boost Spirit Documentation didn't help either.
Has anyone got any tips for me?
The Tree is generated with (Boost 1.46.1):
tree_parse_info<> info = ast_parse(expression.c_str(), parser, skipparser);
And the Expression is something like this:
(variable_17 OR variable_18) AND function( variable_17) <= 30 OR function( subkey_18) <= 30
I use
tree_match<iterator_t>::tree_iterator tree_it = info.trees.begin();
to get the beginning of the tree, and then I do check if one of the subtrees is redundant (doensn't have anything to do with the deleting itself). `Then I traverse through the tree using
tree_match<iterator_t>::tree_iterator children_it = tree_it->children.begin()
and calling the same function with its children (recursive). I can't post the complete code,but that's the most important part of it. I thought, that i can traverse to the leafnodes of a redundant subtree, and set them to null, or something like this. And then I go up the tree again, and delete all other children one after another. However, nothing has worked so far.
An example for traversing the tree: The Traversing
It's the answer.
If I can't delete any nodes, does anyone has an idea, how to create a new tree, based on the existing one, skiping the redundant parts of it.
Related
I am trying to find all the possible paths from one node in my graph that will visit all other nodes in the graph. I want the function to produce all possibilities of paths in my n*m graph. Each node in my graph has a vector of all neighbors nodes and a Boolean that check if the node is visited or not.
example:
a b
c d
will produce:
abcd
abdc
acbd
...
I tried the solution in this answer, but only return one path. How can I produce all possible paths?
It seems like in some situations by your description you could have infinite paths and a path of infinite length because you didn't specify that nodes couldn't be revisited.
You should implement depth first search and pass a reference to an array of marked (visited) nodes in your recursive DFS method assuming that you have a count of the number of nodes in your graph. After you visit each node, before you leave that node make sure you set it to false again so that it can be reaccessed via another node.
The implementation of this algorithm is really going to depend on how you implemented your graph structure and without the details all I can do is speculate that you have a linked structure with an adjacency list representing the different nodes. I also have no idea how the different nodes map to characters so that is another detail I have to speculate, but say that the nodes are represented by integers.
You need to pass into a DFS method the following: array of marked nodes, a linked list which contains the path information, starting node, (i.e, current node) and final node
void printAllPaths(LinkedList<Integer> currentPath, boolean[] marked, int current, int last){
for( all nodes adjacent to current, node ){
if(node == last){
currentPath.addLast(last);
System.out.println(currentPath);
currentPath.removeLast();
}else if(!marked[node]){
currentPath.addLast(node);
marked[node] = true;
printAllPaths(currentPath, marked, node, final);
marked[node] = false;
currentPath.removeLast();
}
}
}
This will be the basic idea of the code. I apologize if it doesn't compile in advance, but this should print out all of the paths.
Iam new to c++.I have to write a c++ program representing a graph where the node has many edges as input and single edge as its output.I should also label them.I felt using linked list would help me but i was struck by the thought that in a linked list i would only have a single edge to a given node and another edge going to the next node.Is there any possible way of representing many different edges from different nodes to a single node.
So a node should look something like this:
struct Node
{
Node(std::string label_) : label(label_) {}
std::string label;
std::vector<Node*> incoming;
Node* outgoing = nullptr;
}
Then it's just a matter of wiring them together. Note this is sort of like a linked list of strings, except that the "previous" pointer (incoming) is multiple instead of singular.
From here, note that what you are actually building is a tree (assuming one node has a null outgoing pointer). That may help you to find an existing data structure implementation that you can use instead of building your own.
I want to implement the following in C++:
1) Check whether the given word exists in a dictionary. The dictionary file is a huge file; consider 100MB or 3-4 Million words.
2) Suggest corrections for the incorrect word.
3) Autocomplete feature.
My Approach
1) I am planning to build a tree so searching will efficient.
2) I am not getting how to implement auto correction feature.
3) I can implement auto complete feature using trees
What's the best data structure and algorithm to implement all the above features?
I have been working on the same problem. So far the best solution I have come across is using a ternary search tree for auto completion. Ternary Search Trees are more space efficient than tries.
If im unable to find the looked up string in my ternary search tree then I use an already built BK Tree for finding the closest match. BK Tree internally uses Levenshtein distance.
You
Metaphones are also something you might want to explore however I havent gone into the depth of metaphones.
I have a solution in Java for BK TREE + TERNARY SEARCH TREE if you like.
You can do autocomplete by looking at all the strings in a given subtree. Some score to help you pick might help. This works something like if you have "te" you go down that path in the trie and the traverse the entire subtree there to get all the possible endings.
For corrections you need to implement something like http://en.wikipedia.org/wiki/Levenshtein_distance over the tree. You can use the fact that if you processed a given path in the trie, you can reuse the result for all the strings in the subtree rooted at the end of your path.
1) Aside from trees, another interesting method is BWT
http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
BWT suffix array can be easily used to track words with given prefix.
2) For error correction, modern approach is LHS:
http://en.wikipedia.org/wiki/Locality-sensitive_hashing#LSH_algorithm_for_nearest_neighbor_search
Some links randomly provided by google search:
https://cs.stackexchange.com/questions/2093/efficient-map-data-structure-supporting-approximate-lookup
https://code.google.com/p/likelike/
http://aspguy.wordpress.com/2012/02/18/the-magic-behind-the-google-search/
So I am currently working on a quick and dirty Python project that supports a data structure made out of a dictionary with keys being GOIDs from the open biological ontology format. It hashses to another dictionary that contains lists of parent nodes or terms and children nodes or terms that helps me form lists with all children or all ancestors for a given node in the ontology ( working with GO .obo file, if that helps anyone ).
My problem is that I have been looking for an algorithm that will help me return all the same nodes on the same level as a given node id which has to be relative because there could be more than one path to a node ( it is a directed acyclic graph, but there can be multiple parents per node ). I essentially need to look up the parents of a node, store the children of the parents all on a common list, and then repeat this process on every node added without repeating nodes or slowing down the computation significantly.
I'm think this can easily be done using a set to prevent duplicate entries, and just keeping track of which parents I have visited until all parents of siblings have been visited without being able to add a new parent, but my suspicions are this might be terribly inefficient. If anyone has experience with this kind of algorithm, and insights would be highly appreciated! Hope this is clear enough for a response.
Thanks!
Ok guys, so this is what I have developed so far, but it seems to keep giving me wrong values for some strange reason. Is there a minor error anyone can see where I accidentally not terminating correctly?
# A helper function to find generations of a given node
def getGenerationals(self,goid):
quit = False
visitedParents = set()
generation = set()
tempGen = set()
generation.add(goid)
while not quit:
quit = True
generation |= tempGen
tempGen = set()
print "TEMP GEN:",tempGen
for g in generation:
parents = set(self._terms[g]['p'])
for p in parents:
if p not in visitedParents:
visitedParents.add(p)
print "Parent:",p
quit = False
tempGen |= set(self._terms[p]['c'])
raw_input("Break")
return generation
# Working function
def getGeneration(self,goid):
generation = list(self.getGenerationals(goid))
generation.remove(goid)
return list(generation)
I need help with adding data to a tree. For example if i have 7+8*9-18/(1+2), how am I supposed to add it to a binary tree in a way that i can compute the result using a binary tree. I am beginner in learning tree structures so I am not very familiar with it.
After converting the post fix expression to infix, follow the below steps to construct a tree.
If it is number, add it to add to the stack.
If is is an operator, make the operator as parent node,
pop the element and make it as right child to the parent node,
pop the element and make it as left child to the parent node
and add the parent node to the stack.
See How to write an evaluator for a string like "(1+3 * ( 5 / 4)) and get a numeric result and the Shunting Yard Algorithm