-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBTree.h
More file actions
122 lines (115 loc) · 3.49 KB
/
BTree.h
File metadata and controls
122 lines (115 loc) · 3.49 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
116
117
118
119
120
121
122
/**
* @file BTree.h
* @author So Man Amanda Au-Yeung, Chin Yuen Au (Isaac)
* @brief CS7280 Special Topics in Database Management
* Project 1: B-tree implementation.
* @date due 2024-02-14
*
* @copyright Copyright (c) 2024
*
*/
#ifndef BTREE_H
#define BTREE_H
#include <vector>
#include <string>
using namespace std;
/*
* Node data structure.
* - This is the simplest structure for nodes used in B-tree
* - This will be used for both internal and leaf nodes.
*/
class Node
{
private:
// Node Values (Leaf Values / Key Values for the children nodes).
long long int *values;
// Children ptrs from current node
Node **children;
// Number of value
int size;
// indicates if the current node is a leaf node
bool leaf;
// Node Id
long long int NodeId;
// nodesize
int nodesize;
// string for child key block
string childKeyBlk;
public:
// Node Constructor Declaration: initiates a new node
Node(int nodesize, bool _leaf);
// nodeLookup(int value) - search the index of the value in the specific node
long long int NodeLookup(long long int value);
// getter for nodeId
long long int getNodeId();
// getter for childKeyBlk chain
string getChildKeyBlk();
// getter for children nodes
Node **getChildren();
// getter for size of the children nodes
int getChildrenSize();
// Node insert values to the specific node
void NodeInsert(long long int value, long long int &NodeIdCounter);
// split child helper function if current node is full
void SplitChild(int i, Node *CurrNode, long long int &NodeIdCounter);
// to convert child to 5 digit string
string intToFiveDigitString(long long int number);
// to convert key+block# to 8+5 digit string
string intToThirteenDigitString(long long int number);
// Display node
void Display(int _size, long long int ¤tBlock, int& firstDb);
// deconstructor
~Node();
// allow access of Node class for BTree
friend class BTree;
};
/**
* @brief BTree class for building the BTree
*
*/
class BTree
{
private:
// Node array, including the root nodes
Node *nodes;
// NodeIdCounter
long long int NodeIdCounter;
// nodesize --> blocking factor (bfr)
int nodesize;
// count layers if there's more than 2 layers meaning internal nodes exist
int layers;
// root node ID
int rootId;
// first index node to write
long long int firstIndexToWrite;
// data block key value
int blockVal;
public:
// constructor for BTree
BTree(int nodesize);
// Lookup - True if the value was found.
bool Lookup(Node *root, long long int value, vector<int> &NodeIds);
// Search for the full ndoes
bool Lookup(Node *root, long long int value, vector<Node *> &FullNodes);
// Public method to access the root nodes
Node *getRootNode();
// Get root node ID
long long int getRootId();
// get total number of nodes used
int getTotalNodes();
// get first index to write
int getFirstIndexToWrite();
// get the block key value upon find
int getBlockVal();
// Setter method for the root node pointer
void setRootNode(Node *root);
// helper function for inserting a new value
Node *SplitRoot(Node *node, long long int value);
// Insert specific value to the tree
void Insert(long long int value);
// Display of the entire tree
void Display(long long int ¤tBlock, int &firstDb);
// deconstructor
~BTree();
};
#endif // BTREE_H