-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceSaving.h
52 lines (42 loc) · 1.59 KB
/
SpaceSaving.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
#ifndef SPACESAVING_H
#define SPACESAVING_H
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#define newline "\n"
using namespace std;
struct Node
{
string id;
int weight, error, index;
// constructor
Node(string a, int b, int c, int d)
{
id = a;
weight = b;
error = c;
index = d;
}
};
typedef unordered_map<string, Node *> umsn;
typedef vector<Node *> vn;
class SpaceSaving
{
private:
vn minHeap; // data structure for the summary
int k; // capacity of heap
umsn dict; // keys on heap
public:
SpaceSaving(int capacity); // constructor
~SpaceSaving(); // destructor
void swap(Node *a, Node *b); // swap two nodes
int parent(int i) { return (i - 1) / 2; } // to get index of parent of node at index i
int left(int i) { return (2 * i + 1); } // to get index of left child of node at index i
int right(int i) { return (2 * i + 2); } // to get index of right child of node at index i
void spaceSaving(string id); // main algorithm
void increaseKey(int i); // increment counter
void print(); // print the stream summary
};
#endif // SPACESAVING_H