int TreeSize(NodeTree* ApT) { if(ApT == NULL) { return 0; } else { return 1 + TreeSize(ApT->left) + TreeSize(ApT->right); } }
int main(void){ int i=0, array[]={34,12,65,23,2,100,120}; Node* root = NULL; root = TreeInit(49); for (i=0;i<7;++i){ NodeInsert(root,array[i]); } InorderPrintTree(root); putchar('\n'); PostorderPrintTree(root); printf("\nAddress of node with key=13 is %p\n",(void *)TreeSearch(root,13)); printf("Minimum : %d \n", (TreeMinimum(root))->key); printf("Maximum : %d \n", (TreeMaximum(root))->key); printf("Treesize : %d \n", TreeSize(root)); printf("Max depth : %d \n", MaxDepth(root)); TreeDelete(root); getchar(); return 0; }
main() { Node root; Node longLivedTree; Node tempTree; double *array; int i; int d; printf ("Garbage Collector Test\n"); printf (" Live storage will peak at %d bytes.\n\n", 2 * sizeof(struct Node0) * TreeSize(kLongLivedTreeDepth) + sizeof(double) * kArraySize); printf (" Stretching memory with a binary tree of depth %d\n", kStretchTreeDepth); PrintDiagnostics(); /* Stretch the memory space quickly */ tempTree = MakeTree(kStretchTreeDepth); free_Node(tempTree); tempTree = 0; /* Create a long lived object */ printf (" Creating a long-lived binary tree of depth %d\n", kLongLivedTreeDepth); longLivedTree = leaf_Node(); Populate(kLongLivedTreeDepth, longLivedTree); /* Create long-lived array, filling half of it */ printf (" Creating a long-lived array of %d doubles\n", kArraySize); array = (double *) malloc(kArraySize*sizeof(double)); for (i = 0; i < kArraySize/2; ++i) { array[i] = 1.0/i; /* sic */ } PrintDiagnostics(); for (d = kMinTreeDepth; d <= kMaxTreeDepth; d += 2) { TimeConstruction(d); } if (longLivedTree == 0 || array[1000] != 1.0/1000) printf ("Failed\n"); /* Fake reference to LongLivedTree */ /* and array */ /* to keep them from being optimized away */ PrintDiagnostics(); }
// Number of iterations to use for a given tree depth static int NumIters(int i) { return 2 * TreeSize(kStretchTreeDepth) / TreeSize(i); }
void main() { Node root; Node longLivedTree; Node tempTree; long tStart, tFinish; long tElapsed; #ifdef GC // GC_full_freq = 30; GC_enable_incremental(); #endif cout << "Garbage Collector Test" << endl; cout << " Live storage will peak at " << 2 * sizeof(Node0) * TreeSize(kLongLivedTreeDepth) + sizeof(double) * kArraySize << " bytes." << endl << endl; cout << " Stretching memory with a binary tree of depth " << kStretchTreeDepth << endl; PrintDiagnostics(); tStart = currentTime(); // Stretch the memory space quickly tempTree = MakeTree(kStretchTreeDepth); # ifndef GC delete tempTree; # endif tempTree = 0; // Create a long lived object cout << " Creating a long-lived binary tree of depth " << kLongLivedTreeDepth << endl; # ifndef GC longLivedTree = new Node0(); # else longLivedTree = new (GC_NEW(Node0)) Node0(); # endif Populate(kLongLivedTreeDepth, longLivedTree); // Create long-lived array, filling half of it cout << " Creating a long-lived array of " << kArraySize << " doubles" << endl; # ifndef GC double *array = new double[kArraySize]; # else double *array = (double *) GC_MALLOC_ATOMIC(sizeof(double) * kArraySize); # endif for (int i = 0; i < kArraySize/2; ++i) { array[i] = 1.0/i; } PrintDiagnostics(); for (int d = kMinTreeDepth; d <= kMaxTreeDepth; d += 2) { TimeConstruction(d); } if (longLivedTree == 0 || array[1000] != 1.0/1000) cout << "Failed" << endl; // fake reference to LongLivedTree // and array // to keep them from being optimized away tFinish = currentTime(); tElapsed = elapsedTime(tFinish-tStart); PrintDiagnostics(); cout << "Completed in " << tElapsed << " msec" << endl; # ifdef GC cout << "Completed " << GC_gc_no << " collections" <<endl; cout << "Heap size is " << GC_get_heap_size() << endl; # endif }
int main() { Node root; Node longLivedTree; Node tempTree; long tStart, tFinish; long tElapsed; int i, d; double *array; #ifdef GC // GC_full_freq = 30; // GC_free_space_divisor = 16; // GC_enable_incremental(); #endif printf("Garbage Collector Test\n"); printf(" Live storage will peak at %d bytes.\n\n", 2 * sizeof(Node0) * TreeSize(kLongLivedTreeDepth) + sizeof(double) * kArraySize); printf(" Stretching memory with a binary tree of depth %d\n", kStretchTreeDepth); PrintDiagnostics(); # ifdef PROFIL init_profiling(); # endif tStart = currentTime(); // Stretch the memory space quickly tempTree = MakeTree(kStretchTreeDepth); # ifndef GC destroy_Node(tempTree); # endif tempTree = 0; // Create a long lived object printf(" Creating a long-lived binary tree of depth %d\n", kLongLivedTreeDepth); # ifndef GC longLivedTree = calloc(1, sizeof(Node0)); # else longLivedTree = GC_NEW(Node0); # endif Populate(kLongLivedTreeDepth, longLivedTree); ggggc_collectFull(); // Create long-lived array, filling half of it printf(" Creating a long-lived array of %d doubles\n", kArraySize); # ifndef GC array = malloc(kArraySize * sizeof(double)); # else # ifndef NO_PTRFREE array = GC_MALLOC_ATOMIC(sizeof(double) * kArraySize); # else array = GC_MALLOC(sizeof(double) * kArraySize); # endif # endif ggggc_collectFull(); for (i = 0; i < kArraySize/2; ++i) { array[i] = 1.0/i; } PrintDiagnostics(); for (d = kMinTreeDepth; d <= kMaxTreeDepth; d += 2) { TimeConstruction(d); } if (longLivedTree == 0 || array[1000] != 1.0/1000) fprintf(stderr, "Failed\n"); // fake reference to LongLivedTree // and array // to keep them from being optimized away tFinish = currentTime(); tElapsed = elapsedTime(tFinish-tStart); PrintDiagnostics(); printf("Completed in %d msec\n", tElapsed); # ifdef GC printf("Completed %d collections\n", GC_gc_no); printf("Heap size is %d\n", GC_get_heap_size()); # endif # ifdef PROFIL dump_profile(); # endif }