c++ - Depth-first search -
i have suffix tree, each node of tree struct
struct state { int len, link; map<char,int> next; }; state[100000] st;
i need make dfs each node , strings can reach, don't know how make. dfs function
void getnext(int node){ for(map<char,int>::iterator = st[node].next.begin();it != st[node].next.end();it++){ getnext(it->second); } }
it perfect if can make like
map<int,vector<string> >
where int node of tree , vector strings can reach
now works
void createsuffices(int node){//, map<int, vector<string> > &suffices) { if (suffices[sz - 1].size() == 0 && (node == sz - 1)) { // node leaf // add vector node containing // 1 element: empty string //suffices[node] = new vector<string> //suffices.add(node, new vector<string>({""})); vector<string> r; r.push_back(string()); suffices[node] = r; } else { // node not leaf // create vector built vector<string> v; // loop on each child for(map<char,int>::iterator = st[node].next.begin();it != st[node].next.end();it++){ createsuffices(it->second); vector<string> t = suffices[it->second]; for(int = 0; < t.size(); ++){ v.push_back(string(1,it->first) + t[i]); } } suffices[node] = v; } }
you can pass map<int, vector<string>>
depth first search. when recursive call returns node n
, know suffices node ready. c++ skills limited, i'll write in pseudo-code:
void createsuffices(int node, map<int, vector<string>> suffices) { if (st[node].next.empty()) { // node leaf // add vector node containing // 1 element: empty string suffices.add(node, new vector<string>({""})); } else { // node not leaf // create vector built vector<string> v; // loop on each child foreach pair<char, int> p in st[node].next { // handle child createsuffices(p.second, suffices); // prepend character suffices of child foreach string suffix in suffices(p.second) { v.add(concatenate(p.first, suffix)); } } // add created vector suffix map suffices.add(node, v); } }
Comments
Post a Comment