// checking duplicate trees int sametree(struct node* tree1 , struct node* tree2) { if (tree1 == NULL && tree2 ==NULL) return 1; else if ( tree1 != NULL && tree2 != NULL ) { if ( tree1->data == tree2->data && sametree(tree1->left,tree2->left) && sametree(tree1->right,tree2->right)) return 1; } else return 0; }
int sametree(exp_tree_t *a, exp_tree_t *b) { int i; /* compare child counts and head-types ... */ if (a->child_count != b->child_count) return 0; if (a->head_type != b->head_type) return 0; if (a->tok && b->tok) { /* extract token strings */ strncpy(buf_a, a->tok->start, a->tok->len); buf_a[a->tok->len] = 0; strncpy(buf_b, b->tok->start, b->tok->len); buf_b[b->tok->len] = 0; /* compare 'em */ if (strcmp(buf_a, buf_b)) return 0; } /* recurse onto children */ for (i = 0; i < a->child_count; ++i) if (!sametree(a->child[i], b->child[i])) return 0; /* none of all these tests failed ? * the trees are the same, then ! */ return 1; }
int main() { struct node *tree1,*tree2; tree1 = newnode(1); tree1->left = newnode(2); tree1->right = newnode(3); tree1->right->left = newnode(4); tree2 = newnode(1); tree2->left = newnode(2); tree2->right = newnode(3); tree2->right->left = newnode(4); int check = sametree(tree1,tree2); if (check == 1) { printf("Both trees are same"); } else { printf("Both trees are unique"); } return 0; }
struct match_t *reduce_matches(struct sorthash_t *obarray, int *hashcountp) /* assemble list of duplicated hashes */ { unsigned int nonuniques, nreduced, progress, hashcount = *hashcountp; struct sorthash_t *mp, *np; struct match_t *reduced; if (debug) dump_array("Chunk list before reduction.\n",obarray, hashcount); if (debug) dump_array("Chunk list after reduction.\n",obarray, hashcount); /* build list of hashes with more than one range associated */ nonuniques = progress = 0; if (verbose) fprintf(stderr, "%% Extracting duplicates... "); nreduced = 10000; reduced = (struct match_t *)malloc(sizeof(struct match_t) * nreduced); for (np = obarray; np < obarray + hashcount; np = mp) { int i, heterogenous, nmatches; if (verbose && !debug && progress++ % 10000 == 0) fprintf(stderr, "\b\b\b%02.0f%%", progress / (hashcount * 0.01)); /* count the number of hash matches */ nmatches = 1; for (mp = np+1; mp < obarray + hashcount; mp++) if (SORTHASHCMP(np, mp)) break; else nmatches++; /* if all these matches are within the same tree, toss them */ heterogenous = 0; for (i = 0; i < nmatches; i++) if (!sametree(np[i].file->name, np[(i+1) % nmatches].file->name)) heterogenous++; if (!heterogenous) { np[i].hash.flags = INTERNAL_FLAG; /* used only in debug code */ continue; } if (debug) { printf("*** %ld has %d in its clique\n", np-obarray, nmatches); for (i = 0; i < nmatches; i++) printf("%ld: %s:%d:%d\n", np-obarray+i, np[i].file->name, np[i].hash.start, np[i].hash.end); } /* passed all tests, keep this set of ranges */ if (nonuniques >= nreduced) { nreduced *= 2; reduced = (struct match_t *)realloc(reduced, sizeof(struct match_t)*nreduced); } /* * Point into the existing hash array rather than allocating * new storage. This means our working set won't get any * smaller, but it avoids the time overhead of doing a bunch * of malloc and free calls. */ reduced[nonuniques].matches = np; reduced[nonuniques].nmatches = nmatches; nonuniques++; } if (verbose) fprintf(stderr, "\b\b\b100%% done.\n"); *hashcountp = nonuniques; return reduced; }