Implementation of B+ tree, which is extensively used in the SQL range queries.The implementation supports queries like INSERT,FIND,COUNT and RANGE.
- INSERT X - insert X into the B+ tree
- FIND X - print YES if X is already inserted, else NO
- COUNT X - print number of occurrences of X in B+tree
- RANGE X Y - print number of elements in range x to y (both x and y included)
- PRINT - Pretty Print B+ Tree
- -10^9 <= X <= 10^9, -10^9 <= Y <= 10^9
- The number of queries will be less than 10^6.
- Open terminal in your machine.
- Type
make
and press enter. - Now you should see an a.out file in the current directory.
- Open terminal in your machine.
- Type
g++ -g BPTree.h BPTree.cpp PrettyPrint.h PrettyPrint.cpp main.cpp
and press enter. - Now you should see an a.out file in the current directory.
- Type
./a.out <file_name>
and press enter.
<file_name>
: Can be a path too, or just file_name if it is present in current directory.
- The input argument consists of a file name.
- Each line in the file consists of one of the above-mentioned queries.
Write outputs of the corresponding queries in command-line.
- Type
./test.sh
and press enter. - If there is no line after the Comparision Line for each test Case then that test case is passed.
- Else output will correspond to the output format of
diff
.
- Inside the Director test_data, there are two programs,
test_input_generator.py
andtest_out_generator.cpp
. - First run
python3 test_input_generator.py
and a new file called input.txt will be generated/overwritten in the pwd. - Now compile
g++ -g test_out_generator.cpp
, followed by./a.out input.txt > out.txt
. Now you will have the correct output corresponding to your newly generated input.txt. - Now you can use this input.txt with the test.sh, and see if it passes it.
-
There will be no PRINT test queries, as it is just for making code easier to write during developement.
-
Also PRINT does not support more than 10 level in B+ Tree.As it will not be pretty printable on terminal.
- PrettyPrint.h - Declares a class called
BPTreePrinter
which gives a print function which pretty prints the tree. - PrettyPrint.cpp - Implements the class
BPTreePrinter
. - BPTree.h - Declares classes called
Node, BPTree
which defines a B+ Tree node and B+ Tree. - BPTree.cpp - Implements the class
Node, BPTree
. - main.cpp - The Driver Program.
- test_data/ - Director Containing Test input files and Test Case Generators.
- Keeps track of Keys, Child Pointers, Number of Keys, Parent Pointer, Leaf Status.
- Common class which acts as both a Leaf as well as a Internal Node.
- Has a special function called
sterlizeNode()
which sterlizes the node i.e. removes the garbage values from the node after Node splitting. - It is a friend of
BPTreePrinter
class
- Keeps track of the root Node.
- Implements functions like insert(), search(), count(), and range() etc.
- It is a friend of
BPTreePrinter
class
- First it checks if the tree is empty, if it is then it creates a leaf node and returns it as root.
- Else Search for the leaf Node where the node should be inserted.
- If the found leaf node has space then add the value, and finish.
- Else split the node using Right Bias, and add the smallest key of new formed leaf to the parent. And again follow insertion process steps 2,3,4 till the overflow condition becomes false, and the tree becomes stable.
- Uses an private utility Function called
findLeafNode(int x)
which returns potential leaf node with the value. - Check the node if value exists then print "YES" else "NO".
- Uses an private utility Function called
findLeafNode(int x)
which returns potential leaf node with the value. But here we call it on x-1, to ensure we get keys from first occurence of x. - Traverse the values in the node and increase count.
- If the values in current leaf node are traversed and no key > y is found we go to next adjacent leaf node and continue counting.
- If any key > y is found , print the value of count.
- Uses the same Range function internally, by calling
range(x,x)
. - We get the required count.