// Programme pour tester et mesurer les performances des // implémentations de WalkTree(). // Testé avec 32-bit Visual C++ 1.10. #include #include #include #include #include "tree.h" long VisitCount = 0; void main(void); void BuildTree(NODE *pNode, int RemainingDepth); extern void WalkTree(NODE *pRootNode); void main() { NODE RootNode; int i; long StartTime; // Construit un arbre test BuildTree(&RootNode, 14); // Parcourt l'arbre 1000 fois et examine le temps que cela prend StartTime = time(NULL); for (i=0; i<1000; i++) { WalkTree(&RootNode); } printf("Seconds elapsed: %ld\n", time(NULL) - StartTime); getch(); } // // Fonction pour ajouter des sous-arbres gauche et droit de la // profondeur spécifiée au nœud spécifié. // void BuildTree(NODE *pNode, int RemainingDepth) { if (RemainingDepth == 0) { pNode->pLeftChild = NULL; pNode->pRightChild = NULL; } else { pNode->pLeftChild = malloc(sizeof(NODE)); if (pNode->pLeftChild == NULL) { printf("Out of memory\n"); exit(1); } pNode->pRightChild = malloc(sizeof(NODE)); if (pNode->pRightChild == NULL) { printf("Out of memory\n"); exit(1); } BuildTree(pNode->pLeftChild, RemainingDepth - 1); BuildTree(pNode->pRightChild, RemainingDepth - 1); } } // // Fonction pour visiter le nœud afin que WalkTree() puisse effectuer // un appel. // void Visit(NODE *pNode) { VisitCount++; }