home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / incl / f_heap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.5 KB  |  149 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  f_heap.h
  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.  
  16.  
  17. #ifndef FHEAP_H
  18. #define FHEAP_H
  19.  
  20. //------------------------------------------------------------------------------
  21. //
  22. // Fibonacci Heap
  23. //
  24. // Michael Muth  (1988)
  25. //
  26. // Modifications
  27. //
  28. // - virtual compare function  (Stefan Naeher, August 1989)
  29. //
  30. // - virtual clear functions   (Stefan Naeher, April 1990)
  31. //
  32. //------------------------------------------------------------------------------
  33.  
  34.  
  35. #include <LEDA/basic.h>
  36.  
  37.  
  38. class f_heap_node  {
  39. // repraesentiert Knoten und Heap geordnete Baeume
  40.  
  41. friend class f_heap;
  42.  
  43.    f_heap_node* next;          // used to link all used items 
  44.    int deleted;                // to mark deleted nodes
  45.  
  46.    f_heap_node* left;          // left and right siblings (circular List)
  47.    f_heap_node* right;   
  48.    f_heap_node* parent;        // parent node
  49.    f_heap_node* children;      // a child
  50.  
  51.    int rank;                   // number of children
  52.    int mark;                   // ( 1=true, 0=false )
  53.    ent key;                    // key
  54.    ent inf;                    // information
  55.  
  56.  
  57. // size: 9 words =  36 bytes
  58.  
  59.  
  60.    void link(f_heap_node*);
  61.    void cut(f_heap_node*);
  62.  
  63.  
  64. public:
  65.  
  66.    f_heap_node(ent k, ent info, f_heap_node* n) 
  67.    {  key = k;
  68.       inf = info;
  69.       next = n;
  70.       deleted = 0;
  71.       rank = 0;
  72.       mark = 0;
  73.       parent = 0;
  74.       children = 0;
  75.     }  // end of f_heap_node()
  76.  
  77. OPERATOR_NEW(10)
  78. OPERATOR_DEL(10)
  79.  
  80.  };  //f_heap_node
  81.  
  82.  
  83. typedef f_heap_node* f_heap_item;
  84.  
  85.  
  86. class f_heap
  87.  /* Ein F-Heap enthaelt eine zirkulaere Liste von Heap geordneten
  88.     Baeumen. Der Einstieg erfolgt ueber den minptr, einen Zeiger
  89.     auf den Baum mit kleinstem Schluessel in der Wurzel. Die ei-
  90.     gentlichen Items ( Paare aus /NxT ) sind in den Knoten der
  91.     Baeume enthalten.                                               */
  92.  { 
  93.    int node_count;         // number of nodes
  94.    f_heap_node* minptr;    // entry to the List of roots
  95.    f_heap_node* node_list; // List of all nodes
  96.  
  97.    f_heap_node** rank_field;
  98.  
  99.    int max_rank() const;
  100.  
  101.    f_heap_node* new_f_heap_node(ent k, ent i)
  102.    { return (node_list = new f_heap_node(k,i,node_list)); }
  103.  
  104.    virtual int cmp(ent& x, ent& y) const { return int(x)-int(y); }
  105.  
  106.    virtual void print_key(ent& x)  const { Print(x); }
  107.    virtual void print_inf(ent& x)  const { Print(x); }
  108.  
  109.    virtual void clear_key(ent& x)  const { Clear(x); }
  110.    virtual void clear_inf(ent& x)  const { Clear(x); }
  111.  
  112.    virtual void copy_key(ent& x)   const { Copy(x); }
  113.    virtual void copy_inf(ent& x)   const { Copy(x); }
  114.  
  115. public:
  116.  
  117.  f_heap();
  118.  f_heap(const f_heap&);
  119.  f_heap& operator=(const f_heap&);
  120. ~f_heap()  { clear(); delete rank_field; }
  121.  
  122.  
  123. f_heap_node* insert(ent, ent);
  124. f_heap_node* find_min() const { return(minptr); }
  125.  
  126. void del_min();  
  127. void decrease_key(f_heap_node*,ent);
  128. void change_inf(f_heap_node*,ent);
  129. void del_item(f_heap_node *x){ decrease_key(x,minptr->key); del_min();}
  130. void clear();
  131.  
  132. ent  key(f_heap_node *x) const { return x->key ; }
  133. ent  inf(f_heap_node *x) const { return x->inf; }
  134.  
  135.   
  136. int  size()  const { return node_count; }
  137. int  empty() const { return (find_min()==0) ? true : false; }
  138.  
  139.  
  140. // Iteration:
  141. f_heap_node* first_item() const;
  142. f_heap_node* next_item(f_heap_node*) const;
  143.  
  144.  
  145.  };  // end of class f_heap
  146.  
  147. #endif
  148.