Skip to content
/ BTree Public

This repository contains a fully functional B-Tree implemented in C++. It showcases key computer science concepts, including self-balancing trees, recursion, dynamic memory management, and in-order traversal. Includes ASCII diagrams and detailed documentation for educational purposes.

License

Notifications You must be signed in to change notification settings

lkkhwhb/BTree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

B-Tree Implementation in C++

GitHub: https://github.com/lkkhwhb/BTree

This repository contains a C++ implementation of a B-Tree, a self-balancing search tree commonly used in databases and filesystems. The project demonstrates core data structure concepts, including nodes, recursion, dynamic memory management, and tree traversal.


Detailed Explanation of B-Trees

1. What is a B-Tree?

A B-Tree is a self-balancing search tree that maintains sorted data and allows efficient search, insertion, and traversal operations. Unlike a binary search tree (BST), where each node has at most 2 children, a B-Tree node can have multiple keys and multiple children, making it suitable for large datasets and disk-based storage systems.


2. Terminology

  • Minimum Degree (t): Determines the minimum and maximum number of keys per node.

    • Minimum keys in a non-root node: t - 1
    • Maximum keys in a node: 2t - 1
    • Minimum children for a non-leaf node: t
    • Maximum children: 2t
  • Node: Container for keys and child pointers.

  • Leaf Node: Node with no children. All leaf nodes are at the same level.

  • Internal Node: Node that is not a leaf; it has child pointers.


3. Properties of B-Trees

  1. All leaf nodes are at the same depth, ensuring balance.
  2. Keys within a node are always sorted in ascending order.
  3. Each child subtree contains keys in a range defined by the parent’s keys.
  4. Node splitting ensures the height of the tree remains low, guaranteeing O(log n) operations.

4. Structure of a B-Tree Node

Each node contains:

  1. Array of keys (size ≤ 2t - 1)
  2. Array of child pointers (size ≤ 2t)
  3. Number of keys currently stored
  4. Leaf indicator

Example (t = 3):

Keys: [10 | 20 | 30]
Children: C0  C1  C2  C3

5. Insertion in a B-Tree

  • Traverse recursively to the correct child node.
  • If a node is full, split the node and move the middle key up.
  • Insert the key into a non-full leaf node.
  • Node splitting ensures balance is maintained automatically.

6. Search in a B-Tree

  • Start at the root and compare with keys.
  • Traverse the appropriate child based on the key range.
  • Repeat recursively until the key is found or a leaf is reached.
  • Time complexity: O(log n).

7. Example and ASCII Diagram

Inserting keys: 10, 20, 5, 6, 12, 30, 7, 17 (t = 3):

B-Tree Structure (ASCII Diagram):

          [10 20]
         /   |    \
     [5 6 7] [12 17] [30]
  • Root node: [10 20]
  • Left child: [5 6 7]
  • Middle child: [12 17]
  • Right child: [30]

Traversal (in-order) gives sorted keys:

5 6 7 10 12 17 20 30

Searching for 6 finds it in the leftmost child.


Project Features (Implemented)

  • Configurable minimum degree (t)
  • Insertion of keys
  • Search for keys
  • In-order traversal (prints keys in sorted order)
  • Modular C++ classes and dynamic memory usage

Project Structure

BTree/
├── include/
│   └── BTree.h       # Class declarations
├── src/
│   └── BTree.cpp     # Function definitions
└── main.cpp          # Demonstration of insertion, traversal, and search

Usage Example

#include <iostream>
#include "include/BTree.h"
using namespace std;

int main() {
    BTree t(3); // Minimum degree 3

    t.insert(10);
    t.insert(20);
    t.insert(5);
    t.insert(6);
    t.insert(12);
    t.insert(30);
    t.insert(7);
    t.insert(17);

    cout << "Traversal of the tree:" << endl;
    t.traverse();

    int key = 6;
    cout << "\nSearching for " << key << "..." << endl;
    if (t.search(key))
        cout << "Found " << key << endl;
    else
        cout << key << " not found" << endl;

    return 0;
}

Expected Output:

Traversal of the tree:
5 6 7 10 12 17 20 30
Searching for 6...
Found 6

How to Run

  1. Open terminal or PowerShell in the project folder.
  2. Compile the project:
g++ main.cpp src/BTree.cpp -o BTreeApp
  1. Run the executable:
.\BTreeApp.exe

Concepts Demonstrated

  • Self-Balancing Trees: Node splitting maintains balance.
  • Dynamic Memory Management: Nodes and keys are allocated using pointers.
  • Recursion: Traversal, search, and insertion implemented recursively.
  • Data Structures: Arrays, pointers, and hierarchical tree structure.
  • Algorithmic Efficiency: Search and insertion operate in O(log n).

References

About

This repository contains a fully functional B-Tree implemented in C++. It showcases key computer science concepts, including self-balancing trees, recursion, dynamic memory management, and in-order traversal. Includes ASCII diagrams and detailed documentation for educational purposes.

Topics

Resources

License

Stars

Watchers

Forks

Languages