I have following recursive algorithm needed to redactor into iterative process.
CvSeq is a tree structure.Where contour->h_next gives the next node in the same level.
contour->v_next gives the next contour in level below.(child node)
void helperParseCurves(CvSeq* contour, int level) {
if(contour->h_next != NULL) {
helperParseCurves(contour->h_next, level);
}
if(contour->v_next != NULL) {
helperParseCurves(contour->v_next, level+1);
}
//Process the nodes in contour
for(int i=0; i<contour->total; i++){
CvPoint* p = CV_GET_SEQ_ELEM(CvPoint, contour, i);
//Paint the point p
}
}
I want to refactor this logic into iterative algorithm.
Any tips on this?
To traverse nodes w/o recursion you will need stack for saving previous states. [Recursion is actually using of stack as well…] :