Skip to content

Instantly share code, notes, and snippets.

@pilky
Created May 16, 2013 14:59
Show Gist options
  • Save pilky/5592335 to your computer and use it in GitHub Desktop.
Save pilky/5592335 to your computer and use it in GitHub Desktop.
//We use a typedef to make things simpler
typedef void (*rxt_enumerator)(char *key, char *value);
void rxt_enumerate_node(rxt_node *root, rxt_enumerator callback) {
if (!root) return;
if (root->color) {
if (root->color == 2) root = root->value;
//All we change is that instead of printing out, we now call the function supplied to us
if (callback) callback(root->key, root->value);
return;
}
rxt_enumerate_node(root->left, callback);
rxt_enumerate_node(root->right, callback);
}
//To print as before we create the following function
void printKeyValuePair(char *key, char *value) {
printf("%s: %s\n", root->key, (char*)root->value);
}
//Then you call rxt_enumerate_node() and pass in the supplied function
void someFunction() {
rxt_enumerate_node(myNode, printKeyValuePair);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment