-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibheap.h
105 lines (77 loc) · 2.96 KB
/
fibheap.h
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
#ifndef _FIBHEAP_H
#define _FIBHEAP_H
//***************************************************************************
// The Fibonacci heap implementation contained in FIBHEAP.H and FIBHEAP.CPP
// is Copyright (c) 1996 by John Boyer
//
// Once this Fibonacci heap implementation (the software) has been published
// by Dr. Dobb's Journal, permission to use and distribute the software is
// granted provided that this copyright notice remains in the source and
// and the author (John Boyer) is acknowledged in works that use this program.
//
// Every effort has been made to ensure that this implementation is free of
// errors. Nonetheless, the author (John Boyer) assumes no liability regarding
// your use of this software.
//
// The author would also be very glad to hear from anyone who uses the
// software or has any feedback about it.
// Email: [email protected]
//***************************************************************************
#define OK 0
#define NOTOK -1
//======================================================
// Fibonacci Heap Node Class
//======================================================
class FibHeap;
class FibHeapNode
{
friend class FibHeap;
FibHeapNode *Left, *Right, *Parent, *Child;
short Degree, Mark, NegInfinityFlag;
protected:
int FHN_Cmp(FibHeapNode& RHS);
void FHN_Assign(FibHeapNode& RHS);
public:
FibHeapNode();
virtual ~FibHeapNode();
virtual void operator =(FibHeapNode& RHS);
virtual int operator ==(FibHeapNode& RHS);
virtual int operator <(FibHeapNode& RHS);
virtual void Print();
};
//========================================================================
// Fibonacci Heap Class
//========================================================================
class FibHeap
{
FibHeapNode *MinRoot;
long NumNodes, NumTrees, NumMarkedNodes;
int HeapOwnershipFlag;
public:
FibHeap();
virtual ~FibHeap();
// The Standard Heap Operations
void Insert(FibHeapNode *NewNode);
void Union(FibHeap *OtherHeap);
inline FibHeapNode *Minimum();
FibHeapNode *ExtractMin();
int DecreaseKey(FibHeapNode *theNode, FibHeapNode& NewKey);
int Delete(FibHeapNode *theNode);
// Extra utility functions
int GetHeapOwnership() { return HeapOwnershipFlag; };
void SetHeapOwnership() { HeapOwnershipFlag = 1; };
void ClearHeapOwnership() { HeapOwnershipFlag = 0; };
long GetNumNodes() { return NumNodes; };
long GetNumTrees() { return NumTrees; };
long GetNumMarkedNodes() { return NumMarkedNodes; };
void Print(FibHeapNode *Tree = nullptr, FibHeapNode *theParent=nullptr);
private:
// Internal functions that help to implement the Standard Operations
inline void _Exchange(FibHeapNode*&, FibHeapNode*&);
void _Consolidate();
void _Link(FibHeapNode *, FibHeapNode *);
void _AddToRootList(FibHeapNode *);
void _Cut(FibHeapNode *, FibHeapNode *);
void _CascadingCut(FibHeapNode *);
};
#endif