/* Remove overlapping and repeated subtree entries from the list of subtrees. * If sscope is not 2 (sub), only remove repeated entries. */ static void remove_overlapping_subtrees(char **list, int *subtcount, int sscope) { size_t ilen, jlen; int i, j; int count = *subtcount; for (i = 0; i < count && list[i] != NULL; i++) { ilen = strlen(list[i]); for (j = i + 1; j < count && list[j] != NULL; j++) { jlen = strlen(list[j]); /* Remove list[j] if it is identical to list[i] or a subtree of it. * Remove list[i] if it is a subtree of list[j]. */ if ((ilen == jlen && strcasecmp(list[j], list[i]) == 0) || (sscope == 2 && is_subtree(list[j], jlen, list[i], ilen))) { free(list[j]); list[j--] = list[count - 1]; list[--count] = NULL; } else if (sscope == 2 && is_subtree(list[i], ilen, list[j], jlen)) { free(list[i]); list[i--] = list[count - 1]; list[--count] = NULL; break; } } } *subtcount = count; }
bool is_subtree(TreeNode* t1, TreeNode* t2) { return is_subtree_at(t1, t2) || (t1 != NULL && (is_subtree(t1->left, t2) || is_subtree(t1->right, t2))); }
bool is_subtree(struct node *T,struct node *S) { if(S==NULL) return 1; if(T==NULL) return 0; if (is_identical(T,S)) { return 1; } if(is_subtree(T->left,S->left)||is_subtree(T->right,S->right)) { printf("sub tree"); return 1; } else printf("not found"); }
int main() { struct node *T = newnode(26); T->right = newnode(3); T->right->right = newnode(3); T->left = newnode(10); T->left->left = newnode(4); T->left->left->right = newnode(30); T->left->right = newnode(6); struct node *S = newnode(610); S->right = newnode(68); S->left = newnode(41); S->left->right = newnode(360); if(is_subtree(T,S)==1) printf("sub tree found"); if(is_subtree(T,S)==0) printf("sub tree not found"); return 0; }