#include #include #include struct lnode{ int value; lnode *right; lnode *left; }; typedef lnode node; int func(int arg){ return arg*arg; } int func2(int arg1,int arg2){ return arg1*arg2; } void map( node *root, int (*pt2Func)(int)) { //printf("value:%d",root->value); root->value=(*pt2Func)(root->value); if(root->right!=NULL) map(root->right,(*pt2Func)); if(root->left!=NULL) map(root->left,(*pt2Func)); } int accumulate (node *root,int nulvalue,int(*pt2Func)(int,int)) { if(root==NULL) return nulvalue; printf("gelen:%d",root->value); int acc=root->value; acc=(*pt2Func)(acc,accumulate(root->right,nulvalue,(*pt2Func))); acc=(*pt2Func)(acc,accumulate(root->left,nulvalue,(*pt2Func))); return acc; } node * bst_insert(node * root,int value){ if(root==NULL){ root=(node * ) malloc(sizeof(node)); root->value=value; root->right=(node *)0; root->left=(node *)0; } if(value < root->value){ if(root->left==(node *)0){ root->left = bst_insert(root->left,value); } else bst_insert(root->left,value); } if(value > root->value){ if(root->right==(node *)0){ root->right= bst_insert(root->right,value); } else bst_insert(root->right,value); } return root; } void printBST(node * root){ if(root!=(node *)0){ printf("(%d",root->value); printf("\n"); printBST(root->left); printf("\t"); printBST(root->right); printf(")\n"); } else printf("bos"); } int main(){ node root; node *a; a=NULL; int (* fp2)(int,int); fp2=func2; a=bst_insert(a,10); a=bst_insert(a,20); a=bst_insert(a,30); a=bst_insert(a,5); a=bst_insert(a,15); printBST(a); printf("accumulate: %d",accumulate(a,1,fp2)); int (*fp)(int); fp=func; map(a,fp); printBST(a); printf("accumulate: %d",accumulate(a,1,fp2)); getch(); }