-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTree.cpp
48 lines (39 loc) · 1.08 KB
/
BTree.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
// Author: Andy Yang
// Author: Manvinder Sodhi
//#define NDEBUG
#include <iostream>
#include <cassert>
#include "BTree.h"
#include "BTreeNode.h"
#include "LeafNode.h"
#include "InternalNode.h"
using namespace std;
BTree::BTree(int ISize, int LSize):internalSize(ISize), leafSize(LSize)
{
root = new LeafNode(LSize, NULL, NULL, NULL);
} // BTree::BTree()
void BTree::insert(const int value)
{
BTreeNode *split = root->insert(value);
if (split) // LeafNode needs split
{
InternalNode *parent = new InternalNode(internalSize, leafSize, NULL,
NULL, NULL);
root->setParent(parent); // set parents to root
split->setParent(parent);
parent->insert(root); // should be on the left
parent->insert(split); // should be on the right
root = parent;
} // if split needed
} // BTree::insert()
void BTree::print()
{
BTreeNode *BTreeNodePtr;
Queue<BTreeNode*> queue(1000);
queue.enqueue(root);
while(!queue.isEmpty())
{
BTreeNodePtr = queue.dequeue();
BTreeNodePtr->print(queue);
} // while
} // BTree::print()