[Home]
[Contents]
[Chapter]
[Previous Algorithm]
[Next Algorithm]


Pagodas merging (Pascal version available)


tree merge( a, b ) tree a, b; { tree bota, botb, r, temp; if ( a==NULL ) return( b ); else if ( b==NULL ) return( a ); else { /*** Find bottom of a's rightmost edge ***/ bota = a->right; a->right = NULL; /*** bottom of b's leftmost edge ***/ botb = b->left; b->left = NULL; r = NULL; /*** Merging loop ***/ while ( bota!=NULL && botb!=NULL ) if ( bota->k < botb->k ) { temp = bota->right; if ( r==NULL ) bota->right = bota; else {bota->right = r->right; r->right = bota; }; r = bota; bota = temp; } else {temp = botb->left; if ( r==NULL ) botb->left = botb; else {botb->left = r->left; r->left = botb; }; r = botb; botb = temp; }; /*** one edge is exhausted, finish merge ***/ if ( botb==NULL ) { a->right = r->right; r->right = bota; return( a ); } else {b->left = r->left; r->left = botb; return( b ); } } };

C source (515.merg.c) Pascal source (515.merg.p)



© Addison-Wesley Publishing Co. Inc.