Esempio n. 1
0
void CObAvlTree::SingleLeftRotation( CNode* pNode )
{
	ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));

	CNode* pRChild = pNode->pRChild;
	ASSERT(AfxIsValidAddress(pRChild, sizeof(CNode)));

	CNode** ppNode = NULL;
	if( pNode==m_pNodeRoot )ppNode = &m_pNodeRoot;
	else if( pNode->pParent->pLChild==pNode )ppNode=&pNode->pParent->pLChild;
	else ppNode=&pNode->pParent->pRChild;

	//the left subtree of the right child becomes of the right subtree of the rotation node
	pNode->pRChild = pRChild->pLChild;
	if( pNode->pRChild )pNode->pRChild->pParent = pNode;

	//the right child becomes the root node of the subtree
	*ppNode = pRChild;
	pRChild->pParent = pNode->pParent;

	//the rotation node becomes the left child
	pRChild->pLChild = pNode;
	pNode->pParent = pRChild;

	CalcHeight(pNode);
	CalcHeight(pRChild);
}
Esempio n. 2
0
int AvlTree<KEY, VALUE>::CalcBalance(BstNode<KEY, VALUE>* tree)
{
  if (!tree)
    return 0;

  int ht = CalcHeight(tree->left);
  return (ht - CalcHeight(tree->right));
}
Esempio n. 3
0
BstNode<KEY, VALUE>* AvlTree<KEY, VALUE>::RmHasBoth(BstNode<KEY, VALUE>* tree)
{
  int htl, htlr, htr, htrl, maxht;

  htl = ((htlr = CalcHeight(tree->left->right)) ? 0:CalcHeight(tree->left));
  htr = ((htrl = CalcHeight(tree->right->left)) ? 0:CalcHeight(tree->right));

  maxht = ObjRelation<int>::Max(ObjRelation<int>::Max(htlr, htrl),
                                ObjRelation<int>::Max(htl, htr));

  return
  (
    (maxht == htl) ? RmLeft(tree):
    (maxht == htr) ? RmRight(tree):
    (maxht == htlr) ? RmLeftRight(tree):
		      RmRightLeft(tree)
  );
}
Esempio n. 4
0
void CFileSaveAsGraphicDlg::SelectDimensionSet()
{
   CPoint p = GetSelectedDimensionSet ();
   
   m_dWidth = (double)p.x;
   m_dHeight = (double)p.y;
   
   LimitDimensions ();
   CalcHeight ();
   CalcWidth ();
}
Esempio n. 5
0
void CFileSaveAsGraphicDlg::OnKillFocusWidth() 
{
   if ((UpdateData (TRUE) != FALSE) && (m_Aspect == TRUE))
   {
      if (m_Width != (UINT)m_dWidth)
      {
         m_dWidth = (double)m_Width;
         CalcHeight ();
         UpdateData (FALSE);
      }
   }
}
Esempio n. 6
0
void CSystemControlModel::Resize()
{
  int width = 0;  
  for (auto child : GetChildren()) {
    width = MAX(child->GetRect().GetWidth(), width);
  }
  width += 15;

  int height = CalcHeight();

	rect.Right() = rect.Left() + width;
	rect.Bottom() = rect.Top() + height;
}
Esempio n. 7
0
int main()
{
    int i,j,k,len,maxlen;
    int l,r,mid;
    freopen("poj3294.txt","r",stdin);
    freopen("poj3294ans.txt","w",stdout);
    while (scanf("%d",&n) && n)
    {
        scanf("%s",s[0]);
        sum[0]=strlen(s[0]);
        maxlen=sum[0];
        if (n==1)
        {
            printf("%s\n\n",s[0]);
            continue;
        }
        for (i=1;i<n;i++)
        {
            scanf("%s",s[i]);
            sum[i]=sum[i-1]+strlen(s[i])+1;
        }
        k=0;
        for (i=0;i<n;i++)
        {
            len=strlen(s[i]);
            for (j=0;j<len;j++) num[k++]=s[i][j]-'a'+100;
            num[k++]=i;
        }

        m=k-1;
        DA(num,sa,rank,k,130);
        CalcHeight(num,sa,height,k-1);

        l=0;
        r=m;
        while (l<r)
        {
            mid=(l+r+1)/2;
            if (Check(mid,0)) l=mid;
            else r=mid-1;
        }

        if (l==0) printf("?\n\n");
        else
        {
            Check(l,1);
            printf("\n");
        }
    }
    return 0;
}
Esempio n. 8
0
int main()
{
    int i;
    freopen("poj3261.txt","r",stdin);
    freopen("poj3261ans.txt","w",stdout);
    while (scanf("%d%d",&n,&k)!=EOF)
    {
        for (i=0;i<n;i++) scanf("%d",&num[i]);
        num[n]=0;
        DA(num,sa,rank,n+1,10000);
        CalcHeight(num,sa,height,n);
        BinSearch(1,n);
        printf("%d\n",ans);
    }
}
Esempio n. 9
0
//! Expose callback.
void GraphView::BeginViewExpose(bool predraw)
{
	glViewport( 0, 0, mW, mH );

	glDisable(GL_DEPTH_TEST);
	glClearColor( mClearColor[0], mClearColor[1], mClearColor[2], mClearColor[3]);
	glClear(GL_COLOR_BUFFER_BIT);

	glLineWidth(1.0);

	int lWidth = CalcWidth();
	int lHeight = CalcHeight();

	Push2dViewport(lWidth, lHeight);

	// call graph manager proc
	if (predraw)	PreDraw();
}
Esempio n. 10
0
void CStatBox::Update(int line,const char* string,double data)
{
    if(line < 0 || line >= lines->size()) return;

    char buf[STATBOX_MAX_LINE_LENGTH];
    sprintf(buf,"%s%.2f",string,data);

    CSurface* text = factory->RenderText(buf);
    if(text != NULL)
    {
        if(lines->at(line) != NULL)
        {
            //free old surface
            delete lines->at(line);
            lines->at(line) = NULL;
        }
        lines->at(line) = text;
    }

    w = CalcWidth();
    h = CalcHeight();
}
Esempio n. 11
0
TwistDeformer::TwistDeformer(
      TimeValue t, ModContext &mc,
      float angle, int naxis, float bias,
      float from, float to, int doRegion,
      Matrix3& modmat, Matrix3& modinv) 
   {  
   this->doRegion = doRegion;
   this->from = from;
   this->to   = to;
   if (bias!=0.0f) {
      this->bias = 1.0f-(bias+100.0f)/200.0f;
      if (this->bias < 0.00001f) this->bias = 0.00001f;
      if (this->bias > 0.99999f) this->bias = 0.99999f;
      this->bias = float(log(this->bias)/log(0.5));
      doBias = TRUE;
   } else {
      this->bias = 1.0f;
      doBias = FALSE;
      }
   
   Matrix3 mat;
   Interval valid;   
   time   = t; 

   tm = modmat;
   invtm = modinv;
   mat.IdentityMatrix();
   
   switch ( naxis ) {
      case 0: mat.RotateY( -HALFPI );   break; //X
      case 1: mat.RotateX( HALFPI );  break; //Y
      case 2: break;  //Z
      }
   SetAxis( mat );   
   assert (mc.box);
   bbox = *mc.box;
   CalcHeight(naxis,DegToRad(angle));
   }
