-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpquelib.c
139 lines (124 loc) · 3.71 KB
/
pquelib.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**********************************************************************/
/* FILE: pquelib,c
* DATE: 24 AUG 2000
* AUTH: G. E. Deschaines
* DESC: Data structures and methods for a priority queue implemented
* with an ordered binary heap maintained in an array. A decent
* explanation of priority queues can be found at this link:
*
* http://algs4.cs.princeton.edu/24pq/
*
* Methods presented herein were derived from those presented in
* a textbook on programming and data structures using Pascal.
*/
/**********************************************************************/
#define MaxElements 1024
#define FALSE 0
#define TRUE 1
typedef struct
{
Longint Key;
Integer Info;
} HeapElement;
typedef HeapElement HeapArray[MaxElements];
typedef struct PQ_type *PQtypePtr;
typedef struct PQ_type
{
HeapElement Elements[MaxElements];
Integer Bottom;
} PQtype;
void ClearPQ( PQtypePtr pPQueue )
{
pPQueue->Bottom = 0;
}
Boolean EmptyPQ( PQtype aPQueue )
{
return (Boolean)( aPQueue.Bottom == 0 );
}
Boolean FullPQ( PQtype aPQueue )
{
return (Boolean)( aPQueue.Bottom == MaxElements );
}
void ReHeapUp( HeapElement *HeapElements, Integer Bottom )
{
Integer CurrentIndex;
Integer ParentIndex;
Boolean HeapOk;
HeapElement TempElement;
HeapOk = FALSE;
CurrentIndex = Bottom;
ParentIndex = CurrentIndex / 2;
while ( ( CurrentIndex > 1 ) && ( ! HeapOk ) )
{
if ( HeapElements[ParentIndex].Key >=
HeapElements[CurrentIndex].Key )
{
HeapOk = TRUE;
}
else
{
TempElement = HeapElements[ParentIndex];
HeapElements[ParentIndex] = HeapElements[CurrentIndex];
HeapElements[CurrentIndex] = TempElement;
CurrentIndex = ParentIndex;
ParentIndex = ParentIndex / 2;
}
}
}
void ReHeapDown( HeapElement *HeapElements, Integer Root, Integer Bottom )
{
Boolean HeapOk;
Integer MaxChild;
Integer Root2;
Integer Root2p1;
HeapElement TempElement;
Root2 = Root*2;
Root2p1 = Root2 + 1;
HeapOk = FALSE;
while ( ( Root2 <= Bottom ) && ( ! HeapOk ) )
{
if ( Root2 == Bottom )
{
MaxChild = Root2;
}
else
{
if ( HeapElements[Root2].Key > HeapElements[Root2p1].Key )
{
MaxChild = Root2;
}
else
{
MaxChild = Root2p1;
}
}
if ( HeapElements[Root].Key < HeapElements[MaxChild].Key )
{
TempElement = HeapElements[Root];
HeapElements[Root] = HeapElements[MaxChild];
HeapElements[MaxChild] = TempElement;
Root = MaxChild;
Root2 = Root*2;
Root2p1 = Root2 + 1;
}
else
{
HeapOk = TRUE;
}
}
}
void PriorityEnq( PQtypePtr pPQueue, HeapElement NewElement )
{
pPQueue->Bottom = pPQueue->Bottom + 1;
pPQueue->Elements[pPQueue->Bottom] = NewElement;
ReHeapUp(pPQueue->Elements, pPQueue->Bottom);
}
void PriorityDeq( PQtypePtr pPQueue, HeapElement *FirstElement )
{
*FirstElement = pPQueue->Elements[1];
pPQueue->Elements[1] = pPQueue->Elements[pPQueue->Bottom];
pPQueue->Bottom = pPQueue->Bottom - 1;
ReHeapDown(pPQueue->Elements, 1, pPQueue->Bottom);
}
/**********************************************************************/
/**********************************************************************/