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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:10+00:00 2026-06-14T17:19:10+00:00

I am trying to traverse a binary tree using twalk() with <search.h> #define _GNU_SOURCE

  • 0

I am trying to traverse a binary tree using twalk() with <search.h>

   #define _GNU_SOURCE     /* Expose declaration of tdestroy() */
   #include <search.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <time.h>

   void *root = NULL;

   void *
   xmalloc(unsigned n)
   {
       void *p;
       p = malloc(n);
       if (p)
           return p;
       fprintf(stderr, "insufficient memory\n");
       exit(EXIT_FAILURE);
   }

   int
   compare(const void *pa, const void *pb)
   {
       if (*(int *) pa < *(int *) pb)
           return -1;
       if (*(int *) pa > *(int *) pb)
           return 1;

       return 0;
   }

   void
   action(const void *nodep, const VISIT which, const int depth)
   {
       int *datap;

       switch (which) {
       case preorder:
           break;
       case postorder:
           datap = *(int **) nodep;
           printf("%6d\n", *datap);
           break;
       case endorder:
           break;
       case leaf:
           datap = *(int **) nodep;
           printf("%6d\n", *datap);
           break;
       }
   }

   int
   main(void)
   {
       int i, *ptr;
       void *val;

       srand(time(NULL));
       for (i = 0; i < 12; i++) {
           ptr = (int *) xmalloc(sizeof(int));
           *ptr = rand() & 0xff;
           val = tsearch((void *) ptr, &root, compare);
           if (val == NULL)
               exit(EXIT_FAILURE);
           else if ((*(int **) val) != ptr)
               free(ptr);
       }
       twalk(root, action);
       tdestroy(root, free);
       exit(EXIT_SUCCESS);
   }

As you can see there is no way to pass to or return any variable from action().
why is so hermetic? I can’t use any global because the program uses threads, my question is: how can I traverse (and share nodep with non-global variable) in thread-safe mode?
Excuse my poor english

EDIT:
As unwind said, the solution is to re-invent this particular wheel, redefine the structure used at tsearch.c solves the problem:

/* twalk() fake */

struct node_t
{
    const void *key;
    struct node_t *left;
    struct node_t *right;
    unsigned int red:1;
};

static void tmycallback(const xdata *data, const void *misc)
{
    printf("%s %s\n", (const char *)misc, data->value);
}

static void tmywalk(const struct node_t *root, void (*callback)(const xdata *, const void *), const void *misc)
{
    if (root->left == NULL && root->right == NULL) {
        callback(*(xdata * const *)root, misc);
    } else {
        if (root->left != NULL) tmywalk(root->left, callback, misc);
        callback(*(xdata * const *)root, misc);
        if (root->right != NULL) tmywalk(root->right, callback, misc);
    }
}

/* END twalk() fake */

if (root) tmywalk(root, tmycallback, "Hello walker");
  • 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-14T17:19:11+00:00Added an answer on June 14, 2026 at 5:19 pm

    I guess nobody can answer the “why” exactly, except those who specified and implemented the functions. I guess “shortsightedness”, or maybe “historical reasons” (they did it before thread programming became a common thing).

    Anyway, this API seems a bit “toyish” to me due to this limitation, as do in fact all APIs that fail to include a user-owned void * that is just opaquely passed around between API and any callbacks.

    So, the solution I guess is to re-invent this particular wheel, and write your own functions to traverse a binary tree.

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

Sidebar

Related Questions

I am trying to traverse Binary search tree with the follwoing code and my
I'm trying to traverse a binary tree in C. My tree contains an AST
I'm trying to traverse a tree using ANTLR tree commands and recursion. The code
I'm trying to write a function to traverse a tree with depth first search.
I am trying to implement a binary search tree. Here is the code I
I am trying to search through a binary search tree and store each node
I'm trying to traverse a 3D KD-Tree in my raytracer. The Tree is correct,
I'm trying to traverse a tree via TBB tasks and continuations. The code is
I am trying to traverse all directories under the current directory using a shell
I'm trying to recursively traverse through my Drive to search some files. The code

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.