lemon/binom_heap.h
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 20 Jul 2009 19:06:39 +0200
changeset 706 9314d9339475
parent 701 d1a9224f1e30
child 707 3887d6f994d7
permissions -rw-r--r--
Smarter bubbleDown() in K-ary heaps (#301)
kpeter@703
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@701
     2
 *
kpeter@703
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@701
     4
 *
kpeter@703
     5
 * Copyright (C) 2003-2009
kpeter@701
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@701
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@701
     8
 *
kpeter@701
     9
 * Permission to use, modify and distribute this software is granted
kpeter@701
    10
 * provided that this copyright notice appears in all copies. For
kpeter@701
    11
 * precise terms see the accompanying LICENSE file.
kpeter@701
    12
 *
kpeter@701
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@701
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@701
    15
 * purpose.
kpeter@701
    16
 *
kpeter@701
    17
 */
kpeter@701
    18
kpeter@701
    19
#ifndef LEMON_BINOM_HEAP_H
kpeter@701
    20
#define LEMON_BINOM_HEAP_H
kpeter@701
    21
kpeter@701
    22
///\file
kpeter@703
    23
///\ingroup heaps
kpeter@701
    24
///\brief Binomial Heap implementation.
kpeter@701
    25
kpeter@701
    26
#include <vector>
kpeter@703
    27
#include <utility>
kpeter@701
    28
#include <functional>
kpeter@701
    29
#include <lemon/math.h>
kpeter@701
    30
#include <lemon/counter.h>
kpeter@701
    31
kpeter@701
    32
namespace lemon {
kpeter@701
    33
kpeter@703
    34
  /// \ingroup heaps
kpeter@701
    35
  ///
kpeter@703
    36
  ///\brief Binomial heap data structure.
kpeter@701
    37
  ///
kpeter@703
    38
  /// This class implements the \e binomial \e heap data structure.
kpeter@703
    39
  /// It fully conforms to the \ref concepts::Heap "heap concept".
kpeter@701
    40
  ///
kpeter@703
    41
  /// The methods \ref increase() and \ref erase() are not efficient
kpeter@703
    42
  /// in a binomial heap. In case of many calls of these operations,
kpeter@703
    43
  /// it is better to use other heap structure, e.g. \ref BinHeap
kpeter@703
    44
  /// "binary heap".
kpeter@701
    45
  ///
kpeter@703
    46
  /// \tparam PR Type of the priorities of the items.
kpeter@703
    47
  /// \tparam IM A read-writable item map with \c int values, used
kpeter@703
    48
  /// internally to handle the cross references.
kpeter@703
    49
  /// \tparam CMP A functor class for comparing the priorities.
kpeter@703
    50
  /// The default is \c std::less<PR>.
kpeter@701
    51
#ifdef DOXYGEN
kpeter@703
    52
  template <typename PR, typename IM, typename CMP>
kpeter@701
    53
#else
kpeter@703
    54
  template <typename PR, typename IM, typename CMP = std::less<PR> >
kpeter@701
    55
#endif
kpeter@701
    56
  class BinomHeap {
kpeter@701
    57
  public:
kpeter@703
    58
    /// Type of the item-int map.
kpeter@703
    59
    typedef IM ItemIntMap;
kpeter@703
    60
    /// Type of the priorities.
kpeter@703
    61
    typedef PR Prio;
kpeter@703
    62
    /// Type of the items stored in the heap.
kpeter@701
    63
    typedef typename ItemIntMap::Key Item;
kpeter@703
    64
    /// Functor type for comparing the priorities.
kpeter@703
    65
    typedef CMP Compare;
kpeter@703
    66
kpeter@703
    67
    /// \brief Type to represent the states of the items.
kpeter@703
    68
    ///
kpeter@703
    69
    /// Each item has a state associated to it. It can be "in heap",
kpeter@703
    70
    /// "pre-heap" or "post-heap". The latter two are indifferent from the
kpeter@703
    71
    /// heap's point of view, but may be useful to the user.
kpeter@703
    72
    ///
kpeter@703
    73
    /// The item-int map must be initialized in such way that it assigns
kpeter@703
    74
    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
kpeter@703
    75
    enum State {
kpeter@703
    76
      IN_HEAP = 0,    ///< = 0.
kpeter@703
    77
      PRE_HEAP = -1,  ///< = -1.
kpeter@703
    78
      POST_HEAP = -2  ///< = -2.
kpeter@703
    79
    };
kpeter@701
    80
kpeter@701
    81
  private:
kpeter@701
    82
    class store;
kpeter@701
    83
kpeter@703
    84
    std::vector<store> _data;
kpeter@703
    85
    int _min, _head;
kpeter@703
    86
    ItemIntMap &_iim;
kpeter@703
    87
    Compare _comp;
kpeter@703
    88
    int _num_items;
kpeter@701
    89
kpeter@701
    90
  public:
kpeter@703
    91
    /// \brief Constructor.
kpeter@703
    92
    ///
kpeter@703
    93
    /// Constructor.
kpeter@703
    94
    /// \param map A map that assigns \c int values to the items.
kpeter@703
    95
    /// It is used internally to handle the cross references.
kpeter@703
    96
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
kpeter@703
    97
    explicit BinomHeap(ItemIntMap &map)
kpeter@703
    98
      : _min(0), _head(-1), _iim(map), _num_items(0) {}
kpeter@701
    99
kpeter@703
   100
    /// \brief Constructor.
kpeter@701
   101
    ///
kpeter@703
   102
    /// Constructor.
kpeter@703
   103
    /// \param map A map that assigns \c int values to the items.
kpeter@703
   104
    /// It is used internally to handle the cross references.
kpeter@703
   105
    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
kpeter@703
   106
    /// \param comp The function object used for comparing the priorities.
kpeter@703
   107
    BinomHeap(ItemIntMap &map, const Compare &comp)
kpeter@703
   108
      : _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
kpeter@701
   109
kpeter@701
   110
    /// \brief The number of items stored in the heap.
kpeter@701
   111
    ///
kpeter@703
   112
    /// This function returns the number of items stored in the heap.
kpeter@703
   113
    int size() const { return _num_items; }
kpeter@701
   114
kpeter@703
   115
    /// \brief Check if the heap is empty.
kpeter@701
   116
    ///
kpeter@703
   117
    /// This function returns \c true if the heap is empty.
kpeter@703
   118
    bool empty() const { return _num_items==0; }
kpeter@701
   119
kpeter@703
   120
    /// \brief Make the heap empty.
kpeter@701
   121
    ///
kpeter@703
   122
    /// This functon makes the heap empty.
kpeter@703
   123
    /// It does not change the cross reference map. If you want to reuse
kpeter@703
   124
    /// a heap that is not surely empty, you should first clear it and
kpeter@703
   125
    /// then you should set the cross reference map to \c PRE_HEAP
kpeter@703
   126
    /// for each item.
kpeter@701
   127
    void clear() {
kpeter@703
   128
      _data.clear(); _min=0; _num_items=0; _head=-1;
kpeter@701
   129
    }
kpeter@701
   130
kpeter@703
   131
    /// \brief Set the priority of an item or insert it, if it is
kpeter@703
   132
    /// not stored in the heap.
kpeter@701
   133
    ///
kpeter@703
   134
    /// This method sets the priority of the given item if it is
kpeter@703
   135
    /// already stored in the heap. Otherwise it inserts the given
kpeter@703
   136
    /// item into the heap with the given priority.
kpeter@703
   137
    /// \param item The item.
kpeter@703
   138
    /// \param value The priority.
kpeter@701
   139
    void set (const Item& item, const Prio& value) {
kpeter@703
   140
      int i=_iim[item];
kpeter@703
   141
      if ( i >= 0 && _data[i].in ) {
kpeter@703
   142
        if ( _comp(value, _data[i].prio) ) decrease(item, value);
kpeter@703
   143
        if ( _comp(_data[i].prio, value) ) increase(item, value);
kpeter@701
   144
      } else push(item, value);
kpeter@701
   145
    }
kpeter@701
   146
kpeter@703
   147
    /// \brief Insert an item into the heap with the given priority.
kpeter@701
   148
    ///
kpeter@703
   149
    /// This function inserts the given item into the heap with the
kpeter@703
   150
    /// given priority.
kpeter@703
   151
    /// \param item The item to insert.
kpeter@703
   152
    /// \param value The priority of the item.
kpeter@703
   153
    /// \pre \e item must not be stored in the heap.
kpeter@701
   154
    void push (const Item& item, const Prio& value) {
kpeter@703
   155
      int i=_iim[item];
kpeter@701
   156
      if ( i<0 ) {
kpeter@703
   157
        int s=_data.size();
kpeter@703
   158
        _iim.set( item,s );
kpeter@701
   159
        store st;
kpeter@701
   160
        st.name=item;
kpeter@703
   161
        _data.push_back(st);
kpeter@701
   162
        i=s;
kpeter@701
   163
      }
kpeter@701
   164
      else {
kpeter@703
   165
        _data[i].parent=_data[i].right_neighbor=_data[i].child=-1;
kpeter@703
   166
        _data[i].degree=0;
kpeter@703
   167
        _data[i].in=true;
kpeter@701
   168
      }
kpeter@703
   169
      _data[i].prio=value;
kpeter@701
   170
kpeter@703
   171
      if( 0==_num_items ) { _head=i; _min=i; }
kpeter@701
   172
      else { merge(i); }
kpeter@701
   173
kpeter@703
   174
      _min = findMin();
kpeter@701
   175
kpeter@703
   176
      ++_num_items;
kpeter@701
   177
    }
kpeter@701
   178
kpeter@703
   179
    /// \brief Return the item having minimum priority.
kpeter@701
   180
    ///
kpeter@703
   181
    /// This function returns the item having minimum priority.
kpeter@703
   182
    /// \pre The heap must be non-empty.
kpeter@703
   183
    Item top() const { return _data[_min].name; }
kpeter@701
   184
kpeter@703
   185
    /// \brief The minimum priority.
kpeter@701
   186
    ///
kpeter@703
   187
    /// This function returns the minimum priority.
kpeter@703
   188
    /// \pre The heap must be non-empty.
kpeter@703
   189
    Prio prio() const { return _data[_min].prio; }
kpeter@701
   190
kpeter@703
   191
    /// \brief The priority of the given item.
kpeter@701
   192
    ///
kpeter@703
   193
    /// This function returns the priority of the given item.
kpeter@703
   194
    /// \param item The item.
kpeter@703
   195
    /// \pre \e item must be in the heap.
kpeter@701
   196
    const Prio& operator[](const Item& item) const {
kpeter@703
   197
      return _data[_iim[item]].prio;
kpeter@701
   198
    }
kpeter@701
   199
kpeter@703
   200
    /// \brief Remove the item having minimum priority.
kpeter@701
   201
    ///
kpeter@703
   202
    /// This function removes the item having minimum priority.
kpeter@701
   203
    /// \pre The heap must be non-empty.
kpeter@701
   204
    void pop() {
kpeter@703
   205
      _data[_min].in=false;
kpeter@701
   206
kpeter@701
   207
      int head_child=-1;
kpeter@703
   208
      if ( _data[_min].child!=-1 ) {
kpeter@703
   209
        int child=_data[_min].child;
kpeter@701
   210
        int neighb;
kpeter@701
   211
        int prev=-1;
kpeter@701
   212
        while( child!=-1 ) {
kpeter@703
   213
          neighb=_data[child].right_neighbor;
kpeter@703
   214
          _data[child].parent=-1;
kpeter@703
   215
          _data[child].right_neighbor=prev;
kpeter@701
   216
          head_child=child;
kpeter@701
   217
          prev=child;
kpeter@701
   218
          child=neighb;
kpeter@701
   219
        }
kpeter@701
   220
      }
kpeter@701
   221
kpeter@701
   222
      // The first case is that there are only one root.
kpeter@703
   223
      if ( -1==_data[_head].right_neighbor ) {
kpeter@703
   224
        _head=head_child;
kpeter@701
   225
      }
kpeter@701
   226
      // The case where there are more roots.
kpeter@701
   227
      else {
kpeter@703
   228
        if( _head!=_min )  { unlace(_min); }
kpeter@703
   229
        else { _head=_data[_head].right_neighbor; }
kpeter@701
   230
kpeter@701
   231
        merge(head_child);
kpeter@701
   232
      }
kpeter@703
   233
      _min=findMin();
kpeter@703
   234
      --_num_items;
kpeter@701
   235
    }
kpeter@701
   236
kpeter@703
   237
    /// \brief Remove the given item from the heap.
kpeter@701
   238
    ///
kpeter@703
   239
    /// This function removes the given item from the heap if it is
kpeter@703
   240
    /// already stored.
kpeter@703
   241
    /// \param item The item to delete.
kpeter@703
   242
    /// \pre \e item must be in the heap.
kpeter@701
   243
    void erase (const Item& item) {
kpeter@703
   244
      int i=_iim[item];
kpeter@703
   245
      if ( i >= 0 && _data[i].in ) {
kpeter@703
   246
        decrease( item, _data[_min].prio-1 );
kpeter@701
   247
        pop();
kpeter@701
   248
      }
kpeter@701
   249
    }
kpeter@701
   250
kpeter@703
   251
    /// \brief Decrease the priority of an item to the given value.
kpeter@701
   252
    ///
kpeter@703
   253
    /// This function decreases the priority of an item to the given value.
kpeter@703
   254
    /// \param item The item.
kpeter@703
   255
    /// \param value The priority.
kpeter@703
   256
    /// \pre \e item must be stored in the heap with priority at least \e value.
kpeter@701
   257
    void decrease (Item item, const Prio& value) {
kpeter@703
   258
      int i=_iim[item];
kpeter@701
   259
kpeter@703
   260
      if( _comp( value,_data[i].prio ) ) {
kpeter@703
   261
        _data[i].prio=value;
kpeter@701
   262
kpeter@703
   263
        int p_loc=_data[i].parent, loc=i;
kpeter@701
   264
        int parent, child, neighb;
kpeter@701
   265
kpeter@703
   266
        while( -1!=p_loc && _comp(_data[loc].prio,_data[p_loc].prio) ) {
kpeter@701
   267
kpeter@701
   268
          // parent set for other loc_child
kpeter@703
   269
          child=_data[loc].child;
kpeter@701
   270
          while( -1!=child ) {
kpeter@703
   271
            _data[child].parent=p_loc;
kpeter@703
   272
            child=_data[child].right_neighbor;
kpeter@701
   273
          }
kpeter@701
   274
kpeter@701
   275
          // parent set for other p_loc_child
kpeter@703
   276
          child=_data[p_loc].child;
kpeter@701
   277
          while( -1!=child ) {
kpeter@703
   278
            _data[child].parent=loc;
kpeter@703
   279
            child=_data[child].right_neighbor;
kpeter@701
   280
          }
kpeter@701
   281
kpeter@703
   282
          child=_data[p_loc].child;
kpeter@703
   283
          _data[p_loc].child=_data[loc].child;
kpeter@701
   284
          if( child==loc )
kpeter@701
   285
            child=p_loc;
kpeter@703
   286
          _data[loc].child=child;
kpeter@701
   287
kpeter@701
   288
          // left_neighb set for p_loc
kpeter@703
   289
          if( _data[loc].child!=p_loc ) {
kpeter@703
   290
            while( _data[child].right_neighbor!=loc )
kpeter@703
   291
              child=_data[child].right_neighbor;
kpeter@703
   292
            _data[child].right_neighbor=p_loc;
kpeter@701
   293
          }
kpeter@701
   294
kpeter@701
   295
          // left_neighb set for loc
kpeter@703
   296
          parent=_data[p_loc].parent;
kpeter@703
   297
          if( -1!=parent ) child=_data[parent].child;
kpeter@703
   298
          else child=_head;
kpeter@701
   299
kpeter@701
   300
          if( child!=p_loc ) {
kpeter@703
   301
            while( _data[child].right_neighbor!=p_loc )
kpeter@703
   302
              child=_data[child].right_neighbor;
kpeter@703
   303
            _data[child].right_neighbor=loc;
kpeter@701
   304
          }
kpeter@701
   305
kpeter@703
   306
          neighb=_data[p_loc].right_neighbor;
kpeter@703
   307
          _data[p_loc].right_neighbor=_data[loc].right_neighbor;
kpeter@703
   308
          _data[loc].right_neighbor=neighb;
kpeter@701
   309
kpeter@703
   310
          _data[p_loc].parent=loc;
kpeter@703
   311
          _data[loc].parent=parent;
kpeter@701
   312
kpeter@703
   313
          if( -1!=parent && _data[parent].child==p_loc )
kpeter@703
   314
            _data[parent].child=loc;
kpeter@701
   315
kpeter@701
   316
          /*if new parent will be the first root*/
kpeter@703
   317
          if( _head==p_loc )
kpeter@703
   318
            _head=loc;
kpeter@701
   319
kpeter@703
   320
          p_loc=_data[loc].parent;
kpeter@701
   321
        }
kpeter@701
   322
      }
kpeter@703
   323
      if( _comp(value,_data[_min].prio) ) {
kpeter@703
   324
        _min=i;
kpeter@701
   325
      }
kpeter@701
   326
    }
kpeter@701
   327
kpeter@703
   328
    /// \brief Increase the priority of an item to the given value.
kpeter@701
   329
    ///
kpeter@703
   330
    /// This function increases the priority of an item to the given value.
kpeter@703
   331
    /// \param item The item.
kpeter@703
   332
    /// \param value The priority.
kpeter@703
   333
    /// \pre \e item must be stored in the heap with priority at most \e value.
kpeter@701
   334
    void increase (Item item, const Prio& value) {
kpeter@701
   335
      erase(item);
kpeter@701
   336
      push(item, value);
kpeter@701
   337
    }
kpeter@701
   338
kpeter@703
   339
    /// \brief Return the state of an item.
kpeter@701
   340
    ///
kpeter@703
   341
    /// This method returns \c PRE_HEAP if the given item has never
kpeter@703
   342
    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
kpeter@703
   343
    /// and \c POST_HEAP otherwise.
kpeter@703
   344
    /// In the latter case it is possible that the item will get back
kpeter@703
   345
    /// to the heap again.
kpeter@703
   346
    /// \param item The item.
kpeter@701
   347
    State state(const Item &item) const {
kpeter@703
   348
      int i=_iim[item];
kpeter@701
   349
      if( i>=0 ) {
kpeter@703
   350
        if ( _data[i].in ) i=0;
kpeter@701
   351
        else i=-2;
kpeter@701
   352
      }
kpeter@701
   353
      return State(i);
kpeter@701
   354
    }
kpeter@701
   355
kpeter@703
   356
    /// \brief Set the state of an item in the heap.
kpeter@701
   357
    ///
kpeter@703
   358
    /// This function sets the state of the given item in the heap.
kpeter@703
   359
    /// It can be used to manually clear the heap when it is important
kpeter@703
   360
    /// to achive better time complexity.
kpeter@701
   361
    /// \param i The item.
kpeter@701
   362
    /// \param st The state. It should not be \c IN_HEAP.
kpeter@701
   363
    void state(const Item& i, State st) {
kpeter@701
   364
      switch (st) {
kpeter@701
   365
      case POST_HEAP:
kpeter@701
   366
      case PRE_HEAP:
kpeter@701
   367
        if (state(i) == IN_HEAP) {
kpeter@701
   368
          erase(i);
kpeter@701
   369
        }
kpeter@703
   370
        _iim[i] = st;
kpeter@701
   371
        break;
kpeter@701
   372
      case IN_HEAP:
kpeter@701
   373
        break;
kpeter@701
   374
      }
kpeter@701
   375
    }
kpeter@701
   376
kpeter@701
   377
  private:
kpeter@703
   378
    int findMin() {
kpeter@701
   379
      int min_loc=-1, min_val;
kpeter@703
   380
      int x=_head;
kpeter@701
   381
      if( x!=-1 ) {
kpeter@703
   382
        min_val=_data[x].prio;
kpeter@701
   383
        min_loc=x;
kpeter@703
   384
        x=_data[x].right_neighbor;
kpeter@701
   385
kpeter@701
   386
        while( x!=-1 ) {
kpeter@703
   387
          if( _comp( _data[x].prio,min_val ) ) {
kpeter@703
   388
            min_val=_data[x].prio;
kpeter@701
   389
            min_loc=x;
kpeter@701
   390
          }
kpeter@703
   391
          x=_data[x].right_neighbor;
kpeter@701
   392
        }
kpeter@701
   393
      }
kpeter@701
   394
      return min_loc;
kpeter@701
   395
    }
kpeter@701
   396
kpeter@701
   397
    void merge(int a) {
kpeter@701
   398
      interleave(a);
kpeter@701
   399
kpeter@703
   400
      int x=_head;
kpeter@701
   401
      if( -1!=x ) {
kpeter@703
   402
        int x_prev=-1, x_next=_data[x].right_neighbor;
kpeter@701
   403
        while( -1!=x_next ) {
kpeter@703
   404
          if( _data[x].degree!=_data[x_next].degree || ( -1!=_data[x_next].right_neighbor && _data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
kpeter@701
   405
            x_prev=x;
kpeter@701
   406
            x=x_next;
kpeter@701
   407
          }
kpeter@701
   408
          else {
kpeter@703
   409
            if( _comp(_data[x].prio,_data[x_next].prio) ) {
kpeter@703
   410
              _data[x].right_neighbor=_data[x_next].right_neighbor;
kpeter@701
   411
              fuse(x_next,x);
kpeter@701
   412
            }
kpeter@701
   413
            else {
kpeter@703
   414
              if( -1==x_prev ) { _head=x_next; }
kpeter@701
   415
              else {
kpeter@703
   416
                _data[x_prev].right_neighbor=x_next;
kpeter@701
   417
              }
kpeter@701
   418
              fuse(x,x_next);
kpeter@701
   419
              x=x_next;
kpeter@701
   420
            }
kpeter@701
   421
          }
kpeter@703
   422
          x_next=_data[x].right_neighbor;
kpeter@701
   423
        }
kpeter@701
   424
      }
kpeter@701
   425
    }
kpeter@701
   426
kpeter@701
   427
    void interleave(int a) {
kpeter@701
   428
      int other=-1, head_other=-1;
kpeter@701
   429
kpeter@703
   430
      while( -1!=a || -1!=_head ) {
kpeter@701
   431
        if( -1==a ) {
kpeter@701
   432
          if( -1==head_other ) {
kpeter@703
   433
            head_other=_head;
kpeter@701
   434
          }
kpeter@701
   435
          else {
kpeter@703
   436
            _data[other].right_neighbor=_head;
kpeter@701
   437
          }
kpeter@703
   438
          _head=-1;
kpeter@701
   439
        }
kpeter@703
   440
        else if( -1==_head ) {
kpeter@701
   441
          if( -1==head_other ) {
kpeter@701
   442
            head_other=a;
kpeter@701
   443
          }
kpeter@701
   444
          else {
kpeter@703
   445
            _data[other].right_neighbor=a;
kpeter@701
   446
          }
kpeter@701
   447
          a=-1;
kpeter@701
   448
        }
kpeter@701
   449
        else {
kpeter@703
   450
          if( _data[a].degree<_data[_head].degree ) {
kpeter@701
   451
            if( -1==head_other ) {
kpeter@701
   452
              head_other=a;
kpeter@701
   453
            }
kpeter@701
   454
            else {
kpeter@703
   455
              _data[other].right_neighbor=a;
kpeter@701
   456
            }
kpeter@701
   457
            other=a;
kpeter@703
   458
            a=_data[a].right_neighbor;
kpeter@701
   459
          }
kpeter@701
   460
          else {
kpeter@701
   461
            if( -1==head_other ) {
kpeter@703
   462
              head_other=_head;
kpeter@701
   463
            }
kpeter@701
   464
            else {
kpeter@703
   465
              _data[other].right_neighbor=_head;
kpeter@701
   466
            }
kpeter@703
   467
            other=_head;
kpeter@703
   468
            _head=_data[_head].right_neighbor;
kpeter@701
   469
          }
kpeter@701
   470
        }
kpeter@701
   471
      }
kpeter@703
   472
      _head=head_other;
kpeter@701
   473
    }
kpeter@701
   474
kpeter@701
   475
    // Lacing a under b
kpeter@701
   476
    void fuse(int a, int b) {
kpeter@703
   477
      _data[a].parent=b;
kpeter@703
   478
      _data[a].right_neighbor=_data[b].child;
kpeter@703
   479
      _data[b].child=a;
kpeter@701
   480
kpeter@703
   481
      ++_data[b].degree;
kpeter@701
   482
    }
kpeter@701
   483
kpeter@701
   484
    // It is invoked only if a has siblings.
kpeter@701
   485
    void unlace(int a) {
kpeter@703
   486
      int neighb=_data[a].right_neighbor;
kpeter@703
   487
      int other=_head;
kpeter@701
   488
kpeter@703
   489
      while( _data[other].right_neighbor!=a )
kpeter@703
   490
        other=_data[other].right_neighbor;
kpeter@703
   491
      _data[other].right_neighbor=neighb;
kpeter@701
   492
    }
kpeter@701
   493
kpeter@701
   494
  private:
kpeter@701
   495
kpeter@701
   496
    class store {
kpeter@701
   497
      friend class BinomHeap;
kpeter@701
   498
kpeter@701
   499
      Item name;
kpeter@701
   500
      int parent;
kpeter@701
   501
      int right_neighbor;
kpeter@701
   502
      int child;
kpeter@701
   503
      int degree;
kpeter@701
   504
      bool in;
kpeter@701
   505
      Prio prio;
kpeter@701
   506
kpeter@701
   507
      store() : parent(-1), right_neighbor(-1), child(-1), degree(0), in(true) {}
kpeter@701
   508
    };
kpeter@701
   509
  };
kpeter@701
   510
kpeter@701
   511
} //namespace lemon
kpeter@701
   512
kpeter@701
   513
#endif //LEMON_BINOM_HEAP_H
kpeter@701
   514