void mktree(char pre[],int pres,int pree,char in[],int is,int ie,tnode * * r) { int i; if(is>ie||pres>pree) * r=NULL; else { * r=(tnode *) malloc (sizeof(tnode)); (* r)->d=pre[pres]; for(i=is; i<=ie; i++) if(pre[pres]==in[i]) { mktree(pre,pres+1,pres+i-is,in,is,i-1,&(* r)->lchild); mktree(pre,pres+i-is+1,pree,in,i+1,ie,&(* r)->rchild); break; } if (i>ie) { printf("error:input contains an error! \n"); return; } } }
struct treenode * mktree(int i) { struct treenode * r = GC_MALLOC(sizeof(struct treenode)); if (0 == i) return 0; if (1 == i) r = GC_MALLOC_ATOMIC(sizeof(struct treenode)); r -> x = mktree(i-1); r -> y = mktree(i-1); return r; }
static void *gc_tree(gcthread_t thread) { unsigned i, j; mps_ap_t ap = thread->ap; obj_t leaf = pinleaf ? mktree(ap, 1, objNULL) : objNULL; for (i = 0; i < niter; ++i) { obj_t tree = mktree(ap, depth, leaf); for (j = 0 ; j < npass; ++j) { if (preuse < 1.0) tree = new_tree(ap, tree, depth); if (pupdate > 0.0) tree = update_tree(ap, tree, depth); } } return NULL; }
int main(int argc, char *argv[]) { int c, ret; while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) { switch (c) { case 'b': pbasedir = optarg; break; case 'l': nlevel = atoi(optarg); break; case 'd': ndir = atoi(optarg); break; case 'f': nfile = atoi(optarg); break; case '?': usage(argv[0]); } } if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) { usage(argv[0]); } ret = mktree(pbasedir, 1); return (ret); }
/* mktree - make a tree of nodes with depth d. */ static obj_t mktree(mps_ap_t ap, unsigned d, obj_t leaf) { obj_t tree; size_t i; if (d <= 0) return leaf; tree = mkvector(ap, width); for (i = 0; i < width; ++i) { aset(tree, i, mktree(ap, d - 1, leaf)); } return tree; }
int main() { int i; for (i = 0; i < 10; ++i) { root[i] = mktree(12); } GC_generate_random_backtrace(); GC_generate_random_backtrace(); GC_generate_random_backtrace(); GC_generate_random_backtrace(); return 0; }
bool FileSystem::mktree(const Path& path) { bool ret = true; std::pair<Path, Path> path_parts = path.split(); if (!path_parts.first.empty()) { ret &= mktree(path_parts.first); } if (!exists(path) && !mkdir(path)) { return false; } return ret; }
// combine function: takes two trees, combines their frequencies // and returns a parent tree with their sum as it's frequency Tree* combine(Tree* a, Tree* b) { // make parent tree Tree* parent_tree = mktree(); // set parent tree's freq. to the sum of it's children's freq. parent_tree->frequency = (a->frequency + b->frequency); // set parent tree's children parent_tree->left = a; parent_tree->right = b; // return combined 'parent' tree return parent_tree; }
void main() { tnode *r; int n=0; char pre[max],in[max]; printf("input inorder and postorder! \n"); gets(pre); gets(in); mktree(pre,0,strlen(pre)-1,in,0,strlen(in)-1,&r); printf("The preorder is as follows \n"); postorder(r); printf("\n There are %5d leaves in the tree. \n", leaf(r)); printf("\nThe one degree node is as follows:\n"); oneleaf(r,&n); printf("\n The one degree nodes number is:%d\n",n); }
// Function that takes two trees and combines them with a new parent Tree* combine(Tree* a, Tree* b) { // create a new parent tree Tree* combinedTree = mktree(); // make the parent's frequency the sum of its children's frequencies combinedTree->frequency = (a->frequency + b->frequency); // attach the smaller child to parent's left combinedTree->left = a; // attach the larger child to parent's right combinedTree->right = b; //return combined tree to the main function return combinedTree; }
/* * Extract the files from a pak. * * *d -> a pointer to the first element * of the pak directory * * *fd -> a file descriptor holding * the pak to be extracted */ static void extract_files(FILE *fd, directory *dirs, int num_entries) { int i; for(i=0; i<num_entries; ++i) { directory* d = &dirs[i]; mktree(d->file_name); if(d->is_compressed) { assert(dk_pak_mode != 0 && "Only Daikatana paks contain compressed files!"); extract_compressed(fd, d); } else { extract_raw(fd, d); } } }
int main(int argc, char **argv) { int fd1, fd2; struct lnode *clist; struct kpnode *tree; if (argc < 1) { fprintf(stderr, "Error: not enough arguments.\n"); return 1; } if ((fd1 = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "Error: cannot open file.\n"); return 1; } if ((clist = mklist(fd1)) == 0) { fprintf(stderr, "Error while compiling frequencies.\n"); return 1; } displist(clist); tree = mktree(clist); disptree(tree); encode(); /*save(fd2);*/ remlnode(&clist); /*remnode(&tree);*/ close(fd1); return 0; }
void main(void) { Tree *t; File *hello, *goodbye, *world; t = mktree(); hello = fcreate(t->root, "hello", CHDIR|0777); assert(hello != nil); goodbye = fcreate(t->root, "goodbye", CHDIR|0777); assert(goodbye != nil); world = fcreate(hello, "world", 0666); assert(world != nil); world = fcreate(goodbye, "world", 0666); assert(world != nil); fdump(t->root, 0); fremove(world); fdump(t->root, 0); }
int main(int argc, char* argv[]) { // ensure proper usage if (argc != 3) { printf("Usage: %s input output\n", argv[0]); return 1; } // open input Huffile* input = hfopen(argv[1], "r"); if (input == NULL) { printf("Could not open %s for reading.\n", argv[1]); return 1; } // read in header Huffeader header; if (hread(&header, input) == false) { hfclose(input); printf("Could not read header.\n"); return 1; } // check for magic number if (header.magic != MAGIC) { hfclose(input); printf("File was not huffed.\n"); return 1; } // check checksum int checksum = header.checksum; for (int i = 0; i < SYMBOLS; i++) { checksum -= header.frequencies[i]; } if (checksum != 0) { hfclose(input); printf("File was not huffed.\n"); return 1; } int index; Forest* f1 = mkforest(); for(index = 0; index<SYMBOLS;index++) { if(header.frequencies[index] != 0) { Tree* t1 = mktree(); t1->symbol = (char)index; t1->frequency = header.frequencies[index]; plant(f1,t1); } } while(true) { Tree* temp1 = pick(f1); Tree* temp2 = pick(f1); if(temp2 != NULL) { Tree* new_tree = mktree(); new_tree->left = temp1; new_tree->right = temp2; new_tree->frequency = temp1->frequency + temp2->frequency; plant(f1,new_tree); } else { plant(f1,temp1); break; } } Tree* huffman_tree = pick(f1); int bit; Tree* temp = huffman_tree; FILE *output = fopen(argv[2],"w"); while ((bit = bread(input)) != EOF) { if(bit == 1) { temp = temp->right; if(temp->right == NULL && temp->left ==NULL) { fprintf(output,"%c",temp->symbol); temp = huffman_tree; } } else { temp = temp->left; if(temp->right == NULL && temp->left ==NULL) { fprintf(output,"%c",temp->symbol); temp = huffman_tree; } } } // close input hfclose(input); return 0; }
//main int main (int argc, char* argv[]) { //keep frequencies in array for (int i = 0; i < SYMBOLS; i++) frequencies[i] = 0; //make sure user inputs two arguments if (argc != 3) { printf("You must enter %s input output\n", argv[0]); return 1; } //open input FILE *fp = fopen(argv[1],"r" ); if (fp == NULL) { printf("Could not open souce file: %s\n", argv[1]); return 1; } //open output Huffile *outfile = hfopen(argv[2], "w"); if (outfile == NULL) { printf("Could not open destination file: %s\n", argv[2]); return 1; } //create a forest Forest *myForest = mkforest(); if (myForest == NULL) { printf("The forest could not be created :(\n"); return 1; } //read each character from source file for (int c = fgetc(fp); c!= EOF; c = fgetc(fp)) { //printf("%c", c); //increment frequencies for char frequencies[c]++; //increment checksum checksum_counter++; } //make and plant trees in sorted order for (int i = 0; i < SYMBOLS; i++) { if (frequencies[i] != 0) { //make the tree Tree *tempTree = mktree(); tempTree->symbol = i; tempTree->frequency = frequencies[i]; tempTree->left = NULL; tempTree->right = NULL; //plant tree in forest if (plant(myForest, tempTree) == false) printf("Could not plant tree %c\n", i); } } //build the huffman tree while (myForest->first->tree->frequency < checksum_counter) { //make the tree Tree *tempTree = mktree(); tempTree->symbol = 0x00; tempTree->left = pick(myForest); tempTree->right = pick(myForest); if (tempTree->right != NULL) tempTree->frequency = tempTree->left->frequency + tempTree->right->frequency; else tempTree->frequency = tempTree->left->frequency; //plant tree in forest if (plant(myForest, tempTree) == false) printf("Could not plant parent tree\n"); } //create encoding of each character for (int i = 0; i < SYMBOLS; i++) { if (frequencies[i] != 0) { //create a temporary array to store the encoded value char temp[256]; for (int j = 0; j < 256; j++) temp[j] = '9'; encode(myForest->first->tree, i, 0, "", &temp[0]); //copy temp to encoded strncpy(encoded[i], temp, strlen(temp)); //printf("temp: %s\n", temp); //printf("encoded: %c, hops: %s ", i, encoded[i]); } } //create huffeader header Huffeader *header = malloc(sizeof(Huffeader)); header->magic = MAGIC; for (int i = 0; i < SYMBOLS; i++) header->frequencies[i] = frequencies[i]; header->checksum = checksum_counter; //write header hwrite(header, outfile); //move to beginning of source file rewind(fp); //keep track of how many bits we use int bit_count = 0; //read file per character, lookup frequency, and write to outfile for (int c = fgetc(fp); c!= EOF; c = fgetc(fp)) { for (int i = 0; i < strlen(encoded[c]); i++) { bit_count++; int bit = 0; //write each bit if (encoded[c][i] == '0') bit = 0; else if (encoded[c][i] == '1') bit = 1; bool write_output = bwrite(bit, outfile); if (write_output == false) printf("Error bwriting: %c\n", encoded[c][i]); //printf("%c bit_count: %d\n", encoded[c][i], bit_count); } //printf("%s", encoded[c]); } //figure out how many bits in second to last byte are used int bits_used = 0; if (bit_count <= 8) bits_used = bit_count; else bits_used = (bit_count % 8); //write trailing bits for (int i = 8 - bits_used; i > 1; i--) { //printf("0\n"); bool write_output = bwrite(0, outfile); if (write_output == false) printf("Error bwriting: %c\n", '0'); } //cleanup outfile->ith = bits_used; free(header); rmforest(myForest); hfclose(outfile); fclose(fp); return 0; }
int main(int argc, char* argv[]) { // ensure proper usage if (argc != 3) { printf("Usage: %s input output\n", argv[0]); return 1; } // open input Huffile* input = hfopen(argv[1], "r"); if (input == NULL) { printf("Could not open %s for reading.\n", argv[1]); return 1; } // open outfile FILE* outfile = fopen(argv[2], "w"); // read in header Huffeader header; if (hread(&header, input) == false) { hfclose(input); printf("Could not read header.\n"); return 1; } // check for magic number if (header.magic != MAGIC) { hfclose(input); printf("File was not huffed.\n"); return 1; } // check checksum int checksum = header.checksum; for (int i = 0; i < SYMBOLS; i++) { checksum -= header.frequencies[i]; } if (checksum != 0) { hfclose(input); printf("File was not huffed.\n"); return 1; } // make forest Forest* forest = mkforest(); // search for symbols that are non-zero frequency for (int i = 0; i < SYMBOLS; i++) { // make and plant the trees in the forest if (header.frequencies[i] >= 1) { Tree* newTree = mktree(); newTree->frequency = header.frequencies[i]; newTree->symbol = i; newTree->left = NULL; newTree->right = NULL; plant(forest, newTree); } } // run loop until there is only one tree left bool done = false; while (!done) { // pick smallest tree Tree* a = pick(forest); // pick second smallest tree Tree* b = pick(forest); // if there is only one tree left in the forest, a is the huffman tree if (b == NULL) { done = true; root = a; } // if two trees were succesfully picked else { // combine the two trees by calling the combine function Tree* combinedTree = combine(a, b); // plant combined tree back in forest plant(forest, combinedTree); } } // write message to file int bit; Tree* cursor = root; while ((bit = bread(input)) != EOF) { // if bit == 0 -> go left if (bit == 0) { cursor = cursor->left; } // if bit == 1 -> go right else if (bit == 1) { cursor = cursor->right; } // when you find a leaf if ((cursor->right == NULL) && (cursor->left == NULL)) { // print the leaf fprintf(outfile, "%c", cursor->symbol); // reset the cursor to root for the next iteration cursor = root; } } // free root rmtree(root); // close forest rmforest(forest); // close input hfclose(input); // close outfile fclose(outfile); // that's all folks! return 0; }
int main() { Tree *myTree = NULL; List *tmpList = NULL; Node *tmp = NULL; char data[] = { 11, 9, 13, 11, 7, 6, 4, 2, 8 }; int testno = 0; int i = 0; int j = 0; code_t result = 0; char *output[3]; output[INORDER] = "11 -> 9 -> 7 -> 6 -> 4 -> 8 -> 11 -> 13 -> NULL"; output[PREORDER] = "4 -> 6 -> 7 -> 8 -> 9 -> 11 -> 11 -> 13 -> NULL"; output[POSTORDER] = "13 -> 11 -> 11 -> 9 -> 8 -> 7 -> 6 -> 4 -> NULL"; fprintf(stdout, "UNIT TEST: tree library traverse_s() function\n"); fprintf(stdout, "=============================================\n"); for (i = 0; i < 2; i++) { for (j = INORDER; j <= POSTORDER + 1; j++) { if (i == 1) mklist(&tmpList); fprintf(stdout, "Test %d: ", testno++); fprintf(stdout, "STACK-BASED "); result = traverse_s(myTree, &tmpList, j); if (j == INORDER) fprintf(stdout, "INORDER "); else if (j == PREORDER) fprintf(stdout, "PREORDER "); else if (j == POSTORDER) fprintf(stdout, "POSTORDER "); else fprintf(stdout, "*INVALID* "); fprintf(stdout, "traversal of NULL tree (run %d) ...\n", i); if (myTree == NULL) { if (tmpList == NULL) fprintf(stdout, " you have: NULL tree and NULL list\n"); else fprintf(stdout, " you have: NULL tree and non-NULL list\n"); } else { if (tmpList == NULL) fprintf(stdout, " you have: non-NULL tree and NULL list\n"); else fprintf(stdout, " you have: non-NULL tree and non-NULL list\n"); } if (i == 0) fprintf(stdout, "should be: NULL tree and NULL list\n\n"); else fprintf(stdout, "should be: NULL tree and non-NULL list\n\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); lscodes(DLT_ERROR | DLT_NULL); fprintf(stdout, "\n"); fflush (stdout); } } myTree = NULL; result = mktree(&myTree, 4); rmlist(&tmpList); for (i = 0; i < 2; i++) { for (j = INORDER; j <= POSTORDER + 1; j++) { if (i == 1) mklist(&tmpList); fprintf(stdout, "Test %d: ", testno++); fprintf(stdout, "STACK-BASED "); result = traverse_s(myTree, &tmpList, j); if (j == INORDER) fprintf(stdout, "INORDER "); else if (j == PREORDER) fprintf(stdout, "PREORDER "); else if (j == POSTORDER) fprintf(stdout, "POSTORDER "); else fprintf(stdout, "*INVALID* "); fprintf(stdout, "traversal of empty tree (run %d)...\n", i); fprintf(stdout, " you have: "); display(tmpList, 0); if (j == POSTORDER + 1) fprintf(stdout, "should be: (NULL)\n\n"); else fprintf(stdout, "should be: -> NULL\n\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); if ((i == 0) && (j != POSTORDER+1)) lscodes(DLT_EMPTY|DLT_SUCCESS|DLL_EMPTY|DLL_SUCCESS); else if (j == POSTORDER+1) lscodes(DLT_ERROR|DLT_EMPTY); else lscodes(DLT_EMPTY|DLT_ERROR|DLL_ERROR|DLL_ALREADY_ALLOC); fprintf(stdout, "\n"); fflush (stdout); rmlist(&tmpList); } } // result = mktree(&myTree, 4); for (j = 0; j < 9; j++) { tmp = NULL; mknode(&tmp, data[j]); result = addnode(&myTree, tmp); } for (i = 0; i < 2; i++) { for (j = INORDER; j <= POSTORDER + 1; j++) { if (i == 1) mklist(&tmpList); fprintf(stdout, "Test %d: ", testno++); fprintf(stdout, "STACK-BASED "); result = traverse_s(myTree, &tmpList, j); if (j == INORDER) fprintf(stdout, "INORDER "); else if (j == PREORDER) fprintf(stdout, "PREORDER "); else if (j == POSTORDER) fprintf(stdout, "POSTORDER "); else fprintf(stdout, "*INVALID* "); fprintf(stdout, "traversal of populated tree (run %d) ...\n", i); if (i == 0) { fprintf(stdout, " you have: "); display(tmpList, 0); if (j != POSTORDER + 1) fprintf(stdout, "should be: %s\n\n", output[j]); else fprintf(stdout, "should be: (NULL)\n\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); } fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); if ((i == 0) && (j != POSTORDER + 1)) lscodes(DLT_SUCCESS); else if (j == POSTORDER+1) lscodes(DLT_ERROR); else lscodes(DLT_ERROR|DLL_ALREADY_ALLOC|DLL_ERROR); fprintf(stdout, "\n"); fflush (stdout); rmlist(&tmpList); tmpList = NULL; } } return(0); }
int main(int argc, char* argv[]) { // ensure proper usage if (argc != 3) { printf("Usage: %s input\n", argv[0]); return 1; } // open input Huffile* input = hfopen(argv[1], "r"); if (input == NULL) { printf("Could not open %s for reading.\n", argv[1]); return 1; } // open outfile FILE* outfile = fopen(argv[2], "w"); if (outfile == NULL) { fclose(outfile); fprintf(stderr, "Could not create outfile.\n"); return 2; } // read in header Huffeader header; if (hread(&header, input) == false) { hfclose(input); printf("Could not read header.\n"); return 1; } // check for magic number if (header.magic != MAGIC) { hfclose(input); printf("File was not huffed.\n"); return 1; } // check checksum int checksum = header.checksum; for (int i = 0; i < SYMBOLS; i++) { checksum -= header.frequencies[i]; } if (checksum != 0) { hfclose(input); printf("File was not huffed.\n"); return 1; } // make forest Forest* forest = mkforest(); // read in huffeader frequencies for (int i = 0; i < SYMBOLS; i++) { // ignore 0 frequencies if (header.frequencies[i] > 0) { // make new tree for every non-zero frequency occurance Tree* new_tree = mktree(); new_tree->symbol = i; new_tree->frequency = header.frequencies[i]; new_tree->left = NULL; new_tree->right = NULL; // plant every non-zero frequency tree in forest plant(forest, new_tree); } } // run loop until there is only one tree left bool done = false; while (!done) { // pick smallest tree from forest Tree* a = pick(forest); // pick second smallest tree from forest Tree* b = pick(forest); // if there is no second tree in forest... if (b == NULL) { // break loop done = true; // set root to tree 'a' (last remaining) tree root = a; } // else there are at least two remaining trees in the forest else { // combine the two trees into a parent tree Tree* parent_tree = combine(a, b); // plant combined tree in forest plant(forest, parent_tree); } } // write message to outfile int bit; Tree* ptr_location = root; while ((bit = bread(input)) != EOF) { // if bit is 0, go left, else go right if (bit == 0) ptr_location = ptr_location->left; // if bit is 1, go right if (bit == 1) ptr_location = ptr_location->right; // leaf is found when both branchs are NULL if ((ptr_location->left == NULL) && (ptr_location->right == NULL)) { // write leaf's symbol to outfile fprintf(outfile, "%c", ptr_location->symbol); // reset pointer location to root for next iteration of tree ptr_location = root; } } // free root rmtree(root); // close forest rmforest(forest); // close input & outfile hfclose(input); fclose(outfile); // that's all folks! return 0; }
bool createForest(char *path) { // create empty forest f = mkforest(); // open input Huffile* input = hfopen(path, "r"); if (input == NULL) { printf("Could not open %s for reading.\n", path); return 0; } // read in header Huffeader header; if (hread(&header, input) == false) { hfclose(input); printf("Could not read header.\n"); return 0; } // read symbols and freq from header, create and plant trees in forest for (int i = 0; i < SYMBOLS; i++) { if ( header.frequencies[i] != 0 ) { Tree *t = mktree(); t->symbol = i; t->frequency = header.frequencies[i]; plant(f, t); count++; } } // join trees as siblings for (int i = 0; i < count - 1; i++) { Tree *t = mktree(); t->left = pick(f); t->right = pick(f); t->frequency = t->left->frequency + t->right->frequency; plant(f, t); } // close input hfclose(input); return true; }
int nl = t->left->frequency; int nrc = t->right->symbol; int nr = t->right->frequency; int ml = t->left->left->frequency; int mr = t->left->right->frequency; int mlc = t->left->left->symbol; int mrc = t->left->right->symbol; printf("%i: left %i right %i, %c\n", n, nl, nr, nrc); printf("%i: left %i, %i right %i, %c\n", nl, ml, mlc, mr, mrc); printf( "%i %i\n", isLeaf(t->left), isLeaf(t->left->left) ); // create trees Tree *h = mktree(); h->symbol = 'H'; h->frequency = 2; Tree *t = mktree(); t->symbol = 'T'; t->frequency = 1; Tree *end = mktree(); end->symbol = '\n'; end->frequency = 1; //plants trees in forest plant(f, h); plant(f, t); plant(f, end);
int main() { Tree *myTree = NULL; Tree *myTree2 = NULL; List *tmpList = NULL; Node *tmp = NULL; char data[] = { 11, 9, 13, 11, 7, 6, 4, 2, 8 }; int testno = 0; // int mode = 0; int variation = 0; int k = 0; code_t result = 0; fprintf(stdout, "UNIT TEST: tree library cptree_r() function\n"); fprintf(stdout, "===========================================\n"); ////////////////////////////////////////////////////////////////// // // number of test cycles // for (variation = 0; variation < 6; variation++) { ////////////////////////////////////////////////////////////// // // cycle through our three implementations // // for (mode = RECURSIVE; mode <= ITERATIVE; mode++) // { ////////////////////////////////////////////////////////// // // after the NULL run, be sure to allocate a tree // if (variation == 1) { mktree(&myTree, 0); tmp = myTree -> root; } ////////////////////////////////////////////////////////// // // test logic for trees with only one (just a root) node // if (variation == 2) { if (myTree -> root == NULL) { tmp = NULL; mknode (&tmp, data[0]); addnode(&myTree, tmp); } } ////////////////////////////////////////////////////////// // // populate our tree for the remaining half of tests // else if (variation == 3) { for (k = 1; k < 9; k++) { tmp = NULL; mknode (&tmp, data[k]); addnode(&myTree, tmp); } } ////////////////////////////////////////////////////////// // // identify test run and current mode // fprintf(stdout, "Test %d: ", testno++); // if (mode == RECURSIVE) fprintf(stdout, "RECURSIVE "); // else if (mode == STACK_BASED) // fprintf(stdout, "STACK_BASED "); // else if (mode == ITERATIVE) // fprintf(stdout, "ITERATIVE "); ////////////////////////////////////////////////////////// // // in variation 4 we balance the tree before copying // // if (variation == 4) // myTree = balance(myTree); ////////////////////////////////////////////////////////// // // in variation 5 we grab a node out of the tree before // copying // if (variation == 5) //else if (variation == 5) { tmp = myTree -> root -> prior; grabnode_r(&myTree, &tmp); } ////////////////////////////////////////////////////////// // // set the tree's mode (recursive, stack-based, iterative) // // myTree = set_mode(myTree, mode); ////////////////////////////////////////////////////////// // // copy the tree // myTree2 = NULL; // bad result = cptree_r(myTree, &myTree2); ////////////////////////////////////////////////////////// // // display a description of the current tree state // if (variation == 0) fprintf(stdout, "copying NULL tree ...\n"); else if (variation == 1) fprintf(stdout, "copying empty tree ...\n"); else if (variation == 2) fprintf(stdout, "copying tree with one node ...\n"); else if (variation == 4) fprintf(stdout, "copying tree ...\n"); // fprintf(stdout, "copying balanced tree ...\n"); else if (variation == 5) fprintf(stdout, "copying tree we've removed from ...\n"); else fprintf(stdout, "copying populated tree ...\n"); ////////////////////////////////////////////////////////// // // display the results // fprintf(stdout, " you have: "); traverse_r(myTree2, &tmpList, INORDER); display(tmpList, 0); rmlist(&tmpList); fprintf(stdout, "should be: "); traverse_r(myTree, &tmpList, INORDER); display(tmpList, 0); rmlist(&tmpList); fprintf(stdout, "\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); if (variation == 0) lscodes(DLT_ERROR|DLT_NULL); else if (variation == 1) lscodes(DLT_SUCCESS|DLT_EMPTY|DLL_EMPTY); else if (variation >= 2) lscodes(DLT_SUCCESS); fprintf(stdout, "\n"); fflush (stdout); // } } result = result + 1; return(0); }
int main() { Tree *myTree = NULL; Node *tmp = NULL; char data[] = { 11, 9, 13, 11, 7, 6, 4, 2, 8 }; int testno = 0; int i = 0; int nothing = 0; code_t result = 0; fprintf(stdout, "UNIT TEST: tree library addnode() function\n"); fprintf(stdout, "==========================================\n"); fprintf(stdout, "Test %d: Adding %hhd to NULL tree ...\n", testno++, data[i]); tmp = NULL; mknode(&tmp, data[i]); result = addnode(&myTree, tmp); if (myTree == NULL) fprintf(stdout, " you have: NULL\n"); else fprintf(stdout, " you have: something\n"); fprintf(stdout, "should be: NULL\n\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); lscodes(DLT_NULL | DLT_ERROR); fflush (stdout); mktree(&myTree, 4); fprintf(stdout, "Test %d: Adding %hhd to empty tree ...\n", testno++, data[i]); tmp = NULL; mknode(&tmp, data[i]); result = addnode(&myTree, tmp); if (myTree == NULL) fprintf(stdout, " you have: NULL\n"); else fprintf(stdout, " you have: something (success)\n"); fprintf(stdout, "should be: something (success)\n\n"); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); lscodes(DLT_SUCCESS); fflush (stdout); fprintf(stdout, "Test %d: Checking root ...\n", testno++); if (myTree -> root == NULL) fprintf(stdout, " you have: NULL\n"); else if (myTree -> root ->VALUE == data[i]) fprintf(stdout, " you have: %hhd\n", data[i]); else fprintf(stdout, " you have: %hhd\n", myTree -> root -> VALUE); fprintf(stdout, "should be: %hhd\n\n", data[i]); fflush (stdout); for (i = 1; i < 9; i++) { // adding a new node to the tree fprintf(stdout, "Test %d: Adding %hhd to tree ...\n", testno++, data[i]); tmp = NULL; mknode(&tmp, data[i]); result = addnode(&myTree, tmp); switch (i) { case 1: tmp = myTree -> root -> prior; nothing = 0; break; case 2: tmp = myTree -> root -> after; nothing = 0; break; case 3: tmp = myTree -> root -> prior -> after; nothing = 0; break; case 4: tmp = myTree -> root -> prior -> prior; nothing = 0; break; case 5: tmp = myTree -> root -> prior -> prior -> prior; nothing = 0; break; case 6: tmp = myTree -> root -> prior -> prior -> prior -> prior; nothing = 0; break; case 7: tmp = myTree -> root -> prior -> prior -> prior -> prior -> prior; nothing = 1; break; case 8: tmp = myTree -> root -> prior -> prior -> after; nothing = 0; break; } if (myTree == NULL) fprintf(stdout, " you have: NULL tree\n"); else if (myTree -> root == NULL) fprintf(stdout, " you have: empty tree\n"); else if ((tmp == NULL) && (nothing == 1)) { fprintf(stdout, " you have: exceeded tree height\n"); } else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); if (nothing == 1) fprintf(stdout, "should be: exceeded tree height\n\n"); else fprintf(stdout, "should be: %hhd\n\n", data[i]); fflush (stdout); fprintf(stdout, "Test %d: Checking results ...\n", testno++); fprintf(stdout, " you have: "); lscodes(result); fprintf(stdout, "should be: "); if (nothing == 1) lscodes(DLT_MAX | DLT_ERROR); else lscodes(DLT_SUCCESS); fflush (stdout); } tmp = myTree -> root; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (myTree -> root ->VALUE == data[0]) fprintf(stdout, " you have: OK (%hhd)\n", data[0]); else fprintf(stdout, " you have: %hhd\n", myTree -> root -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[0]); fflush (stdout); tmp = myTree -> root -> after; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[2]) fprintf(stdout, " you have: OK (%hhd)\n", data[2]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[2]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after == NULL) && (tmp -> prior == NULL)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[1]) fprintf(stdout, " you have: OK (%hhd)\n", data[1]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[1]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after->VALUE == 11) && (tmp -> prior->VALUE == 7)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior -> after; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[3]) fprintf(stdout, " you have: OK (%hhd)\n", data[3]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[3]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after == NULL) && (tmp -> prior == NULL)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior -> prior; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[4]) fprintf(stdout, " you have: OK (%hhd)\n", data[4]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[4]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after->VALUE == 8) && (tmp -> prior->VALUE == 6)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior -> prior -> prior; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[5]) fprintf(stdout, " you have: OK (%hhd)\n", data[5]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[5]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after == NULL) && (tmp -> prior->VALUE == 4)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior -> prior -> prior -> prior; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[6]) fprintf(stdout, " you have: OK (%hhd)\n", data[6]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[6]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after == NULL) && (tmp -> prior == NULL)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); tmp = myTree -> root -> prior -> prior -> after; fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if (tmp -> VALUE == data[8]) fprintf(stdout, " you have: OK (%hhd)\n", data[8]); else fprintf(stdout, " you have: %hhd\n", tmp -> VALUE); fprintf(stdout, "should be: OK (%hhd)\n\n", data[8]); fflush (stdout); fprintf(stdout, "Test %d: Checking tree integrity ...\n", testno++); if ((tmp -> after == NULL) && (tmp -> prior == NULL)) { fprintf(stdout, " you have: all connections OK\n"); } else fprintf(stdout, " you have: invalid connection\n"); fprintf(stdout, "should be: all connections OK\n\n"); fflush (stdout); return(0); }
tn * mktree(int n) { tn * result = (tn *)GC_MALLOC(sizeof(tn)); collectable_count++; # if defined(MACOS) /* get around static data limitations. */ if (!live_indicators) live_indicators = (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word)); if (!live_indicators) { GC_printf("Out of memory\n"); exit(1); } # endif if (n == 0) return(0); if (result == 0) { GC_printf("Out of memory\n"); exit(1); } result -> level = n; result -> lchild = mktree(n-1); result -> rchild = mktree(n-1); if (counter++ % 17 == 0 && n >= 2) { tn * tmp = result -> lchild -> rchild; result -> lchild -> rchild = result -> rchild -> lchild; result -> rchild -> lchild = tmp; } if (counter++ % 119 == 0) { int my_index; { # ifdef PCR PCR_ThCrSec_EnterSys(); # endif # if defined(GC_PTHREADS) static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&incr_lock); # elif defined(GC_WIN32_THREADS) EnterCriticalSection(&incr_cs); # endif /* Losing a count here causes erroneous report of failure. */ finalizable_count++; my_index = live_indicators_count++; # ifdef PCR PCR_ThCrSec_ExitSys(); # endif # if defined(GC_PTHREADS) pthread_mutex_unlock(&incr_lock); # elif defined(GC_WIN32_THREADS) LeaveCriticalSection(&incr_cs); # endif } GC_REGISTER_FINALIZER((void *)result, finalizer, (void *)(GC_word)n, (GC_finalization_proc *)0, (void * *)0); if (my_index >= MAX_FINALIZED) { GC_printf("live_indicators overflowed\n"); FAIL; } live_indicators[my_index] = 13; if (GC_GENERAL_REGISTER_DISAPPEARING_LINK( (void * *)(&(live_indicators[my_index])), (void *)result) != 0) { GC_printf("GC_general_register_disappearing_link failed\n"); FAIL; } if (GC_unregister_disappearing_link( (void * *) (&(live_indicators[my_index]))) == 0) { GC_printf("GC_unregister_disappearing_link failed\n"); FAIL; } if (GC_GENERAL_REGISTER_DISAPPEARING_LINK( (void * *)(&(live_indicators[my_index])), (void *)result) != 0) { GC_printf("GC_general_register_disappearing_link failed 2\n"); FAIL; } GC_reachable_here(result); } return(result); }