int isSimilar(struct node *p1, struct node *p2) { if(p1==NULL && p2==NULL) return 1; if(p1!=NULL && p2!=NULL) if(isSimilar(p1->lchild, p2->lchild) && isSimilar(p1->rchild, p2->rchild)) return 1; return 0; }
bool isSimilar(node *r1, node *r2){ if(r1 == NULL && r2 == NULL) return true; if(r1 == NULL || r2 == NULL) return false; if(r1->data == r2->data && isSimilar(r1->left, r2->left) && isSimilar(r1->right, r2->right)) return true; return false; }
main() { struct node *root1=NULL, *root2=NULL,*root3=NULL, *root4=NULL; root1 = insert(root1, 10); root1 = insert(root1, 2); root1 = insert(root1, 9); root1 = insert(root1, 5); root2 = insert(root2, 6); root2 = insert(root2, 3); root2 = insert(root2, 8); root2 = insert(root2, 7); root2 = insert(root2, 1); root2 = insert(root2, 4); root3 = insert(root3, 16); root3 = insert(root3, 13); root3 = insert(root3, 18); root3 = insert(root3, 17); root3 = insert(root3, 11); root3 = insert(root3, 14); if(isSimilar(root1,root2)) printf("Tree 1 and 2 are Similar\n"); else printf("Tree 1 and 2 are Not Similar\n"); if(isIdentical(root1,root2)) printf("Tree 1 and 2 are Identical\n"); else printf("Tree 1 and 2 are Not Identical\n"); if(isSimilar(root2,root3)) printf("Tree 2 and 3 are Similar\n"); else printf("Tree 2 and 3 are Not Similar\n"); if(isIdentical(root2,root3)) printf("Tree 2 and 3 are Identical\n"); else printf("Tree 2 and 3 are Not Identical\n"); root4=copy_tree(root3); if(isSimilar(root3,root4)) printf("Tree 3 and 4 are Similar\n"); else printf("Tree 3 and 4 are Not Similar\n"); if(isIdentical(root3,root4)) printf("Tree 3 and 4 are Identical\n"); else printf("Tree 3 and 4 are Not Identical\n"); }/*End of main( )*/
CSPSource* CSPSource::intersect(CSPSource* other) const { if (!isSimilar(other)) return nullptr; String scheme = other->schemeMatches(m_scheme) ? m_scheme : other->m_scheme; if (isSchemeOnly() || other->isSchemeOnly()) { const CSPSource* stricter = isSchemeOnly() ? other : this; return new CSPSource(m_policy, scheme, stricter->m_host, stricter->m_port, stricter->m_path, stricter->m_hostWildcard, stricter->m_portWildcard); } String host = m_hostWildcard == NoWildcard ? m_host : other->m_host; // Since sources are similar and paths match, pick the longer one. String path = m_path.length() > other->m_path.length() ? m_path : other->m_path; // Choose this port if the other port is empty, has wildcard or is a port for // a less secure scheme such as "http" whereas scheme of this is "https", in // which case the lengths would differ. int port = (other->m_portWildcard == HasWildcard || !other->m_port || m_scheme.length() > other->m_scheme.length()) ? m_port : other->m_port; WildcardDisposition hostWildcard = (m_hostWildcard == HasWildcard) ? other->m_hostWildcard : m_hostWildcard; WildcardDisposition portWildcard = (m_portWildcard == HasWildcard) ? other->m_portWildcard : m_portWildcard; return new CSPSource(m_policy, scheme, host, port, path, hostWildcard, portWildcard); }
bool isSubTree(node *T, node *S){ if(T == NULL) return false; if(T->data == S->data && isSimilar(T, S)) return true; else{ return isSubTree(T->left, S) || isSubTree(T->right, S); } }
int main(){ struct node *root = NULL; add(&root,4); add(&root,1); add(&root,3); add(&root,7); add(&root,9); add(&root,5); add(&root,8); add(&root,2); add(&root,6); struct node *b = NULL; add(&b,1); add(&b,2); add(&b,3); add(&b,4); add(&b,5); add(&b,6); add(&b,7); add(&b,8); add(&b,9); printf("\n\n Compare trees for similarity: %s \n\n", isSimilar(root,b) ? "true": "false"); printf("\n\ninorder:"); inorder(root); // printf("\nPREORDER: \n"); // preorderStack(root); printf("\nPOSTORDER: \n"); postOrderStack(root); struct node *lca_6_8 = commonAncestor(root,find(root,8),find(root,6)); printf("\n\nleast common ancestor: 6,8: %d\n", lca_6_8->data); struct node *lca_2_8 = commonAncestor(root,find(root,8),find(root,2)); printf("\n\nleast common ancestor: 2,8: %d\n", lca_2_8->data); struct node *lca_6_7 = commonAncestor(root,find(root,7),find(root,6)); printf("\n\nleast common ancestor: 6,7: %d\n", lca_6_7->data); int distance_6_8 = distance(root,find(root,8),find(root,6)); printf("\n\ndistance: 6,8: %d\n", distance_6_8); int distance_2_8 = distance(root,find(root,8),find(root,2)); printf("\n\ndistance: 2,8: %d\n", distance_2_8); int distance_6_7 = distance(root,find(root,7),find(root,6)); printf("\n\ndistance: 6,7: %d\n", distance_6_7); printf("\n\nzigazag:"); zigzag(root); printf("\n\n"); }
void LabelToolPlugin::insertSelectedPoint(QPointF point) { if(selected_points_.empty() || !isSimilar(selected_points_[0], point) ) { selected_points_.push_back(point); drawQtPolygon(false); return; } else { drawQtPolygon(true); if(!img_with_polygons_.empty()) setNewPolygonEdges(); selected_points_.clear(); return; } }
static bool HHVM_METHOD(ImagickPixel, isSimilar, const Variant& color, double fuzz) { raiseDeprecated(s_ImagickPixel.c_str(), "isSimilar", s_ImagickPixel.c_str(), "isPixelSimilar"); return isSimilar(this_, color, fuzz, false); }
static bool HHVM_METHOD(ImagickPixel, isPixelSimilar, const Variant& color, double fuzz) { return isSimilar(this_, color, fuzz, true); }
void quater::getRotation(const char* aChannell, double *aValue, bool bRightToLeft) const { //!< to euler angle. aChannel="YXZ" or something like that. vector3 euler; char aChannel[3]; int numChannels=strlen(aChannell); if(bRightToLeft) { for(int i=0; i<numChannels; i++) { aChannel[i]=aChannell[i]; } } else { for(int i=0; i<numChannels; i++) { aChannel[i]=aChannell[numChannels-i-1]; } } for(int i=numChannels; i<3; i++) { aChannel[i]='X'; for(int k=0; k<2; k++) { for(int j=0; j<i; j++) if(aChannel[i]==aChannel[j]) aChannel[i]++; } } if(strncmp(aChannel, "XYZ", 3)==0 ) { // R=RZ*RY*RX euler=Quater2EulerAngleZYX(*this, numChannels); } else { matrix4 matT, matInvT; matT.setValue(0,0,0, 0,0,0, 0,0,0); for(int i=0; i<3; i++) { switch(aChannel[i]) { case 'X': matT.m[i][0]=1.0; break; case 'Y': matT.m[i][1]=1.0; break; case 'Z': matT.m[i][2]=1.0; break; default: assert(0); continue; } } matInvT.transpose(matT); matrix4 matRot2; matRot2.mult(matT, *this); matRot2.mult(matRot2, matInvT); quater q; q.setRotation(matRot2); euler=Quater2EulerAngleZYX(q, numChannels); //printf("det %f\n", determinants(matT)); euler*=determinants(matT); } if(bRightToLeft) { aValue[0]=euler.x; aValue[1]=euler.y; aValue[2]=euler.z; } else { if(numChannels==3) { aValue[0]=euler.z; aValue[1]=euler.y; aValue[2]=euler.x; } else if(numChannels==2) { ASSERT(isSimilar(euler.z,0)); aValue[0]=euler.y; aValue[1]=euler.x; aValue[2]=0.0; } else if(numChannels==1) { ASSERT(isSimilar(euler.z,0)); // ASSERT(isSimilar(euler.y,0)); aValue[0]=euler.x; aValue[1]=0.0; aValue[2]=0.0; } } #ifdef _DEBUG quater qtest; qtest.setRotation(aChannell, aValue, bRightToLeft); qtest.align(*this); //ASSERT(isSimilar(qtest%*this, 1.0)); #endif // printf("get %f %f %f\n", aValue[0], aValue[1], aValue[2]); // testing codes /* quater q(0.813, -0.368, 0.228, 0.387); q.normalize(); quater qtest; double aValue[3]; q.getRotation("YZX", aValue, true); qtest.setRotation("YZX", aValue, true); printf("error %f %s\n", TO_DEGREE(qtest.distance(q)), ((quater&)q).output().ptr()); qtest.getRotation("YZX", aValue, true); q.setRotation("YZX", aValue, true); printf("error %f %s\n", TO_DEGREE(qtest.distance(q)), ((quater&)q).output().ptr()); */ }