1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
|
/*-----------------------------------------------------------------------
File : clb_ptrees.c
Author: Stephan Schulz
Contents
Functions for pointer storing SPLAY trees.
Copyright 1998-2019 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Created: Wed Dec 17 21:17:34 MET 1997
-----------------------------------------------------------------------*/
#include "clb_ptrees.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: splay_tree()
//
// Perform the splay operation on tree at node with key.
//
// Global Variables: -
//
// Side Effects : Changes tree
//
/----------------------------------------------------------------------*/
static PTree_p splay_ptree(PTree_p tree, void* key)
{
PTree_p left, right, tmp;
PTreeCell newnode;
if (!tree)
{
return tree;
}
newnode.lson = NULL;
newnode.rson = NULL;
left = &newnode;
right = &newnode;
for (;;)
{
if(PLesser(key, tree->key))
{
if(!tree->lson)
{
break;
}
if(PLesser(key, tree->lson->key))
{
tmp = tree->lson;
tree->lson = tmp->rson;
tmp->rson = tree;
tree = tmp;
if (!tree->lson)
{
break;
}
}
right->lson = tree;
right = tree;
tree = tree->lson;
}
else if(PGreater(key, tree->key))
{
if (!tree->rson)
{
break;
}
if(PGreater(key, tree->rson->key))
{
tmp = tree->rson;
tree->rson = tmp->lson;
tmp->lson = tree;
tree = tmp;
if (!tree->rson)
{
break;
}
}
left->rson = tree;
left = tree;
tree = tree->rson;
}
else
{
break;
}
}
left->rson = tree->lson;
right->lson = tree->rson;
tree->lson = newnode.rson;
tree->rson = newnode.lson;
return tree;
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: PTreeCellAllocEmpty()
//
// Allocate a empty, initialized PTreeCell. Pointers to children
// are NULL, int values are 0 (and pointer values in ANSI-World
// undefined, in practice NULL on 32 bit machines)(This comment is
// superfluous!). The balance field is (correctly) set to 0.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
PTree_p PTreeCellAllocEmpty(void)
{
PTree_p handle = PTreeCellAlloc();
handle->lson = handle->rson = NULL;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeFree()
//
// Free a PTree (including the keys, but not potential objects
// pointed to in the val fields
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void PTreeFree(PTree_p junk)
{
if(junk)
{
PStack_p stack = PStackAlloc();
PStackPushP(stack, junk);
while(!PStackEmpty(stack))
{
junk = PStackPopP(stack);
if(junk->lson)
{
PStackPushP(stack, junk->lson);
}
if(junk->rson)
{
PStackPushP(stack, junk->rson);
}
PTreeCellFree(junk);
}
PStackFree(stack);
}
}
/*-----------------------------------------------------------------------
//
// Function: PTreeInsert()
//
// If an entry with key *newnode->key exists in the tree return a
// pointer to it. Otherwise insert *newnode in the tree and return
// NULL. Will splay the tree!
//
// Global Variables: -
//
// Side Effects : Changes the tree.
//
/----------------------------------------------------------------------*/
PTree_p PTreeInsert(PTree_p *root, PTree_p newnode)
{
if(!*root)
{
newnode->lson = newnode->rson = NULL;
*root = newnode;
return NULL;
}
*root = splay_ptree(*root, newnode->key);
if(PLesser(newnode->key, (*root)->key))
{
newnode->lson = (*root)->lson;
newnode->rson = *root;
(*root)->lson = NULL;
*root = newnode;
return NULL;
}
else if(PGreater(newnode->key, (*root)->key))
{
newnode->rson = (*root)->rson;
newnode->lson = *root;
(*root)->rson = NULL;
*root = newnode;
return NULL;
}
return *root;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeStore()
//
// Insert a cell with given key into the tree. Return false if an
// entry for this key exists, true otherwise.
//
// Global Variables: -
//
// Side Effects : Changes tree
//
/----------------------------------------------------------------------*/
bool PTreeStore(PTree_p *root, void* key)
{
PTree_p handle, newnode;
handle = PTreeCellAlloc();
//printf("\nPTreeStore: %p\n", handle);
handle->key = key;
newnode = PTreeInsert(root, handle);
//printf("\nNewnode, handle: %p, %p\n", newnode, handle);
if(newnode)
{
PTreeCellFree(handle);
return false;
}
return true;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeFind()
//
// Find the entry with key key in the tree and return it. Return
// NULL if no such key exists.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
PTree_p PTreeFind(PTree_p *root, void* key)
{
if(*root)
{
*root = splay_ptree(*root, key);
if(PEqual((*root)->key, key))
{
return *root;
}
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeFindBinary()
//
// Find an entry by simple binary search. This does not reorganize
// the tree, otherwise it is inferior to PTreeFind()!
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
PTree_p PTreeFindBinary(PTree_p root, void* key)
{
while(root)
{
if(PLesser(key, root->key))
{
root = root->lson;
}
else if(PGreater(key, root->key))
{
root = root->rson;
}
else
{
break;
}
}
return root;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeExtractEntry()
//
// Find the entry with key key, remove it from the tree, rebalance
// the tree, and return the pointer to the removed element. Return
// NULL if no matching element exists.
//
//
// Global Variables: -
//
// Side Effects : Changes the tree
//
/----------------------------------------------------------------------*/
PTree_p PTreeExtractEntry(PTree_p *root, void* key)
{
PTree_p x, cell;
if (!(*root))
{
return NULL;
}
*root = splay_ptree(*root, key);
if(PEqual(key, (*root)->key))
{
if (!(*root)->lson)
{
x = (*root)->rson;
}
else
{
x = splay_ptree((*root)->lson, key);
x->rson = (*root)->rson;
}
cell = *root;
cell->lson = cell->rson = NULL;
*root = x;
return cell;
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeExtractKey()
//
// Extract the entry with key key, delete the PTree-Node and return
// the key.
//
// Global Variables: -
//
// Side Effects : Memory operations, by PTreeExtractEntry()
//
/----------------------------------------------------------------------*/
void* PTreeExtractKey(PTree_p *root, void* key)
{
PTree_p handle;
void* res = NULL;
handle = PTreeExtractEntry(root, key);
if(handle)
{
res = handle->key;
PTreeCellFree(handle);
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeExtractRootKey()
//
// Extract the root node of the tree, delete it and return the
// key. Return NULL if the tree is empty.
//
// Global Variables: -
//
// Side Effects : Changes tree
//
/----------------------------------------------------------------------*/
void* PTreeExtractRootKey(PTree_p *root)
{
if(*root)
{
return PTreeExtractKey(root, (*root)->key);
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeDeleteEntry()
//
// Delete the entry with key key from the tree. Return true, if the
// key existed, false otherwise.
//
// Global Variables: -
//
// Side Effects : By PTreeExtract(), memory operations
//
/----------------------------------------------------------------------*/
bool PTreeDeleteEntry(PTree_p *root, void* key)
{
PTree_p cell;
cell = PTreeExtractEntry(root, key);
if(cell)
{
PTreeCellFree(cell);
return true;
}
return false;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeMerge()
//
// Merge the two trees, i.e. destroy the second one and add its
// elements to the first one. Return true if *root gains a new
// element.
//
// Global Variables: -
//
// Side Effects : Changes both trees.
//
/----------------------------------------------------------------------*/
bool PTreeMerge(PTree_p *root, PTree_p add)
{
PStack_p stack = PStackAlloc();
PTree_p tmp;
bool res = false;
//printf("Allocated %p\n", stack);
PStackPushP(stack, add);
while(!PStackEmpty(stack))
{
add = PStackPopP(stack);
if(add)
{
PStackPushP(stack, add->lson);
PStackPushP(stack, add->rson);
tmp = PTreeInsert(root, add);
if(tmp)
{
PTreeCellFree(add);
}
else
{
res = true;
}
}
}
//printf("Freeing %p\n", stack);
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeInsertTree()
//
// Insert the elements stored in add into *root. The tree at add
// remains unchanged.
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
void PTreeInsertTree(PTree_p *root, PTree_p add)
{
PStack_p stack = PStackAlloc();
PStackPushP(stack, add);
while(!PStackEmpty(stack))
{
add = PStackPopP(stack);
if(add)
{
PStackPushP(stack, add->lson);
PStackPushP(stack, add->rson);
PTreeStore(root, add->key);
}
}
PStackFree(stack);
}
/*-----------------------------------------------------------------------
//
// Function: PTreeNodes()
//
// Return the number of nodes in the tree.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long PTreeNodes(PTree_p root)
{
PStack_p stack = PStackAlloc();
long res = 0;
PStackPushP(stack, root);
while(!PStackEmpty(stack))
{
root = PStackPopP(stack);
if(root)
{
PStackPushP(stack, root->lson);
PStackPushP(stack, root->rson);
res++;
}
}
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeDebugPrint()
//
// Print the keys stored in the tree. Returns number of nodes (why
// not ?).
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
long PTreeDebugPrint(FILE* out, PTree_p root)
{
PStack_p stack = PStackAlloc();
long res = 0;
PStackPushP(stack, root);
while(!PStackEmpty(stack))
{
root = PStackPopP(stack);
if(root)
{
if(res % 10 == 0)
{
fprintf(out, "\n#");
}
fprintf(out, " %7p", root->key);
PStackPushP(stack, root->lson);
PStackPushP(stack, root->rson);
res++;
}
}
PStackFree(stack);
fprintf(out, "\n");
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PStackToPTree()
//
// Interprete a stack as a list of pointers and insert these
// pointers into the tree at *root. Returns number of new elements
// in the tree.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long PStackToPTree(PTree_p *root, PStack_p stack)
{
PStackPointer i;
long res = 0;
for(i=0; i<PStackGetSP(stack); i++)
{
if(PTreeStore(root, PStackElementP(stack,i)))
{
res++;
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeToPStack()
//
// Push all the keys in the tree onto the stack (in arbitrary
// order). Return number of values pushed.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long PTreeToPStack(PStack_p target_stack, PTree_p root)
{
long res = 0;
PStack_p stack = PStackAlloc();
PTree_p handle;
PStackPushP(stack, root);
while(!PStackEmpty(stack))
{
handle = PStackPopP(stack);
if(handle)
{
PStackPushP(target_stack, handle->key);
res++;
PStackPushP(stack,handle->lson);
PStackPushP(stack,handle->rson);
}
}
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeSharedElement()
//
// If there exists an element common in both trees, return the first
// one found. Otherwise return NULL. This iterates over the elements
// of the second tree and searches in the first, so make the second
// one smaller if you have a choice.
//
// Global Variables: -
//
// Side Effects : Reorganizes tree1!
//
/----------------------------------------------------------------------*/
void* PTreeSharedElement(PTree_p *tree1, PTree_p tree2)
{
PStack_p stack = PStackAlloc();
PTree_p handle, tmp;
void* res = NULL;
PStackPushP(stack, tree2);
while(!PStackEmpty(stack))
{
handle = PStackPopP(stack);
if(handle)
{
tmp = PTreeFind(tree1, handle->key);
if(tmp)
{
res = tmp->key;
break;
}
PStackPushP(stack,handle->lson);
PStackPushP(stack,handle->rson);
}
}
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeIntersection()
//
// Compute the intersection of the two PTrees and return it.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
PTree_p PTreeIntersection(PTree_p tree1, PTree_p tree2)
{
PStack_p stack = PStackAlloc();
PTree_p handle, tmp, res=NULL;
PStackPushP(stack, tree2);
while(!PStackEmpty(stack))
{
handle = PStackPopP(stack);
if(handle)
{
tmp = PTreeFindBinary(tree1, handle->key);
if(tmp)
{
PTreeStore(&res, handle->key);
}
PStackPushP(stack,handle->lson);
PStackPushP(stack,handle->rson);
}
}
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeIntersection()
//
// Make tree1 the intersection of tree1 and tree2. Does not change
// tree 2. Return the number of keys removed.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
long PTreeDestrIntersection(PTree_p *tree1, PTree_p tree2)
{
PTree_p tmp = NULL;
void* key;
long res = 0;
while((key = PTreeExtractRootKey(tree1)))
{
if(PTreeFindBinary(tree2, key))
{
PTreeStore(&tmp, key);
}
else
{
res++;
}
}
assert(!(*tree1));
*tree1 = tmp;
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeCopy()
//
// Return a Ptree that stores the same elements as tree.
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
PTree_p PTreeCopy(PTree_p tree)
{
PTree_p res = NULL, handle;
PStack_p stack = PStackAlloc();
PStackPushP(stack, tree);
while(!PStackEmpty(stack))
{
handle = PStackPopP(stack);
if(handle)
{
PTreeStore(&res, handle->key);
PStackPushP(stack,handle->lson);
PStackPushP(stack,handle->rson);
}
}
PStackFree(stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeEquiv()
//
// Determin if two PTrees contain the same pointers.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
bool PTreeEquiv(PTree_p t1, PTree_p t2)
{
bool res = true;
PStack_p iter1 = PTreeTraverseInit(t1);
PStack_p iter2 = PTreeTraverseInit(t2);
PTree_p handle1, handle2;
while((handle1 = PTreeTraverseNext(iter1)))
{
handle2 = PTreeTraverseNext(iter2);
if(!handle2 || (handle1->key != handle2->key))
{
res = false;
break;
}
}
if(!handle1)
{
handle2= PTreeTraverseNext(iter2);
if(handle2)
{
res = false;
}
}
PTreeTraverseExit(iter2);
PTreeTraverseExit(iter1);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeIsSubset()
//
// Determine if pointers stored in sub are a subset of pointers
// stored in super.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
bool PTreeIsSubset(PTree_p sub, PTree_p *super)
{
bool res = true;
PStack_p iter = PTreeTraverseInit(sub);
PTree_p handle;
while((handle = PTreeTraverseNext(iter)))
{
if(!PTreeFind(super, handle->key))
{
res = false;
break;
}
}
PTreeTraverseExit(iter);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PTreeVisitInOrder()
//
// Apply function visitor to every key stored in PTree t. Nodes will be
// visited as in inorder traversal. "arg" is an additional (first)
// arg to the visitor function.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void PTreeVisitInOrder(PTree_p t, void (*visitor)(void*, void*), void* arg)
{
PStack_p iter = PTreeTraverseInit(t);
PTree_p handle = NULL;
while((handle = PTreeTraverseNext(iter)))
{
visitor(arg, handle->key);
}
PTreeTraverseExit(iter);
}
AVL_TRAVERSE_DEFINITION(PTree, PTree_p)
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|