void Terrain::ForceMerge(Node* T, Node* End) { if(T != TriangleTree.root) { if(TriangleTree.isInternal(T->LeftChild)) { ForceMerge(T->LeftChild, End); } else if(TriangleTree.isInternal(T->RightChild)) { ForceMerge(T->RightChild, End); } else if(TriangleTree.isExternal(T)) { ForceMerge(T->Parent, End); } else if( TriangleTree.isExternal(T->LeftChild) && TriangleTree.isExternal(T->RightChild) ) // Left and Right children of T are external { MergeNode(T); if(T != End) { ForceMerge(T->Parent, End); } } } }
BN* MakeTree(STRING* Leaf,int NLeaf){ if(!NLeaf) return NULL; random_quicksort(Leaf,0,NLeaf-1); DEPTH=strlen(Leaf[0]); BS* bs = (BS*)malloc(sizeof(BS)*NLeaf); int i; for(i=0;i!=NLeaf;i++){ bs[i].curr=CreateNode(Leaf[i]); if(i!=NLeaf-1)bs[i].next=&bs[i+1]; else bs[i].next=NULL; if(DEPTH<(int) strlen(Leaf[i])) DEPTH=(int) strlen(Leaf[i]); } IS* is=(IS*)malloc(sizeof(IS)*NLeaf); for(i=0;i<NLeaf;i++){ int idx=DEPTH-strlen(Leaf[i]); is[i].id=B2D(Leaf[i],(int) strlen(Leaf[i]))<<idx; if(i!=NLeaf-1)is[i].next=&is[i+1]; else is[i].next=NULL; } int stack_size=NLeaf-1; IS* iseek; BS* bseek; /*bseek=bs; iseek=is; while(bseek){ printf("tag:%s id:%d TreeInfo:%s\n",bseek->curr->tag,iseek->id,bseek->curr->TreeInfo); bseek=bseek->next; iseek=iseek->next; }*/ while(stack_size>0){ bseek=bs; iseek=is; int i1,i2; BN* nbn; int tail_branch=0; while(iseek->next){ i1=(int) ((unsigned) iseek->id)>>1; i2=(int) ((unsigned) iseek->next->id)>>1; if(i1==i2){ nbn=MergeNode(bseek->curr,bseek->next->curr); iseek->id=(int) ((unsigned) iseek->id)>>1; bseek->curr=nbn; bseek->next=bseek->next->next; iseek->next=iseek->next->next; if(bseek->next){ bseek=bseek->next; iseek=iseek->next; stack_size--; } else{ stack_size--; tail_branch=1; break; } } else{ if((int) strlen(bseek->curr->tag)==DEPTH)UpdateNode(bseek->curr,1); else UpdateNode(bseek->curr,0); iseek->id=(int) ((unsigned) iseek->id)>>1; if(bseek->next){ bseek=bseek->next; iseek=iseek->next; } else break; } }