-
Notifications
You must be signed in to change notification settings - Fork 45
/
utilFunc.cpp
89 lines (71 loc) · 2.35 KB
/
utilFunc.cpp
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
#include <iostream>
#include "B+ Tree.h"
Node* parent = NULL;
Node::ptr::ptr() {
}
Node::ptr::~ptr() {
}
Node::Node() {
this->isLeaf = false;
this->ptr2next = NULL;
}
BPTree::BPTree() {
/*
By Default it will take the maxIntChildLimit as 4. And
maxLeafNodeLimit as 3.
::REASON FOR TWO SEPERATE VARIABLES maxIntChildLimit & maxLeafNodeLimit !!
We are keeping the two seperate Orders
because Internal Nodes can hold more values in one disc block
as the size of the Tree pointer is small but the size of the
data pointer in the leaf nodes is large so we can only put less
nodes in the leafs as compared to the internal Nodes. Thats the
reson to reperate out these to variables.
*/
this->maxIntChildLimit = 4;
this->maxLeafNodeLimit = 3;
this->root = NULL;
}
BPTree::BPTree(int degreeInternal, int degreeLeaf) {
this->maxIntChildLimit = degreeInternal;
this->maxLeafNodeLimit = degreeLeaf;
this->root = NULL;
}
int BPTree::getMaxIntChildLimit() {
return maxIntChildLimit;
}
int BPTree::getMaxLeafNodeLimit() {
return maxLeafNodeLimit;
}
Node* BPTree::getRoot() {
return this->root;
}
void BPTree::setRoot(Node *ptr) {
this->root = ptr;
}
Node* BPTree::firstLeftNode(Node* cursor) {
if (cursor->isLeaf)
return cursor;
for (int i = 0; i < cursor->ptr2TreeOrData.ptr2Tree.size(); i++)
if (cursor->ptr2TreeOrData.ptr2Tree[i] != NULL)
return firstLeftNode(cursor->ptr2TreeOrData.ptr2Tree[i]);
return NULL;
}
Node** BPTree::findParent(Node* cursor, Node* child) {
/*
Finds parent using depth first traversal and ignores leaf nodes as they cannot be parents
also ignores second last level because we will never find parent of a leaf node during insertion using this function
*/
if (cursor->isLeaf || cursor->ptr2TreeOrData.ptr2Tree[0]->isLeaf)
return NULL;
for (int i = 0; i < cursor->ptr2TreeOrData.ptr2Tree.size(); i++) {
if (cursor->ptr2TreeOrData.ptr2Tree[i] == child) {
parent = cursor;
} else {
//Commenting To Remove vector out of bound Error:
//new (&cursor->ptr2TreeOrData.ptr2Tree) std::vector<Node*>;
Node* tmpCursor = cursor->ptr2TreeOrData.ptr2Tree[i];
findParent(tmpCursor, child);
}
}
return &parent;
}