Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:04:27+00:00 2026-05-25T22:04:27+00:00

I have following recursive algorithm needed to redactor into iterative process. CvSeq is a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:04:28+00:00Added an answer on May 25, 2026 at 10:04 pm

    To traverse nodes w/o recursion you will need stack for saving previous states. [Recursion is actually using of stack as well…] :

    struct StackData
    {
     CvSeq* contour;
     int level;
     int traversed;
    };
    
    const int traversed_l = (1 << 0);
    const int traversed_r = (1 << 1);
    
    const int stack_size = 50; // should be at leas max depth
    StackData stack[stack_size];
    int stack_p = 0;
    
    void helperParseCurves(CvSeq* contour, int level) {
    
        int traversed = 0;
    
        while(contour)
        {
           if(contour->h_next != NULL && !(traversed & traversed_l)) { // down to left
            assert(stack_p < stack_size);                             // and save current state
            traversed |= traversed_l;
            stack[stack_p].contour = contour;
            stack[stack_p].level = level;
            stack[stack_p].traversed = traversed;
            ++stack_p;
            contour = contour->h_next;
            traversed = 0;
            continue;
            }
    
           if(contour->h_next != NULL  && !(traversed & traversed_r)) { // down to right
            assert(stack_p < stack_p);                             // and save current state
            traversed |= traversed_r;
            stack[stack_p].contour = contour;
            stack[stack_p].level = level;
            stack[stack_p].traversed = traversed;
            ++stack_p;
            contour = contour->v_next;
            level = level+1;
            traversed = 0;
            continue;
           }
    
    
           //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
           }
    
           // move up because either left and right nodes are NULL or already traversed
           if(!stack_p) break; // we are at the top
           contour = stack[stack_p].contour;
           level = stack[stack_p].level;
           traversed = stack[stack_p].traversed;
           --stack_p;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an algorithm that recursively makes change in the following manner: public static
I have the following recursive code and it doesn't behave as expected (see details
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
Problem: I have to design an algorithm, which does the following for me: Say
I have the following recursive function which works... up until a point. Then the
I have the following recursive code and i am getting a stackoverflow exception. I
I have the following recursive function: ALTER FUNCTION [dbo].[ListAncestors] ( @Id int ) RETURNS
I have the following recursive function for project euler question no. 74 : chain
Is there an algorithm to optimize a highly recursive function into an iteration over
I have the following example of a recursive function, and what I don't understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.