-
Notifications
You must be signed in to change notification settings - Fork 45
/
B+ Tree.h
71 lines (63 loc) · 2.6 KB
/
B+ Tree.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>
//#include <filesystem>
using namespace std;
#ifndef NODE_H
#define NODE_H
class Node {
/*
Generally size of the this node should be equal to the block size. Which will limit the number of disk access and increase the accesssing time.
Intermediate nodes only hold the Tree pointers which is of considerably small size(so they can hold more Tree pointers) and only Leaf nodes hold
the data pointer directly to the disc.
IMPORTANT := All the data has to be present in the leaf node
*/
public:
bool isLeaf;
vector<int> keys;
//Node* ptr2parent; //Pointer to go to parent node CANNOT USE check https://stackoverflow.com/questions/57831014/why-we-are-not-saving-the-parent-pointer-in-b-tree-for-easy-upward-traversal-in
Node* ptr2next; //Pointer to connect next node for leaf nodes
union ptr { //to make memory efficient Node
vector<Node*> ptr2Tree; //Array of pointers to Children sub-trees for intermediate Nodes
vector<FILE*> dataPtr; // Data-Pointer for the leaf node
ptr(); // To remove the error !?
~ptr(); // To remove the error !?
} ptr2TreeOrData;
friend class BPTree; // to access private members of the Node and hold the encapsulation concept
public:
Node();
};
class BPTree {
/*
::For Root Node :=
The root node has, at least two tree pointers
::For Internal Nodes:=
1. ceil(maxIntChildLimit/2) <= #of children <= maxIntChildLimit
2. ceil(maxIntChildLimit/2)-1 <= #of keys <= maxIntChildLimit -1
::For Leaf Nodes :=
1. ceil(maxLeafNodeLimit/2) <= #of keys <= maxLeafNodeLimit -1
*/
private:
int maxIntChildLimit; //Limiting #of children for internal Nodes!
int maxLeafNodeLimit; // Limiting #of nodes for leaf Nodes!!!
Node* root; //Pointer to the B+ Tree root
void insertInternal(int x, Node** cursor, Node** child); //Insert x from child in cursor(parent)
Node** findParent(Node* cursor, Node* child);
Node* firstLeftNode(Node* cursor);
public:
BPTree();
BPTree(int degreeInternal, int degreeLeaf);
Node* getRoot();
int getMaxIntChildLimit();
int getMaxLeafNodeLimit();
void setRoot(Node *);
void display(Node* cursor);
void seqDisplay(Node* cursor);
void search(int key);
void insert(int key, FILE* filePtr);
void removeKey(int key);
void removeInternal(int x, Node* cursor, Node* child);
};
#endif