-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.h
81 lines (69 loc) · 2.38 KB
/
heap.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
#ifndef _HEAP_H
#define _HEAP_H
#include "hash.h"
class heap {
private:
class node { // An inner class within heap
public:
std::string id = ""; // The id of this node
int key; // The key of this node
void *pData; // A pointer to the actual data
};
bool isEmpty();
void percolateUp(int posCur);
void percolateDown(int posCur);
int getPos(node *pn);
int capacity;
int currSize;
std::vector<node> data; // The actual binary heap
hashTable *mapping; // maps ids to node pointers
public:
//void printHeap();
// heap - The constructor allocates space for the nodes of the heap
// and the mapping (hash table) based on the specified capacity
heap(int capacity);
// insert - Inserts a new node into the binary heap
//
// Inserts a node with the specified id string, key,
// and optionally a pointer. They key is used to
// determine the final position of the new node.
//
// Returns:
// 0 on success
// 1 if the heap is already filled to capacity
// 2 if a node with the given id already exists (but the heap
// is not filled to capacity)
int insert(const std::string &id, int key, void *pv = NULL);
// setKey - set the key of the specified node to the specified value
//
// I have decided that the class should provide this member function
// instead of two separate increaseKey and decreaseKey functions.
//
// Returns:
// 0 on success
// 1 if a node with the given id does not exist
int setKey(const std::string &id, int key);
// deleteMin - return the data associated with the smallest key
// and delete that node from the binary heap
//
// If pId is supplied (i.e., it is not NULL), write to that address
// the id of the node being deleted. If pKey is supplied, write to
// that address the key of the node being deleted. If ppData is
// supplied, write to that address the associated void pointer.
//
// Returns:
// 0 on success
// 1 if the heap is empty
int deleteMin(std::string *pId = NULL, int *pKey = NULL, void *ppData = NULL);
// remove - delete the node with the specified id from the binary heap
//
// If pKey is supplied, write to that address the key of the node
// being deleted. If ppData is supplied, write to that address the
// associated void pointer.
//
// Returns:
// 0 on success
// 1 if a node with the given id does not exist
int remove(const std::string &id, int *pKey = NULL, void *ppData = NULL);
};
#endif