int hasSubTree(struct BTreeNode* root1,struct BTreeNode* root2) { int has=0; if(root1&&root2) { if(root1->val==root2->val) has=hasSameSubTree(root1,root2); if(!has) hasSubTree(root1->left,root2); if(!has) hasSubTree(root1->right,root2); } return has; }
int main(int argc,char* argv[]) { Tree T; T = Insert(5,T); T = Insert(3,T); T = Insert(2,T); T = Insert(4,T); T = Insert(6,T); T = Insert(7,T); T = Insert(1,T); preorder(T); printf("\n"); printf("level:%d\n",level(T)); printf("findMaxHeight = %d\n",findMaxHeight(T)); printf("Max = %d\n",FindMax(T)); printf("Minimum = %d\n",FindMin(T)); printf("PreOrder:\n"); preOrder(T); printf("\n"); printf("InOrder:\n"); inOrder(T); printf("\n"); printf("PostOrder:\n"); postOrder(T); printf("\n"); printf("find the lowest ancestor between 1 and 7:\n"); printf("%d\n",findLowestAncestor(T,1,7)); printf("hasSubTree:%d\n",hasSubTree(T,T)); mirrorReverse(T); preOrder(T); printf("\n"); return 0; }
int main(void) { struct BTreeNode* root1=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); root1->val=8; struct BTreeNode* node1=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node1->val=8; struct BTreeNode* node2=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node2->val=7; struct BTreeNode* node3=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node3->val=9; struct BTreeNode* node4=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node4->val=2; struct BTreeNode* node5=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node5->val=4; struct BTreeNode* node6=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); node6->val=7; root1->left=node1; root1->right=node2; node1->left=node3; node1->right=node4; node2->left=NULL; node2->right=NULL; node3->left=NULL; node3->right=NULL; node4->left=node5; node4->right=node6; node5->left=NULL; node5->right=NULL; node6->left=NULL; node6->right=NULL; struct BTreeNode* root2=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); struct BTreeNode* node2_=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); struct BTreeNode* node3_=(struct BTreeNode*)malloc(sizeof(struct BTreeNode)); root2->val=8; root2->left=node2_; root2->right=node3_; node2_->val=9; node2_->left=NULL; node2_->right=NULL; node3_->val=2; node3_->left=NULL; node3_->right=NULL; printf("preorder of root1:"); preorder(root1); printf("\n"); printf("preorder of root2:"); preorder(root2); printf("\n"); char *result=hasSubTree(root1,root2)==YES ? "yes":"no"; printf("has or not:%s\n",result); return 0; }