Esempio n. 12
0
File: Font.cpp Progetto: arajar/funk
v2i Font::CalcDimen( const char* a_text ) const
{
	return v2i(CalcWidth(a_text), CalcHeight() );
}
Esempio n. 13
0
void CObAvlTree::RemoveAt(POSITION position)
{
	//avl tree removing. 
	//For a node having children, the concept model is to find the inorder successor of the removing node,
	//copy the data of IOS to the removing node, then remove the IOS rather than the removing node.
	//the rotation balance begins from the parent of the IOS and traces back to its ancestors.
	//however, the implementation removes the true removing node so that the iterator won't point to a garbage node.

	ASSERT_VALID(this);

	CNode* pOldNode = (CNode*) position;
	ASSERT(AfxIsValidAddress(pOldNode, sizeof(CNode)));

	//block 1. regular binary tree remove, except makeing pRotateNode point to the start tracing node for rotation,
	//and the bubble node's height is replaced the height of the removed node
	
	//case 1. single child, just bubble the child the the parent place
	//case 2. double children, bubble the in order successor to the parent place.
	CNode* pBubbleNode = NULL;
	CNode* pRotateNode = NULL;
	if( pOldNode->pLChild==NULL ){
		pBubbleNode = pOldNode->pRChild;
		pRotateNode = pOldNode->pParent;
	}else if( pOldNode->pRChild==NULL ){
		pBubbleNode = pOldNode->pLChild;
		pRotateNode = pOldNode->pParent;
	}else{
		//case 2
		pBubbleNode = pOldNode->pRChild;
		while( pBubbleNode->pLChild )pBubbleNode = pBubbleNode->pLChild;
		//now pBubbleNode is the IOS, but if bubble node is the right child of the removing node,
		//do not change the right child of the bubble node
		if( pBubbleNode->pParent!=pOldNode ){ //the bubble node is more than one height below the removing node
			pBubbleNode->pParent->pLChild = pBubbleNode->pRChild;
			if( pBubbleNode->pRChild )pBubbleNode->pRChild->pParent=pBubbleNode->pParent;
			//save the rotation place.
			pRotateNode = pBubbleNode->pParent;
			//link the right child of the removing node to bubble node
			pBubbleNode->pRChild = pOldNode->pRChild;
			pOldNode->pRChild->pParent = pBubbleNode;
		}else{
			pRotateNode = pBubbleNode;
		}
		//link the left child of the removing node to the bubble node
		pBubbleNode->pLChild = pOldNode->pLChild;
		pOldNode->pLChild->pParent = pBubbleNode;

		//replace the height of the bubble node by the removed node
		ASSERT(AfxIsValidAddress(pBubbleNode, sizeof(CNode)));
		pBubbleNode->nHeight = pOldNode->nHeight;
	}
	CNode** ppNode=NULL;
	if( pOldNode==m_pNodeRoot )ppNode = &m_pNodeRoot;
	else if( pOldNode->pParent->pLChild==pOldNode )ppNode=&pOldNode->pParent->pLChild;
	else ppNode=&pOldNode->pParent->pRChild;

	*ppNode = pBubbleNode;
	if( pBubbleNode )pBubbleNode->pParent = pOldNode->pParent;

	FreeNode(pOldNode);
	
	//block 2, avl tree balance rotation
	//the loop goes upward to find the rotation node until the root node is reached or the current subtree's height is not changed.
	//if a rotation is done, LOOP CONTINUE beause the height of the subtree at the rotation node may decrease,
	//so the ancestors may need rotation.
	//if the rotation is not done for the node, then adjust the height of the current node.
	while( pRotateNode ){
		int nOldHeight = NODEHEIGHT(pRotateNode);
		if( NODEHEIGHT(pRotateNode->pLChild)-NODEHEIGHT(pRotateNode->pRChild)==2 ){
			//left subtree is too high, rotate to right
			CNode* pChildNode = pRotateNode->pLChild;
			ASSERT( pChildNode!=NULL );
			if( NODEHEIGHT(pChildNode->pLChild)>=NODEHEIGHT(pChildNode->pRChild) ){
				SingleRightRotation( pRotateNode );
			}else{
				ASSERT( NODEHEIGHT(pChildNode->pLChild)<NODEHEIGHT(pChildNode->pRChild) );
				DoubleRightRotation( pRotateNode );
			}
			//after the rotation, the pRotateNode becomes a child of the node at the rotation point
			pRotateNode = pRotateNode->pParent;
		}else if( NODEHEIGHT(pRotateNode->pRChild)-NODEHEIGHT(pRotateNode->pLChild)==2 ){
			//right subtree is too high, rotate to left
			CNode* pChildNode = pRotateNode->pRChild;
			ASSERT( pChildNode!=NULL );
			if( NODEHEIGHT(pChildNode->pLChild)<=NODEHEIGHT(pChildNode->pRChild) ){
				SingleLeftRotation( pRotateNode );
			}else{
				ASSERT( NODEHEIGHT(pChildNode->pLChild)>NODEHEIGHT(pChildNode->pRChild) );
				DoubleLeftRotation( pRotateNode );
			}
			//after the rotation, the pRotateNode becomes a child of the node at the rotation point
			pRotateNode = pRotateNode->pParent;
		}else{
			//no rotation happend here, adjust the height
			CalcHeight( pRotateNode );
		}

		if( nOldHeight==NODEHEIGHT(pRotateNode) ){
			//since the subtree's height is not changed, STOP LOOP since the ancestors' height is maintained.
			break;
		}
		pRotateNode = pRotateNode->pParent;
	}
}
Esempio n. 14
0
POSITION CObAvlTree::Insert(CObject* newElement)
{
	ASSERT_VALID(this);

	//block 1, regular binary insertion
	CNode* pParent = m_pNodeRoot;
	CNode** ppNode = &m_pNodeRoot;

	while( *ppNode ){
		pParent = *ppNode;
		int nRet = CompareNode( (LPVOID)&newElement, (LPVOID)&pParent->data );
		if( nRet<=0 ){
			ppNode = &pParent->pLChild;
		}else if( nRet>0 ){
			ppNode = &pParent->pRChild;
		}
	}

	CNode* pNewNode = NewNode(pParent, NULL, NULL);
	pNewNode->data = newElement;
	*ppNode = pNewNode;
	
	//block 2, avl tree balance rotation
	//the loop goes upward to find the rotation node, 
	//whenever a rotation is done, STOP LOOP beause the ancestors' height are the same after rotation.
	//if the rotation is not yet found, then adjust the height of the current node.
	CNode* pRotateNode = pParent;
	while( pRotateNode ){
		if( NODEHEIGHT(pRotateNode->pLChild)-NODEHEIGHT(pRotateNode->pRChild)==2 ){
			//left subtree is too high, rotate to right
			CNode* pChildNode = pRotateNode->pLChild;
			ASSERT( pChildNode!=NULL );
			if( NODEHEIGHT(pChildNode->pLChild)>NODEHEIGHT(pChildNode->pRChild) ){
				SingleRightRotation( pRotateNode );
			}else{
				ASSERT( NODEHEIGHT(pChildNode->pLChild)<NODEHEIGHT(pChildNode->pRChild) );
				DoubleRightRotation( pRotateNode );
			}
			break;
		}else if( NODEHEIGHT(pRotateNode->pRChild)-NODEHEIGHT(pRotateNode->pLChild)==2 ){
			//right subtree is too high, rotate to left
			CNode* pChildNode = pRotateNode->pRChild;
			ASSERT( pChildNode!=NULL );
			if( NODEHEIGHT(pChildNode->pLChild)<NODEHEIGHT(pChildNode->pRChild) ){
				SingleLeftRotation( pRotateNode );
			}else{
				ASSERT( NODEHEIGHT(pChildNode->pLChild)>NODEHEIGHT(pChildNode->pRChild) );
				DoubleLeftRotation( pRotateNode );
			}
			break;
		}
		//no rotation happend here, adjust the height
		CalcHeight( pRotateNode );
		pRotateNode = pRotateNode->pParent;
	}
		
/*	CNode* pRotateNode = pParent;
	CNode* pChildNode = pNewNode;
	CNode* pGrandChdNode = pNewNode;
	
	while( pRotateNode ){
		if( pRotateNode->pLChild==pChildNode ){
			//inserted in the left subtree.
			if( NODEHEIGHT(pRotateNode->pLChild)-NODEHEIGHT(pRotateNode->pRChild)==2 ){
				ASSERT( pChildNode!=pGrandChdNode );
				if( pChildNode->pLChild==pGrandChdNode ){
					SingleRightRotation( pRotateNode );
				}else{
					DoubleRightRotation( pRotateNode );
				}
				break;
			}
		}else{
			//inserted in the right subtree
			if( NODEHEIGHT(pRotateNode->pRChild)-NODEHEIGHT(pRotateNode->pLChild)==2 ){
				ASSERT( pChildNode!=pGrandChdNode );
				if( pChildNode->pRChild==pGrandChdNode ){
					SingleRightRotation( pRotNode );
				}else{
					DoubleRightRotation( pRotNode );
				}
				break;
			}
		}
		//no rotation happend here, adjust the height
		CalcuHeight( pRotateNode );
		//change the family
		pGrandChdNode = pChildNode;
		pChildNode = pRotateNode;
		pRotateNode = pRotateNode->pParent;
	}*/

/*	CNode* pRotateNode = pParent;
	CNode* pNode = pNewNode;
	while( pRotateNode ){
		CalcuHeight( pRotateNode );
		if( pRotateNode->pLChild==pNode ){
			//inserted in the left subtree.
			if( NODEHEIGHT(pRotateNode->pLChild)-NODEHEIGHT(pRotateNode->pRChild)==2 ){
				int nRet = CompareNode( (LPVOID)&newElement, (LPVOID)&pNode->data );
				if( nRet<=0 ){
					SingleRightRotation( pRotNode );
				}else{
					DoubleRightRotation( pRotNode );
				}
				break;
			}
		}else{
			//inserted in the right subtree
			if( NODEHEIGHT(pRotateNode->pRChild)-NODEHEIGHT(pRotateNode->pLChild)==2 ){
				int nRet = CompareNode( (LPVOID)&newElement, (LPVOID)&pNode->data );
				if( nRet<=0 ){
					SingleRightRotation( pRotNode );
				}else{
					DoubleRightRotation( pRotNode );
				}
				break;
			}
		}
		//no rotation happend here, adjust the height
		CalcuHeight( pRotateNode );
		pNode = pRotateNode;
		pRotateNode = pRotateNode->pParent;
	}*/
	return (POSITION) pNewNode;

}
Esempio n. 15
0
int CSystemControlModel::GetMiddle( ) const
{
	return CalcHeight() / 2;
}