-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTreeNode.h
96 lines (71 loc) · 3.03 KB
/
BTreeNode.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <fstream>
#include <vector>
#include <algorithm>
#include "index.h"
#define tam 3999
class BTreeNode
{
index* keys; // An array of keys
int t; // Minimum degree (defines the range for number of keys)
BTreeNode** C; // An array of child pointers
int n; // Current number of keys
bool leaf; // Is true when node is leaf. Otherwise false
public:
// Constructor
BTreeNode(int _t, bool _leaf);
//needs destructor
// A function to traverse all nodes in a subtree rooted with this node
void traverse();
// A function to search a key in the subtree rooted with this node.
BTreeNode* search(char* k); // returns NULL if k is not present.
index _search(std::string k); // returns empty if k is not present.
void _find(std::vector<index>& vec, const std::string& k);
// A utility function to insert a new key in the subtree rooted with
// this node. The assumption is, the node must be non-full when this
// function is called
void insertNonFull(index k);
// A utility function to split the child y of this node. i is index of y in
// child array C[]. The Child y must be full when this function is called
void splitChild(int i, BTreeNode* y);
// A function that returns the index of the first key that is greater
// or equal to k
int findKey(index k);
// A wrapper function to remove the key k in subtree rooted with
// this node.
void remove(index k);
// A function to remove the key present in idx-th position in
// this node which is a leaf
void removeFromLeaf(int idx);
// A function to remove the key present in idx-th position in
// this node which is a non-leaf node
void removeFromNonLeaf(int idx);
// A function to get the predecessor of the key- where the key
// is present in the idx-th position in the node
index getPred(int idx);
// A function to get the successor of the key- where the key
// is present in the idx-th position in the node
index getSucc(int idx);
// A function to fill up the child node present in the idx-th
// position in the C[] array if that child has less than t-1 keys
void fill(int idx);
// A function to borrow a key from the C[idx-1]-th node and place
// it in C[idx]th node
void borrowFromPrev(int idx);
// A function to borrow a key from the C[idx+1]-th node and place it
// in C[idx]th node
void borrowFromNext(int idx);
// A function to merge idx-th child of the node with (idx+1)th child of
// the node
void merge(int idx);
// A function to export all nodes in a subtree rooted with this node
void _export(std::fstream& stream, int& counter);
void _exportNode(std::fstream& stream, int& counter);
// A function to import a file to the tree
void _import(std::fstream& stream, int& counter);
void _printLevels();
void _printLevel();
// Make the BTree friend of this so that we can access private members of this
// class in BTree functions
friend class BTree;
};