fibheap.h

Go to the documentation of this file.
00001 #ifndef FIBHEAP_H
00002 #define FIBHEAP_H
00003 
00004 //***************************************************************************
00005 // The Fibonacci heap implementation contained in FIBHEAP.H and FIBHEAP.CPP
00006 // is Copyright (c) 1996 by John Boyer
00007 //
00008 // Once this Fibonacci heap implementation (the software) has been published
00009 // by Dr. Dobb's Journal, permission to use and distribute the software is
00010 // granted provided that this copyright notice remains in the source and
00011 // and the author (John Boyer) is acknowledged in works that use this program.
00012 //
00013 // Every effort has been made to ensure that this implementation is free of
00014 // errors.  Nonetheless, the author (John Boyer) assumes no liability regarding
00015 // your use of this software.
00016 //
00017 // The author would also be very glad to hear from anyone who uses the
00018 // software or has any feedback about it.
00019 // Email: jboyer@gulf.csc.uvic.ca
00020 //***************************************************************************
00021 
00022 #define OK      0
00023 #define NOTOK   -1
00024 
00025 //======================================================
00026 // Fibonacci Heap Node Class
00027 //======================================================
00028 
00029 class FibHeap;
00030 
00032 class FibHeapNode
00033 {
00034 friend class FibHeap;
00035 
00036      FibHeapNode *Left, *Right, *Parent, *Child;
00037      short Degree, Mark, NegInfinityFlag;
00038 
00039 protected:
00040 
00041     inline int  FHN_Cmp(FibHeapNode& RHS){
00042         if (NegInfinityFlag)
00043         return RHS.NegInfinityFlag ? 0 : -1;
00044         return RHS.NegInfinityFlag ? 1 : 0;
00045     }
00046     inline void FHN_Assign(FibHeapNode& RHS){
00047          NegInfinityFlag = RHS.NegInfinityFlag;
00048     }
00049 
00050 public:
00051 
00052      FibHeapNode(){
00053          Left = Right = Parent = Child = NULL;
00054          Degree = Mark = NegInfinityFlag = 0;
00055      }
00056      virtual ~FibHeapNode();
00057 
00058      virtual void operator =(FibHeapNode& RHS);
00059      virtual int  operator ==(FibHeapNode& RHS);
00060      virtual int  operator <(FibHeapNode& RHS);
00061 
00062      virtual void Print();
00063 };
00064 
00065 //========================================================================
00066 // Fibonacci Heap Class
00067 //========================================================================
00068 
00070 class FibHeap
00071 {
00072      FibHeapNode *MinRoot;
00073      long NumNodes, NumTrees, NumMarkedNodes;
00074 
00075      int HeapOwnershipFlag;
00076 
00077 public:
00078 
00079      FibHeap();
00080      virtual ~FibHeap();
00081 
00082 // The Standard Heap Operations
00083 
00084      void Insert(FibHeapNode *NewNode);
00085      void Union(FibHeap *OtherHeap);
00086 
00087     inline FibHeapNode *Minimum(){
00088          return MinRoot;
00089     }
00090      FibHeapNode *ExtractMin();
00091 
00092      int DecreaseKey(FibHeapNode *theNode, FibHeapNode& NewKey);
00093      int Delete(FibHeapNode *theNode);
00094 
00095 // Extra utility functions
00096 
00097      int  GetHeapOwnership() { return HeapOwnershipFlag; };
00098      void SetHeapOwnership() { HeapOwnershipFlag = 1; };
00099      void ClearHeapOwnership() { HeapOwnershipFlag = 0; };
00100 
00101      long GetNumNodes() { return NumNodes; };
00102      long GetNumTrees() { return NumTrees; };
00103      long GetNumMarkedNodes() { return NumMarkedNodes; };
00104 
00105      void Print(FibHeapNode *Tree = NULL, FibHeapNode *theParent=NULL);
00106 
00107 private:
00108 
00109 // Internal functions that help to implement the Standard Operations
00110 
00111     inline void _Exchange(FibHeapNode* &N1, FibHeapNode* &N2){
00112         FibHeapNode *Temp;
00113 
00114          Temp = N1;
00115          N1 = N2;
00116          N2 = Temp;
00117     }
00118 
00119      void _Consolidate();
00120      void _Link(FibHeapNode *, FibHeapNode *);
00121      void _AddToRootList(FibHeapNode *);
00122      void _Cut(FibHeapNode *, FibHeapNode *);
00123      void _CascadingCut(FibHeapNode *);
00124 };
00125 
00126 #endif /* FIBHEAP_H */

Generated on Mon May 24 09:53:30 2010 for TUMBLE by  doxygen 1.5.2