Ho provato a risolvere così:
Ho creato un puntatore a funzione:
codice:
typedef int (*treeInInt) (tree, int);
Ecco le functions:
codice:
/* sommo le chiavi */
int sumKey(tree *root, int sum){
if( root == NULL ){
return sum;
} else {
sumKey(root->left);
sum=sum+root->info;
sumKey(root->right);
sum=sum+root->info;
}
}
/* sumKey */
int Sum_After_Level_k(tree *root, int levels, int k, int sum, treeInInt sumKey){
if ( levels < k ){
Sum_After_Level_k(root->left, levels+1, k, sum, sumKey);
Sum_After_Level_k(root->right, levels+1, k, sum, sumKey);
} else {
sumKey(root, sum);
Sum_After_Level_k=sumKey(root, sum);
}
}
Questi sono i prototipi delle funzioni:
codice:
int sumKey(tree *root, int sum);
int Sum_After_Level_k(tree *root, int levels, int k, int sum, treeInInt sumKey);
Ma ho questi errori dal compilatore, che non riesco a risolvere:
codice:
Compiling: C:\Users\Gaten\Desktop\Untitled1.cpp
C:\Users\Gaten\Desktop\Untitled1.cpp: In function 'int main()':
C:\Users\Gaten\Desktop\Untitled1.cpp:47: error: invalid conversion from 'int (*)(tree*, int)' to 'int'
C:\Users\Gaten\Desktop\Untitled1.cpp:20: error: too few arguments to function 'int Sum_After_Level_k(tree*, int, int, int, int (*)(tree, int))'
C:\Users\Gaten\Desktop\Untitled1.cpp:47: error: at this point in file
C:\Users\Gaten\Desktop\Untitled1.cpp: In function 'int sumKey(tree*, int)':
C:\Users\Gaten\Desktop\Untitled1.cpp:96: error: too few arguments to function 'int sumKey(tree*, int)'
C:\Users\Gaten\Desktop\Untitled1.cpp:100: error: at this point in file
C:\Users\Gaten\Desktop\Untitled1.cpp:96: error: too few arguments to function 'int sumKey(tree*, int)'
C:\Users\Gaten\Desktop\Untitled1.cpp:102: error: at this point in file
C:\Users\Gaten\Desktop\Untitled1.cpp: In function 'int Sum_After_Level_k(tree*, int, int, int, int (*)(tree, int))':
C:\Users\Gaten\Desktop\Untitled1.cpp:114: error: conversion from 'tree*' to non-scalar type 'tree' requested
C:\Users\Gaten\Desktop\Untitled1.cpp:115: error: conversion from 'tree*' to non-scalar type 'tree' requested
Process terminated with status 1 (0 minutes, 0 seconds)
9 errors, 0 warnings