- Timestamp:
- Nov 10, 2009 12:01:38 AM (15 years ago)
- Location:
- trunk/include/k/kRbTmpl
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/k/kRbTmpl/kRbBase.h
r35 r38 147 147 * Defined Constants And Macros * 148 148 *******************************************************************************/ 149 #define KAVL_HEIGHTOF(pNode) ((KU8)((pNode) != NULL ? (pNode)->mHeight : 0))150 151 149 /** @def KRB_GET_POINTER 152 150 * Reads a 'pointer' value. … … 262 260 263 261 262 /** Is the node red or black? 263 * @returns true / false 264 * @param pNode Pointer to the node in question. 265 * @remarks All NULL pointers are considered black leaf nodes. 266 */ 267 #define KRB_IS_RED(pNode) ( (pNode) != NULL && (pNode)->mfIsRed ) 268 264 269 265 270 /******************************************************************************* … … 315 320 316 321 /** 322 * Rotates the tree to the left (shift direction) and recolors the nodes. 323 * 324 * @pre 325 * 326 * 2 4 327 * / \ / \ 328 * 1 4 ==> 2 5 329 * / \ / \ 330 * 3 5 1 3 331 * 332 * @endpre 333 * 334 * @returns The new root node. 335 * @param pRoot The root node. 336 * 337 * @remarks This will not update any pointer <tt>to</tt> the root node! 338 */ 339 K_DECL_INLINE(KRBNODE *) KAVL_FN(RotateLeft)(KRBNODE *pRoot) 340 { 341 KRBNODE *pNewRoot = pRoot->mRight; 342 pRoot->mRight = pNewRoot->mLeft; 343 pNewRoot->mLeft = pRoot; 344 345 pRoot->mfIsRed = 1; 346 pNewRoot->mfIsRed = 0; 347 return pNewRoot; 348 } 349 350 351 /** 352 * Rotates the tree to the right (shift direction) and recolors the nodes. 353 * 354 * @pre 355 * 356 * 4 2 357 * / \ / \ 358 * 2 5 ==> 1 4 359 * / \ / \ 360 * 1 3 3 5 361 * 362 * @endpre 363 * 364 * @returns The new root node. 365 * @param pRoot The root node. 366 * 367 * @remarks This will not update any pointer <tt>to</tt> the root node! 368 */ 369 K_DECL_INLINE(KRBNODE *) KAVL_FN(RotateRight)(KRBNODE *pRoot) 370 { 371 KRBNODE *pNewRoot = pRoot->mLeft; 372 pRoot->mLeft = pNewRoot->mRight; 373 pNewRoot->mRight = pRoot; 374 375 pRoot->mfIsRed = 1; 376 pNewRoot->mfIsRed = 0; 377 return pNewRoot; 378 } 379 380 381 /** 382 * Performs a double left rotation with recoloring. 383 * 384 * @pre 385 * 386 * 2 2 4 387 * / \ / \ / \ 388 * 1 6 ==> 1 4 ==> 2 6 389 * / \ / \ / \ / \ 390 * 4 7 3 6 1 3 5 7 391 * / \ / \ 392 * 3 5 5 7 393 * @endpre 394 * 395 * @returns The new root node. 396 * @param pRoot The root node. 397 * 398 * @remarks This will not update any pointer <tt>to</tt> the root node! 399 */ 400 K_DECL_INLINE(KRBNODE *) KAVL_FN(DoubleRotateLeft)(KRBNODE *pRoot) 401 { 402 pRoot->mRight = KAVL_FN(RotateRight)(pRoot->mRight); 403 return KAVL_FN(RotateLeft)(pRoot); 404 } 405 406 407 /** 408 * Performs a double right rotation with recoloring. 409 * 410 * @pre 411 * 6 6 4 412 * / \ / \ / \ 413 * 2 7 4 7 2 6 414 * / \ ==> / \ ==> / \ / \ 415 * 1 4 2 5 1 3 5 7 416 * / \ / \ 417 * 3 5 1 3 418 * 419 * @endpre 420 * 421 * @returns The new root node. 422 * @param pRoot The root node. 423 * 424 * @remarks This will not update any pointer <tt>to</tt> the root node! 425 */ 426 K_DECL_INLINE(KRBNODE *) KAVL_FN(DoubleRotateRight)(KRBNODE *pRoot) 427 { 428 pRoot->mLeft = KAVL_FN(RotateLeft)(pRoot->mLeft); 429 return KAVL_FN(RotateRight)(pRoot); 430 } 431 432 433 /** 317 434 * Inserts a node into the Red-Black tree. 318 435 * @returns K_TRUE if inserted. … … 320 437 * @param pRoot Pointer to the Red-Back tree's root structure. 321 438 * @param pNode Pointer to the node which is to be added. 322 * @sketch Find the location of the node (using binary tree algorithm.):323 * LOOP until NULL leaf pointer324 * BEGIN325 * Add node pointer pointer to the AVL-stack.326 * IF new-node-key < node key THEN327 * left328 * ELSE329 * right330 * END331 * Fill in leaf node and insert it.332 * Rebalance the tree.333 439 */ 334 440 KRB_DECL(KBOOL) KRB_FN(Insert)(KRBROOT *pRoot, KRBNODE *pNode) 335 441 { 336 KRB_INT(STACK) Stack;337 442 KRBTREEPTR *ppCurNode = &pRoot->mpRoot; 338 register KRBKEY Key = pNode->mKey;443 register KRBKEY Key = pNode->mKey; 339 444 #ifdef KRB_RANGE 340 register KRBKEY KeyLast = pNode->mKeyLast;445 register KRBKEY KeyLast = pNode->mKeyLast; 341 446 #endif 342 447 343 448 #ifdef KRB_RANGE 344 449 if (Key > KeyLast) 345 return false;450 return K_FALSE; 346 451 #endif 347 452
Note:
See TracChangeset
for help on using the changeset viewer.