int main() { int iRetVal = 0; Node *psRoot = NULL; GetNewNode(&psRoot, 10); AddLeftNode(psRoot,5); AddRightNode(psRoot,15); AddLeftNode(psRoot->psLeft,3); AddRightNode(psRoot->psLeft->psLeft,4); AddLeftNode(psRoot->psRight,13); AddRightNode(psRoot->psRight->psLeft,14); AddRightNode(psRoot->psRight,20); AddRightNode(psRoot->psRight->psRight,25); AddLeftNode(psRoot->psRight->psRight->psRight,22); PrintBoundry(psRoot); printf("\n"); SwapNodes(psRoot, 15, 13); PrintBoundry(psRoot); return iRetVal; }
void HuffmanCoDec::UpdateModel( int nextSymbol ) { int currentNode; int newNode; if ( nodes[ iROOT_NODE ].dwWeight == ( DWORD )iMAX_WEIGHT ) RebuildTree(); currentNode = iLeaf[ nextSymbol ]; while( currentNode != -1 ) { nodes[ currentNode ].dwWeight++; for( newNode = currentNode; newNode > iROOT_NODE; newNode-- ) { if ( nodes[ newNode - 1 ].dwWeight >= nodes[ currentNode ].dwWeight ) break; } if ( currentNode != newNode ) { SwapNodes( currentNode, newNode ); currentNode = newNode; } currentNode = nodes[ currentNode ].iParent; } #ifdef _DEBUG VerifyTree(); #endif }
bool TrackList::MoveDown(Track * t) { if (t) { Track *n = GetNext(t, true); if (n) { SwapNodes(t->GetNode(), n->GetNode()); return true; } } return false; }
bool TrackList::MoveUp(Track * t) { if (t) { Track *p = GetPrev(t, true); if (p) { SwapNodes(p->GetNode(), t->GetNode()); return true; } } return false; }
ubi_btNodePtr ubi_btRemove( ubi_btRootPtr RootPtr, ubi_btNodePtr DeadNode ) /* ------------------------------------------------------------------------ ** * This function removes the indicated node from the tree. * * Input: RootPtr - A pointer to the header of the tree that contains * the node to be removed. * DeadNode - A pointer to the node that will be removed. * * Output: This function returns a pointer to the node that was removed * from the tree (ie. the same as DeadNode). * * Note: The node MUST be in the tree indicated by RootPtr. If not, * strange and evil things will happen to your trees. * ------------------------------------------------------------------------ ** */ { ubi_btNodePtr p, *parentp; int tmp; /* if the node has both left and right subtrees, then we have to swap * it with another node. The other node we choose will be the Prev()ious * node, which is garunteed to have no RIGHT child. */ if( (NULL != DeadNode->Link[ubi_trLEFT]) && (NULL != DeadNode->Link[ubi_trRIGHT]) ) SwapNodes( RootPtr, DeadNode, ubi_btPrev( DeadNode ) ); /* The parent of the node to be deleted may be another node, or it may be * the root of the tree. Since we're not sure, it's best just to have * a pointer to the parent pointer, whatever it is. */ if( NULL == DeadNode->Link[ubi_trPARENT] ) parentp = &( RootPtr->root ); else parentp = &((DeadNode->Link[ubi_trPARENT])->Link[(int)(DeadNode->gender)]); /* Now link the parent to the only grand-child and patch up the gender. */ tmp = ((DeadNode->Link[ubi_trLEFT])?ubi_trLEFT:ubi_trRIGHT); p = (DeadNode->Link[tmp]); if( NULL != p ) { p->Link[ubi_trPARENT] = DeadNode->Link[ubi_trPARENT]; p->gender = DeadNode->gender; } (*parentp) = p; /* Finished, reduce the node count and return. */ (RootPtr->count)--; return( DeadNode ); } /* ubi_btRemove */
//большое левое TreeNode* AVLTree::RLrotate(TreeNode* a) { TreeNode* b = a->right; TreeNode* c = b->left; TreeNode* m = c->left; TreeNode* n = c->right; SwapNodes(a, c); c->left = a; c->right = b; b->parent = c; a->right = m; b->left = n; if (n != NULL) n->parent = b; if (m != NULL) m->parent = a; CorrectRotationBalance(a, b, c); return c; }
TreeNode* AVLTree::Rrotate(TreeNode* a) { TreeNode* b = a->left; TreeNode* c = b->right; SwapNodes(a, b); b->right = a; a->left = c; // устанавливаем родителя c if (c != NULL) c->parent = a; // меняем показатель сбалансированности //если С == L if (b->balance == 0) { a->balance = 1; b->balance = -1; } //если C < L else a->balance = b->balance = 0; return b; }