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 8875971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:06:27+00:00 2026-06-14T19:06:27+00:00

typedef struct dt { …..; } Data; typedef struct nd { int id; Data

  • 0
typedef struct dt {
    .....;
} Data;

typedef struct nd {
    int id;
    Data *data;
    struct tm *_parent;
    struct tm *_child[7];
} Node; 


Node* findNode(int id, Node *tree) {
    Node *p = tree;
    if (id == p->_id)
        return p;
    for (int i = 0; i < 7; i++) {
        if (id == p->_child[i]->_id) {
            p = p->_child[i];
            break;
        } else if(p->_child[i] != NULL) {
            findNode(id, p->_child[i]);
        }
    }

    return p;
}

I have a multi-way tree for which every node consists of 0-7 children. Children may be added and removed in no particular order. I am trying to build a search algorithm that given an id will search the tree and return a pointer to the particular node. I have tried to do it recursively as above, without much luck. Is it actually possible to build this algorithm recursively or do I also need to use a stack?

  • 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-06-14T19:06:28+00:00Added an answer on June 14, 2026 at 7:06 pm

    There are some problems with your end conditions. If the loop doesn’t find the id, it must be detectable through the return value. Making it NULL in that case would be sensible. This can be done by making two changes: instead of setting p and doing break, return it directly (that way the for loop will only finish if the id is not found), and change return p at the end into return NULL.

    Then when making the recursive call, you need to check the return value. If it was NULL, continue. Otherwise return it.

    And the previous answer is correct that the check for the child id can better be removed, too, especially because it needs to (but doesn’t) check for NULL.

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

Sidebar

Related Questions

I have this code : typedef struct node { int data; struct node *left;
typedef struct node { int data; struct node *next,*prev; }harsha; void create_leaf(harsha **pnode,int value)
typedef struct hash_table_data { int key; int data; struct hash_table_data* next; struct hash_table_data* prev;
This is C99 code: typedef struct expr_t { int n_children; foo data; // Maybe
typedef struct nodetype { int data; struct nodetype * left; struct nodetype * right;
typedef struct node { struct node *leftChild, *rightChild; int value; } bst; void insert(bst*
Consider the following: typedef struct { int a; int b; int c; int d;
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a struct typedef struct { int id; string name; string address; string
I have a structure typedef struct store { char name[11]; int age; } store;

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.