-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTree.h
63 lines (52 loc) · 1.58 KB
/
BTree.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
#ifndef BTREE_H
#define BTREE_H
#include <vector>
#include <string>
#pragma once
using namespace std;
struct AQIData {
string state;
string county;
string date; //YYYY-MM-DD format for parsing
int aqi;
// Comparison operator for sorting and searching
bool operator<(const AQIData& other) const {
return aqi < other.aqi;
}
bool operator>(const AQIData& other) const {
return aqi > other.aqi;
}
bool operator==(const AQIData& other) const {
return state == other.state && county == other.county && date == other.date && aqi == other.aqi;
}
};
struct BTreeNode {
vector<AQIData> keys; //AQIData objects in the node
vector<BTreeNode*> children; //Children pointers
bool isLeaf; //True if leaf node
BTreeNode(bool leaf) : isLeaf(leaf) {}
~BTreeNode() {
for (auto child : children) {
delete child;
}
}
};
class BTree {
private:
BTreeNode* root;
int t; // min degree of tree (max keys per node)
// Helper functions
void deleteTree(BTreeNode* node);
void insertNonFull(BTreeNode* node, const AQIData& data);
void splitChild(BTreeNode* parent, int index, BTreeNode* child);
void searchByCountyHelper(BTreeNode* node, const string& state, const string& county, vector<AQIData>& results);
void traverseHelper(BTreeNode* node);
public:
BTree(int degree);
~BTree();
void insert(const AQIData& data);
bool search(const AQIData& data);
vector<AQIData> searchByCounty(const string& state, const string& county);
void traverse();
};
#endif // BTREE_H