#include #include typedef struct node{ unsigned int value; struct node *left; struct node *right; }Node, *Tree; typedef void (*Post_process)(Node* node, void* arg); void init_process(Node* node, void* arg){ node->value = (long int)arg; } void display(Tree T){ if (!T) { return; } printf("%d\n", T->value); display(T->left); display(T->right); } void update_max(Node* node, void* arg){ if (node->value > *((int*) arg)) { *((int*) arg) = node->value; } } void map_tree(Tree T, Post_process func, void* init_arg){ if (!T) { return; } func(T, init_arg); map_tree(T->left, func, init_arg); map_tree(T->right, func, init_arg); } int main(int argc, char const *argv[]) { int x = 0; Tree T = malloc(sizeof(*T)); Node a, b, c, d, e, f; T->left = &a; a.left = &b; a.right = &c; b.left = NULL; b.right = NULL; c.left = NULL; c.right = NULL; T->right = &d; d.left = &e; d.right = NULL; e.left = NULL; e.right = &f; f.left = NULL; f.right = NULL; map_tree(T, init_process, (void*)5); f.value = 7; map_tree(T, update_max, (void*)&x); display(T); printf("\n%d\n", x); free(T); return 0; }