-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.h
More file actions
executable file
·115 lines (97 loc) · 2.56 KB
/
Copy pathpriority_queue.h
File metadata and controls
executable file
·115 lines (97 loc) · 2.56 KB
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
/**
* @file priority_queue.h
*/
#ifndef _PQUEUE_H_
#define _PQUEUE_H_
/**
* Implements the max priority queue ADT.
*
* The implementation is up to you, but you must complete all the given
* public functions. You will need to add some member variables and private
* helper functions.
*/
template <class T>
class PriorityQueue
{
private:
struct Node
{
T elem;
Node* child;
Node* prev;
Node* next;
Node(const T & element):
elem(element), child(NULL), prev(NULL), next(NULL) {}
};
public:
/**
* Constructor: creates an empty priority queue.
*/
PriorityQueue();
/**
* Copy constructor
*/
PriorityQueue(const PriorityQueue & other);
/**
* Destructor
*/
~PriorityQueue();
/**
* Assignment operator
*/
const PriorityQueue & operator=(const PriorityQueue & rhs);
/**
* Frees all memory and sets Queue to be empty
*/
void clear();
/**
* Inserts the given value into the queue.
*
* @param value The value to be inserted.
*/
void insert(const T & value);
/**
* Removes the highest value (and its associated data) from the
* queue.
*
* @return A copy of the removed (maximum) element
*/
T pop();
/**
* Returns the highest value from the queue. Does NOT remove it.
*
* @return A copy of the maximum element
*/
const T & top() const;
/**
* Determines if the queue is empty. Should be O(1).
*
* @return A boolean value indicating whether the queue is
* empty.
*/
bool isEmpty() const;
private:
Node* root;
/*
* Copies the pairing heap rooted at subRoot.
* @return The root of the new heap.
*/
Node* copy(const Node* subRoot);
/*
* Frees memory of the pairing heap rooted at subRoot
*/
void clear(Node* subRoot);
/*
* Combines two pairing heaps rooted at first and second into
* a larger pairing heap rooted at first.
* @param first cannot be NULL
*/
void meld(Node* & first, Node* second);
/*
* Two-pass merges siblings
* @param first is the leftmost child
*/
Node* meldSiblings(Node* first);
};
#include "priority_queue.cpp"
#endif