home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _b_heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.0 KB  |  121 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _b_heap.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/b_heap.h>
  16.  
  17. b_heap::b_heap(int a, int b) : T(a,b)
  18. { if (b<a) error_handler(1,"constructor: illegal size\n");
  19.   low = a;
  20.   high = b;
  21.   min = high+1;
  22.   max = low-1;
  23.   int i;
  24.   for(i=a;i<=b;i++) T[i] = new list(b_heap_item);
  25. }
  26.  
  27. void b_heap::clear()
  28. { int i;
  29.   for (i=low;i<=high;i++) T[i]->clear();
  30.   min = high+1;
  31.   max = low-1;
  32.  
  33. b_heap_item b_heap::insert(int key, ent info) 
  34. { if (key<low || key>high) 
  35.   error_handler(1,string("insert: illegal key %d\n",key));
  36.   if (key<min) min = key;
  37.   if (key>max) max = key;
  38.   b_heap_item it = new  b_heap_node(key,info);
  39.   it->loc = T[key]->append(it); 
  40.   return it;
  41. }
  42.  
  43. b_heap_item b_heap::find_min()
  44. { if (min>high) return 0;
  45.   return T[min]->head();
  46. }
  47.  
  48. b_heap_item b_heap::find_max()
  49. { if (max<low) return 0;
  50.   return T[max]->head();
  51. }
  52.  
  53. ent b_heap::del_min()
  54. { if (min>high) 
  55.        error_handler(1,"b_heap del_min: heap is empty");
  56.   b_heap_item p = T[min]->pop();
  57.   ent res = p->info;
  58.   delete p;
  59.   while ((min <= high) && (T[min]->empty())) min++;
  60.   if (min>high) max = low-1;
  61.   return res;
  62. }
  63.  
  64. ent b_heap::del_max()
  65. { if (max<0) error_handler(1,"b_heap del_max: heap is empty");
  66.   b_heap_item p = T[max]->pop();
  67.   ent res = p->info;
  68.   delete p;
  69.   while ((max>=low) && (T[max]->empty())) max--;
  70.   if (max<low) min = high+1;
  71.   return res;
  72. }
  73.  
  74. b_heap_item b_heap::decrease_key(b_heap_item it, int k)
  75. { if (it==0) error_handler(1,"decrease_key: item = nil\n");
  76.   if (k<low || k>high) error_handler(1,"decrease_key: illegal key\n");
  77.   if (it->loc==0) error_handler(1,"decrease_key: item not found\n");
  78.   T[it->key]->del(it->loc);
  79.   while ((max >= low) && (T[max]->empty())) max--;
  80.   it->key = k;
  81.   it->loc = T[k]->append(it); 
  82.   if (k<min) min = k;
  83.   return it;
  84. }
  85.  
  86. b_heap_item b_heap::increase_key(b_heap_item it, int k)
  87. { if (it==0) error_handler(1,"increase_key: item = nil\n");
  88.   if (k<low || k>high) error_handler(1,"increase_key:illegal key\n");
  89.   if (it->loc==0) error_handler(1,"increase_key: item not found\n");
  90.   T[it->key]->del(it->loc);
  91.   while ((min <= high) && (T[min]->empty())) min++;
  92.   it->key = k;
  93.   it->loc = T[k]->append(it); 
  94.   if (k>max) max = k;
  95.   return it;
  96. }
  97.  
  98.  
  99. void b_heap::delete_item(b_heap_item it) 
  100. { if (it==0) error_handler(1,"delete_item: item = nil\n");
  101.   if (it->loc==0) error_handler(1,"delete_item: item not found\n");
  102.   T[it->key]->del(it->loc); 
  103.   while ((min <= high) && (T[min]->empty())) min++;
  104.   while ((max >= low) && (T[max]->empty())) max--;
  105.   delete it;
  106. }
  107.  
  108. /* TEST */
  109.  
  110. void b_heap::print()
  111. { int i;
  112.   for (i=low;i<=high;i++) { cout << i << ": ";
  113.                             T[i]->print();
  114.                             cout << "\n";
  115.                            }
  116. }
  117.  
  118.  
  119.