Exemplo n.º 1
0
NodeT* insertNode(NodeT* a, int value)
{
    if (a == NULL)
        return(createNode(value));
    if (value < a->value)
        a->left  = insertNode(a->left, value);
    else
        a->right = insertNode(a->right, value);
    a->height = max(height(a->left), height(a->right)) + 1;
    int balance = getBalanceTree(a);
    if (balance < -1 && value > a->right->value)
        return RotateLeft(a);
    if (balance > 1 && value < a->left->value)
        return RotateRight(a);

    if (balance < -1 && value < a->right->value)
    {
        a->right = RotateRight(a->right);
        return RotateLeft(a);
    }
    if (balance > 1 && value > a->left->value)
    {
        a->left =  RotateLeft(a->left);
        return RotateRight(a);
    }

    return a;
}
Exemplo n.º 2
0
static void InsertFixup(rbtree_t *tree, rbnode_t *X)
{
	/*************************************
	 *  maintain red-black tree balance  *
	 *  after inserting node X           *
	 *************************************/

	/* check red-black properties */
	while (X != tree->Root && X->Parent->Color == Red) {
		/* we have a violation */
		if (X->Parent == X->Parent->Parent->Left) {
			rbnode_t *Y = X->Parent->Parent->Right;
			if (Y->Color == Red) {

				/* uncle is red */
				X->Parent->Color = Black;
				Y->Color = Black;
				X->Parent->Parent->Color = Red;
				X = X->Parent->Parent;
			} else {

				/* uncle is black */
				if (X == X->Parent->Right) {
					/* make X a left child */
					X = X->Parent;
					RotateLeft(tree, X);
				}

				/* recolor and rotate */
				X->Parent->Color = Black;
				X->Parent->Parent->Color = Red;
				RotateRight(tree, X->Parent->Parent);
			}
		} else {

			/* mirror image of above code */
			rbnode_t *Y = X->Parent->Parent->Left;
			if (Y->Color == Red) {

				/* uncle is red */
				X->Parent->Color = Black;
				Y->Color = Black;
				X->Parent->Parent->Color = Red;
				X = X->Parent->Parent;
			} else {

				/* uncle is black */
				if (X == X->Parent->Left) {
					X = X->Parent;
					RotateRight(tree, X);
				}
				X->Parent->Color = Black;
				X->Parent->Parent->Color = Red;
				RotateLeft(tree, X->Parent->Parent);
			}
		}
	}

	tree->Root->Color = Black;
}
void KeyControlledTransitionBarbarian_cl::ProcessKeyboardEvents()
{
  bool bUp = VAppImpl::GetInputMap()->GetTrigger(CHARACTER_MOVE_FORWARD)!=0;
  bool bRotLeft = VAppImpl::GetInputMap()->GetTrigger(CHARACTER_ROTATE_LEFT)!=0;
  bool bRotRight = VAppImpl::GetInputMap()->GetTrigger(CHARACTER_ROTATE_RIGHT)!=0;
  bool bShift = VAppImpl::GetInputMap()->GetTrigger(CHARACTER_RUN)!=0;

	if (bUp)
  {
    // Trigger the run/walk actions when CURSOR UP is pressed.
    // Allow rotating the entity while walking/running
    if (bShift)
      Run();
    else
      Walk();

		if (bRotLeft)
      RotateLeft();
    else if (bRotRight)
      RotateRight();
  }
  else
  {
    if (bRotLeft)
      RotateLeft();
    else if (bRotRight)
      RotateRight();
    else
      Stand();
  }
}
Exemplo n.º 4
0
void InsertAfter(RBNode** Root, RBNode* Node)
{
	while (Node!=(*Root) && Node->Parent->Color == Red)
	{
		
		if(Node->Parent == Node->Parent->Parent->Left)
		{
			RBNode* Uncle = Node->Parent->Parent->Right;
			if(Uncle->Color == Red)
			{
				Node->Parent->Color = Black;
				Uncle->Color = Black;
				Node->Parent->Parent->Color = Red;

				Node= Node->Parent->Parent;
			}
			else
			{
				if(Node == Node->Parent ->Right)
				{
					Node= Node->Parent;
					RotateLeft(Root,Node);
				}

				Node->Parent->Color = Black;
				Node->Parent->Parent->Color = Red;

				RotateRight(Root, Node->Parent->Parent);
			}
		}
		else
		{
			RBNode* Uncle = Node->Parent->Parent->Left;
			if(Uncle->Color == Red)
			{
				Node->Parent->Color = Black;
				Uncle->Color = Black;
				Node->Parent->Parent->Color = Red;
				Node = Node->Parent->Parent;
			}
			else
			{
				if(Node == Node->Parent->Left)
				{
					Node = Node->Parent;
					RotateRight(Root, Node);
				}

				Node->Parent->Color = Black;
				Node->Parent->Parent->Color = Red;
				RotateLeft(Root, Node->Parent->Parent);
			}
		}
	}
	(*Root) ->Color = Black;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "Core.h"

////////////////////////////////////////////////////////////////////////////////////////////////////

void CLR_RT_AVLTree::Initialize()
{
    NATIVE_PROFILE_CLR_CORE();
    m_root = NULL; // Entry*    m_root;
                   // OwnerInfo m_owner;
}

CLR_RT_AVLTree::Result CLR_RT_AVLTree::Insert( Entry* newDatum )
{
    NATIVE_PROFILE_CLR_CORE();
    return Insert( m_root, newDatum );
}

CLR_RT_AVLTree::Result CLR_RT_AVLTree::Remove( Entry* oldDatum )
{
    NATIVE_PROFILE_CLR_CORE();
    return Remove( m_root, oldDatum );
}

CLR_RT_AVLTree::Entry* CLR_RT_AVLTree::Find( Entry* srcDatum )
{
    NATIVE_PROFILE_CLR_CORE();
    Entry*      node  = m_root;
    ComparerFtn ftn   = m_owner.m_ftn_compare;
    void*       state = m_owner.m_state;

    while(node)
    {
        int cmp = ftn( state, node, srcDatum ); if(cmp == 0) break;

        if(cmp > 0)
        {
            node = node->m_left;
        }
        else
        {
            node = node->m_right;
        }
    }

    return node;
}

//////////////////////////////////////////////////

//
// Perform counterclockwise rotation.
//
void CLR_RT_AVLTree::RotateLeft( Entry*& n )
{
    NATIVE_PROFILE_CLR_CORE();
    //////////////////////////
    //                      //
    // A            A       //
    //  \            \      //
    //   B      =>    \     //
    //  / \            \    //
    //     C            C   //
    //    / \          / \  //
    //                B     //
    //               / \    //
    //                      //
    //////////////////////////
    Entry* top    = n;
    Entry* bottom = top->m_right;

    top   ->m_right = bottom->m_left;
    bottom->m_left  = top;

    n = bottom;
}

//
// Perform clockwise rotation.
//
void CLR_RT_AVLTree::RotateRight( Entry*& n )
{
    NATIVE_PROFILE_CLR_CORE();
    //////////////////////////
    //                      //
    //      A          A    //
    //     /          /     //
    //    B    =>    /      //
    //   / \        /       //
    //  C          C        //
    // / \        / \       //
    //               B      //
    //              / \     //
    //                      //
    //////////////////////////

    Entry* top    = n;
    Entry* bottom = top->m_left;

    top   ->m_left  = bottom->m_right;
    bottom->m_right = top;

    n = bottom;
}

//
// LeftGrown: helper function for avlinsert
//
// Parameters:
//
//   n           Reference to the pointer to a node. This node's left
//               subtree has just grown due to item insertion; its
//               "skew" flag needs adjustment, and the local tree
//               (the subtree of which this node is the root node) may
//               have become unbalanced.
//
// Return values:
//
//   RES_OK      The local tree could be rebalanced or was balanced
//               from the start. The parent activations of the avlinsert
//               activation that called this function may assume the
//               entire tree is valid.
//
//   RES_BALANCE The local tree was balanced, but has grown in height.
//               Do not assume the entire tree is valid.
//
CLR_RT_AVLTree::Result CLR_RT_AVLTree::LeftGrown( Entry*& n )
{
    NATIVE_PROFILE_CLR_CORE();
    Entry* left;
    Entry* right;

    switch(n->m_skew)
    {
    case SKEW_LEFT:
        left = n->m_left;

        if(left->m_skew == SKEW_LEFT)
        {
            n   ->m_skew = SKEW_NONE;
            left->m_skew = SKEW_NONE;

            RotateRight( n );
        }
        else
        {
            right = left->m_right;

            switch(right->m_skew)
            {
            case SKEW_LEFT:
                n   ->m_skew = SKEW_RIGHT;
                left->m_skew = SKEW_NONE;
                break;

            case SKEW_RIGHT:
                n   ->m_skew = SKEW_NONE;
                left->m_skew = SKEW_LEFT;
                break;

            default:
                n   ->m_skew = SKEW_NONE;
                left->m_skew = SKEW_NONE;
            }

            right->m_skew = SKEW_NONE;

            RotateLeft ( n->m_left );
            RotateRight( n         );
        }
        return RES_OK;

    case SKEW_RIGHT:
        n->m_skew = SKEW_NONE;
        return RES_OK;

    default:
        n->m_skew = SKEW_LEFT;
        return RES_BALANCE;
    }
}

///*
// *  avlrightgrown: helper function for avlinsert
// *
// *  See avlleftgrown for details.
// */
CLR_RT_AVLTree::Result CLR_RT_AVLTree::RightGrown( Entry*& n )
{
    NATIVE_PROFILE_CLR_CORE();
    Entry* left;
    Entry* right;

    switch(n->m_skew)
    {
    case SKEW_LEFT:
        n->m_skew = SKEW_NONE;
        return RES_OK;

    case SKEW_RIGHT:
        right = n->m_right;

        if(right->m_skew == SKEW_RIGHT)
        {
            n    ->m_skew = SKEW_NONE;
            right->m_skew = SKEW_NONE;

            RotateLeft( n );
        }
        else
        {
            left = right->m_left;

            switch(left->m_skew)
            {
            case SKEW_RIGHT:
                n    ->m_skew = SKEW_LEFT;
                right->m_skew = SKEW_NONE;
                break;

            case SKEW_LEFT:
                n    ->m_skew = SKEW_NONE;
                right->m_skew = SKEW_RIGHT;
                break;

            default:
                n    ->m_skew = SKEW_NONE;
                right->m_skew = SKEW_NONE;
            }

            left->m_skew = SKEW_NONE;

            RotateRight( n->m_right );
            RotateLeft ( n          );
        }
        return RES_OK;

    default:
        n->m_skew = SKEW_RIGHT;
        return RES_BALANCE;
    }
}
Exemplo n.º 6
0
Arquivo: rbtree.cpp Projeto: Tdarra/a3
bool RBTree<T>::Insert(T item){
	bool flag = false;
	Node<T>* inserted_node = NULL;
	if (!Contains(item)){
		inserted_node = BSTInsert(item); // normal BST insertion method
		inserted_node->is_black = false;
		while (inserted_node != root && !inserted_node->p->is_black){ // iterates until root or black parent is reached
			if (inserted_node->p == inserted_node->p->p->left){
				Node<T>* uncle = inserted_node->p->p->right; // node's uncle
				if(!uncle->is_black){
					inserted_node->p->is_black = true;
					uncle->is_black = true;
					inserted_node->p->p->is_black = false;
					inserted_node = inserted_node->p->p;

				}
				else {//uncle is black yo
					if (inserted_node == inserted_node->p->right){
						inserted_node = inserted_node->p;
						RotateLeft(inserted_node);
					}
					inserted_node->p->is_black = true;
					inserted_node->p->p->is_black = false;
					RotateRight(inserted_node->p->p);
				}
			}
			if (inserted_node->p == inserted_node->p->p->right) {
				Node<T>* aunt = inserted_node->p->p->left;
				if(!aunt->is_black){
					inserted_node->p->is_black = true;
					aunt->is_black = true;
					inserted_node->p->p->is_black = false;
					inserted_node = inserted_node->p->p;
				}
				else{
					if(inserted_node == inserted_node->p->left){
						inserted_node = inserted_node->p;
						RotateRight(inserted_node);
					}
					inserted_node->p->is_black = true;
					inserted_node->p->p->is_black = false;
					RotateLeft(inserted_node->p->p);
				}
			}
		}
		root->is_black = true;//FIXME
		flag = true;
		size++;

	}

	return flag;


}
Exemplo n.º 7
0
void RBTree<KEY>::InsertFixUp(RBNode* pNode)
{
    while(pNode->parent->color == RED)  //如果p[z]==RED,那么p[z]不是根节点,所以p[p[z]一定存在。
    {
        if(pNode->parent == pNode->parent->parent->left)
        {
            RBNode* pUncle = pNode->parent->parent->right;
            if(pUncle->color == RED)
            {
                pNode->parent->color = BLACK;
                pUncle->color = BLACK;
                pNode->parent->parent->color = RED;
                pNode = pNode->parent->parent;
            }
            else
            {
                if(pNode == pNode->parent->right)
                {
                    pNode = pNode->parent;
                    RotateLeft(pNode);
                }
                pNode->parent->color=BLACK;
                pNode->parent->parent->color=RED;
                RotateRight(pNode->parent->parent);
            }
        }
        else
        {
            RBNode* pUncle = pNode->parent->parent->left;
            if(pUncle->color == RED)
            {
                pNode->parent->color = BLACK;
                pUncle->color = BLACK;
                pNode->parent->parent->color = RED;
                pNode = pNode->parent->parent;
            }
            else
            {
                if(pNode == pNode->parent->left)
                {
                    pNode = pNode->parent;
                    RotateRight(pNode);
                }
                pNode->parent->color=BLACK;
                pNode->parent->parent->color=RED;
                RotateLeft(pNode->parent->parent);
            }
        }

    }

    m_root->color=BLACK;

}
Exemplo n.º 8
0
void AVL_tree::Balance(Node& node)
{
  if(BalanceFactor(node) == 2) // Unbalance on right side
    {
      if(BalanceFactor(node->right) < 0) // If left chain longer
	RotateLeft(node->right);
      RotateRight(node);
    }
  if(BalanceFactor(node) == -2)
    {
      if(BalanceFactor(node->left) > 0)
	RotateRight(node->left);
      RotateLeft(node);
    }
}
Exemplo n.º 9
0
//**************************************************
//  NAME: RotateLeft
//  DESCRIPTION: Robot rotates over the center   
//  PARAMETERS: - speed->Motor speed
//              - angle->Rotation angle
//**************************************************
void Motor::RotateLeft(byte speed, int angle)
{
  unsigned long time,timeout;
  byte count;
  byte encoder;

	ReduceInertia();
	
  count = angle/(2*ENCODER_ANGLE);
  encoder = digitalRead(ENC_R);
  time=millis();
  RotateLeft(speed);
  while (count > 0)
  {
    while (encoder == digitalRead(ENC_R))
    {
      timeout=millis();
      if (timeout - time > MAX_TIMEOUT)
      {        
        return;
      }
    }
    encoder=digitalRead(ENC_R);
    count--;
    time=millis();
  }
  ReduceInertia();
}
Exemplo n.º 10
0
void MD5::GG( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T)
{
        uint32_t G = (B & D) | (C & ~D);
        A += G + X + T;
        A = RotateLeft(A, S);
        A += B;
}
Exemplo n.º 11
0
/*! Single or double left rotates the AVL tree on the node to balance
*/
void LeftRotateAvlTree(AVL_TREE_PTR node, AVL_TREE_PTR *avl_root_ptr)
{
	AVL_TREE_PTR child = AVL_TREE_RIGHT_NODE(node);
	int balance_factor = GetAvlTreeBalanceFactor(child);
	BINARY_TREE_PTR bt_root_ptr;
	
	/*only -1,0 or 1 balance is expected*/
	assert( balance_factor<=1 && balance_factor>=-1 );
	
	/*check for double rotate*/
	if ( balance_factor == -1 ) {
		CALCULATE_ROOT_PTR_AVL_TO_BT(avl_root_ptr, &bt_root_ptr);
		RotateRight( &child->bintree, &bt_root_ptr);
		CALCULATE_ROOT_PTR_BT_TO_AVL(avl_root_ptr, &bt_root_ptr);
	}
	
	/*single rotate*/
	CALCULATE_ROOT_PTR_AVL_TO_BT(avl_root_ptr, &bt_root_ptr);
	RotateLeft( &node->bintree, &bt_root_ptr);
	CALCULATE_ROOT_PTR_BT_TO_AVL(avl_root_ptr, &bt_root_ptr);
	
	/*calculate the new heights*/
	RECALCULATE_HEIGHT(node);
	RECALCULATE_HEIGHT(child);
}
Exemplo n.º 12
0
void MD5::HH( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T)
{
        uint32_t H = (B ^ C ^ D);
        A += H + X + T;
        A = RotateLeft(A, S);
        A += B;
}
Exemplo n.º 13
0
void MD5::II( uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t X, uint32_t S, uint32_t T)
{
        uint32_t I = (C ^ (B | ~D));
        A += I + X + T;
        A = RotateLeft(A, S);
        A += B;
}
Exemplo n.º 14
0
void CBinaryTree::AddNode(int N)
{
 CNode *pNode,*pParent=NULL,*pNewNode;

 pNode=LocateNode(N,pRoot);
 pParent=LocateParent(N,pRoot);
 if(pParent==NULL)
  pRoot=new CNode(N);
 else
 {
  if(pNode==NULL)
  {
   pNewNode=new CNode(N,pParent);
   if(pParent->GetData()>N)
    pParent->pLeft=pNewNode;
   else
    pParent->pRight=pNewNode;
   }
  }
 while(pParent!=NULL)
 {
  int iBF=GetBalancingFactor(pParent->pRight)-GetBalancingFactor(pParent->pLeft);
  if(iBF<-1 || iBF>1)
  {
   if(iBF>0)
    RotateLeft(pNewNode);
   else
    RotateRight(pNewNode);
   if(pNewNode->pParent==NULL) pRoot=pNewNode;
  }
  pParent=pParent->pParent;
  pNewNode=pNewNode->pParent;
 }
}
Exemplo n.º 15
0
  Node<Key, Val>* InsertInternal(Node<Key, Val>* h, Key key, Val val){
    if (h == NULL){
      ++num_;
      return new Node<Key, Val>(key, val);
    }
    
    if (IsRED(h->left) && IsRED(h->right)){
      FlipColor(h);
    }

    if (key == h->key){
      h->val = val;
    } else if (Comp()(key, h->key)){
      h->left = InsertInternal(h->left, key, val);
    } else {
      h->right = InsertInternal(h->right, key, val);
    }
    
    if (IsRED(h->right)){
      h = RotateLeft(h);
    }

    if (IsRED(h->left) && IsRED(h->left->left)){
      h = RotateRight(h);
    }

    return h;
  }
Exemplo n.º 16
0
void wxPageContainer::OnLeftDClick(wxMouseEvent& event)
{
	wxPageInfo pgInfo;
	int tabIdx;
	int where = HitTest(event.GetPosition(), pgInfo, tabIdx);
	switch(where)
	{
	case wxFNB_RIGHT_ARROW:
		RotateRight();
		break;
	case wxFNB_LEFT_ARROW:
		RotateLeft();
		break;
	case wxFNB_TAB:
		if(HasFlag(wxFNB_DCLICK_CLOSES_TABS))
		{
			DeletePage((size_t)tabIdx);
		}
		break;
	case wxFNB_X:
		{
			OnLeftDown(event);
		}
		break;
	default:
		event.Skip();
		break;
	}
}
void KeyControlledTransitionCharacter_cl::ProcessKeyboardEvents()
{
  if(!m_bKeyboardInputAllowed) 
    return;

  bool bLeft = m_pInputMap->GetTrigger(CHARACTER_MOVE_LEFT)!=0;
  bool bRight = m_pInputMap->GetTrigger(CHARACTER_MOVE_RIGHT)!=0;
  bool bUp = m_pInputMap->GetTrigger(CHARACTER_MOVE_FORWARD)!=0;
  bool bDown = m_pInputMap->GetTrigger(CHARACTER_MOVE_BACKWARD)!=0;
  bool bShift = m_pInputMap->GetTrigger(CHARACTER_RUN)!=0;

	if (bUp)
  {
    // Trigger the run/walk actions when CURSOR UP is pressed.
    // Allow rotating the entity while walking/running
    if (bShift)
      Run();
    else
      Walk();

		if (bLeft)
      RotateLeft();
    else if (bRight)
      RotateRight();
  }
  else if (bDown)
  {
    // Trigger the walk backward action when CURSOR DOWN is pressed.
    // Allow rotating the entity while walking backwards
    WalkBackwards();

    if (bLeft)
      RotateLeft();
    else if (bRight)
      RotateRight();
  }
  else
  {
    if (bLeft)
      RotateLeft();
    else if (bRight)
      RotateRight();
    else
      Stand();
  }
}
Exemplo n.º 18
0
 Node<Key, Val>* MoveREDLeft(Node<Key, Val>* h){
   FlipColor(h);
   if (h->right != NULL && IsRED(h->right->left)){
     h->right = RotateRight(h->right);
     h = RotateLeft(h);
     FlipColor(h);
   }
   return h;
 }
Exemplo n.º 19
0
void RedBlackTree::CheckInsert(RBNode* node)
{
	if(node->parent == m_nil)
	{
		node->bRed = false;
		return;
	}
	if(!node->parent->bRed)
	{
		return;
	}

	if(getUncle(node)->bRed)
	{
		node->parent->bRed = false;
		getUncle(node)->bRed = false;
		getGrandParent(node)->bRed = true;
		CheckInsert(getGrandParent(node));
		return;
	}

	if(node == node->parent->right && node->parent == getGrandParent(node)->left)
	{
		RotateLeft(node->parent);
		node = node->left;
	}
	else if(node == node->parent->left && node->parent == getGrandParent(node)->right)
	{
		RotateRight(node->parent);
		node = node->right;
	}

	node->parent->bRed = false;
	getGrandParent(node)->bRed = true;
	if(node == node->parent->left)
	{
		RotateRight(getGrandParent(node));
	}
	else
	{
		RotateLeft(getGrandParent(node));
	}
}
Exemplo n.º 20
0
	static void Balance(pNode& p)
	{
		if (!p)
			return;
		FixHeight(p);
		if (BalanceFactor(p) == 2)
		{
			if (BalanceFactor(p->Right) < 0)
				RotateRight(p->Right);
			RotateLeft(p);
		}

		if (BalanceFactor(p) == -2)
		{
			if (BalanceFactor(p->Left) > 0)
				RotateLeft(p->Left);
			RotateRight(p);
		}
	}
Exemplo n.º 21
0
void NodeBase::insert(NodeBase *head, NodeBase * n) {
    /* check Red-Black properties */
    while (n != head->left && n->parent->color == NodeColor::Red) {
    	auto p = n->parent;
    	auto g = n->parent->parent;
        if (p == g->left) {
        	NodeBase * u = g->right;
            if (u && u->color == NodeColor::Red) {
                p->color = NodeColor::Black;
                u->color = NodeColor::Black;
                g->color = NodeColor::Red;
                n = g;
            } else {
                if (n == p->right) {
                	RotateLeft(head, n, p);
                    n = n->left;
                    p = n->parent;
                }
                p->color = NodeColor::Black;
                g->color = NodeColor::Red;
                RotateRight(head, p, g);
            }
        } else {
        	NodeBase * u = g->left;
            if (u && u->color == NodeColor::Red) {
                p->color = NodeColor::Black;
                u->color = NodeColor::Black;
                g->color = NodeColor::Red;
                n = g;
            } else {
                if (n == n->parent->left) {
                	RotateRight(head, n, p);
            		n = n->right;
                    p = n->parent;
                }
                p->color = NodeColor::Black;
                g->color = NodeColor::Red;
                RotateLeft(head, p, g);
            }
        }
    }
    head->left->color = NodeColor::Black; // root
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::MoveRedLeft(RBTNode<T, KEY> *h)
{
    regaincolors(h);
    if (isRed(h->pRight->pLeft))
    {
        h->pRight = RotateRight(h->pRight);
        h = RotateLeft(h);
        flipcolors(h);
    }
    return h;
}
Exemplo n.º 23
0
void RedBlackTree::BalanceInsertion(Node* target) {
	Node* temp;
	target->black = false;
	while (target != root && target->parent->black == false) {
		if (target->parent == target->parent->parent->left) { //parent is a left child
			temp = target->parent->parent->right;
			if (!temp->black) {
				target->parent->black = true;
				temp->black = true;
				target->parent->parent->black = false;
				target = target->parent->parent;
			} else {
				if (target == target->parent->right) {
					target = target->parent;
					RotateLeft(target);
				}
				target->parent->black = true;
				target->parent->parent->black = false;
				RotateRight(target->parent->parent);
			}
		} else { //parent is a right child, mirror the if
			temp = target->parent->parent->left;
			if (!temp->black) {
				target->parent->black = true;
				temp->black = true;
				target->parent->parent->black = false;
				target = target->parent->parent;
			}
			else {
				if (target == target->parent->left) {
					target = target->parent;
					RotateRight(target);
				}
				target->parent->black = true;
				target->parent->parent->black = false;
				RotateLeft(target->parent->parent);
			}
		}
	}
	root->black = true;
}
Exemplo n.º 24
0
 Node<Key, Val>* FixUp(Node<Key, Val>* h){
   if (IsRED(h->right)){
     h = RotateLeft(h);
   }
   if (IsRED(h->left) && IsRED(h->left->left)){
     h = RotateRight(h);
   }
   if (IsRED(h->left) && IsRED(h->right)){
     FlipColor(h);
   }
   return h;
 }
Exemplo n.º 25
0
Arquivo: main.cpp Projeto: CCJY/coliru
Node<T>* AVLTree<T>::aux_add(Node<T>* current,  T val )
{
    
    if( current == nullptr )
        return new Node<T>( val );
    if( val < current->data )
        current->left = aux_add( current->left, val );
    else
        current->right = aux_add( current->right, val );   
  
    current->height = std::max( Height(current->left), 
                                Height(current->right) ) + 1;

    int balance = ( current == nullptr ) ? 0:
                  ( Height(current->left) - Height(current->right) ) ;
                          
    // Balance the Tree                          
    if (balance > 1 && val < current->left->data )
        return RotateRight(current);
 
    if (balance < -1 && val > current->right->data )
        return RotateLeft(current);

    if (balance > 1 && val > current->left->data )
    {
        current->left =  RotateLeft( current->left );
        return RotateRight(current);
    }

    if (balance < -1 && val < current->right->data )
    {
        current->right = RotateRight(current->right);
        return RotateLeft(current);
    }
   
    return current ;
}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deletemin(RBTNode<T, KEY> *h)
{
    if (h->pLeft == NULL)
    {
        // be careful here hasn't check if the h is empty.
        //std::cout << "Delete min " << h->key << std::endl;
        delete h;
        return NULL;
    }
    if (!isRed(h->pLeft) && !isRed(h->pLeft->pLeft))
        h = MoveRedLeft(h);
    h->pLeft = deletemin(h->pLeft);
    if (isRed(h->pRight))
        h = RotateLeft(h);
    return h;
}
Exemplo n.º 27
0
void Calibrate(void){

     LSA08_Calibrate();

    RotateRight(); motor(180,180);
    __delay_ms(600);

    RotateLeft();
    __delay_ms(600);__delay_ms(600);

    RotateRight(); motor(180,180);
    __delay_ms(600);

    motor(0,0);Forward();
    lcd_clr();

}
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::put(RBTNode<T, KEY> *h, T Value, KEY Key)
{
    if (h == NULL)
    {
        h = new RBTNode<T, KEY>(Value, Key, RED);
        return h;
    }
    if (h->key > Key) h->pLeft = put(h->pLeft, Value, Key);
    else if (h->key < Key) h->pRight = put(h->pRight, Value, Key);
    else h->value = Value;

    if (!isRed(h->pLeft) && isRed(h->pRight)) { h = RotateLeft(h); }
    if (isRed(h->pLeft) && isRed(h->pLeft->pLeft)) { h=RotateRight(h); }
    if (isRed(h->pLeft) && isRed(h->pRight)) { flipcolors(h); }

    return h;
}
Exemplo n.º 29
0
void MyKeyboard(int thekey, int mouseX, int mouseY)
{
	switch(thekey)
	{
		case GLUT_KEY_UP:InclineCameraAltitude();break;
		case GLUT_KEY_DOWN:DeclineCameraAltitude();break;
		case GLUT_KEY_RIGHT:InclineCameraAzimuth();break;
		case GLUT_KEY_LEFT:DeclineCameraAzimuth();break;
		case 'w':MoveForward();break;
		case 's':MoveBackward();break;
		case 'c':ElevateCannon();break;
		case 'z':DeclineCannon();break;
		case '1':RotateLeft();break;
		case '2':RotateRight();break;

	}
		
}
Exemplo n.º 30
0
void cBauul::Logic(float min, float max) {
	if (nextState) {
		Stop();
		animation_frame = 0;
		nextState = false;
		int random = rand()%5;
		if (random == 0) 
			actionstate = STATE_MOVE_FORWARD;
		else if (random == 1)
			actionstate = STATE_ROTATE_LEFT;
		else if (random == 2)
			actionstate = STATE_ROTATE_RIGHT;
		else 
			actionstate = STATE_STANDBY;
	}
	switch(actionstate) {
		case STATE_STANDBY: Stop(); break;
		case STATE_MOVE_FORWARD: MoveForward(min,max); break;
		case STATE_ROTATE_LEFT: RotateLeft(); break;
		case STATE_ROTATE_RIGHT: RotateRight(); break;
	}
}