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

  • SEARCH
  • Home
  • 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 6775825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:57:11+00:00 2026-05-26T15:57:11+00:00

Say we have the following structure: struct Tree { string id; int numof_children; Tree

  • 0

Say we have the following structure:

struct Tree {
  string id;
  int numof_children;
  Tree *children[5];
};

…where each id is unique (can only appear once in tree), how can I find the path to an id in this type of tree and output it?

I know how to reach the node and check if it exists, but I can’t figure out how to output the proper path.

Restrictions: No vectors/lists/stack data types may be used. Recursion only.
Recommendations: Function look(ATree *t, string &id) should have the return type of a string.

Is there a general structure of recursion that I can follow?

  • 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-26T15:57:11+00:00Added an answer on May 26, 2026 at 3:57 pm

    Recursion can be difficult to explain, but I’ll try my best explaining how it applys in this situation. Since you are able to recursively check weither each node exists, you will want to return the id as the return string. This notifies up the recursive stack that a match has been found. You then append the current node’s id to the string and return it up the stack. This in turn notifies up the stack that a match has been found, etc. Here is my solution, which I’ve added multiple comments to better illustrate the point.

    // If found return [string] [of] [ids], else empty string
    string look(const Tree * t, const string & id) {
       // If current node matchs return id
       if(t->id == id) {
          return "[" + id + "]";
       }
    
       // Loop over each child, recursively calling look
       for(int i=0; i<t->numof_children; i++) {
          string s = look(t->children[i], id);
          // If found add to string and return
          if(!s.empty()) {
             return "[" + t->id + "] " + s;
             // alternatively: return s + " [" + t->id + "]"; will genreate string in reverse
          }
       }
    
       // Not found, return empty string
       return string();
    }
    

    Most recursive functions follow a similar pattern. If you still a little lost, I would recommend running it through a debugger so you can see exactly what is going on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Lets say I have a following structures in c++ struct Fruit { std::string name,
Let's say I have the following structure declaration (simple struct with no constructor). struct
Say I have the following C structure definition: struct stringStructure { char *stringVariable; };
Lets say we have the following structure in a web page. How can i
Let's say you have the following structure in C#: struct Piece : IEquatable<Piece> {
I have the following structure: Table: products id, name, sort_order Let's say I only
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Let's say I have the following class structure: class Car; class FooCar : public
Let's say we have a solution with the following structure: Project.DAL - Data access

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.