Searching in B+ Tree

Searching in a B-tree involves traversing the tree from the root to find the node that might contain the desired key.

Step-by-step algorithm:

  • Start at the root of the B+ tree.
  • Check if the current node is a leaf node.
  • If not a leaf node, compare the search key with the keys in the node to determine the appropriate child node to traverse.
  • If a leaf node, search for the key within the keys stored in the leaf node.
  • If the key is found, return the corresponding value.
  • If the key is not found in any leaf node, return a failure indication (e.g., null or false).
  • Repeat steps 3-6 until the key is found or until there are no more child nodes to traverse.

Below is the implementation of the above idea:

Python3
import math

# Node creation
class Node:
    def __init__(self, order):
        self.order = order
        self.values = []
        self.keys = []
        self.nextKey = None
        self.parent = None
        self.check_leaf = False

    # Search operation
    def search(self, value):
        current_node = self
        while(not current_node.check_leaf):
            temp2 = current_node.values
            for i in range(len(temp2)):
                if (value == temp2[i]):
                    current_node = current_node.keys[i + 1]
                    break
                elif (value < temp2[i]):
                    current_node = current_node.keys[i]
                    break
                elif (i + 1 == len(current_node.values)):
                    current_node = current_node.keys[i + 1]
                    break
        return current_node

# B plus tree
class BplusTree:
    def __init__(self, order):
        self.root = Node(order)
        self.root.check_leaf = True

    # Search operation
    def search(self, value):
        current_node = self.root
        while(not current_node.check_leaf):
            temp2 = current_node.values
            for i in range(len(temp2)):
                if (value == temp2[i]):
                    current_node = current_node.keys[i + 1]
                    break
                elif (value < temp2[i]):
                    current_node = current_node.keys[i]
                    break
                elif (i + 1 == len(current_node.values)):
                    current_node = current_node.keys[i + 1]
                    break
        return current_node

# Print the tree
def printTree(tree):
    lst = [tree.root]
    level = [0]
    leaf = None
    flag = 0
    lev_leaf = 0

    node1 = Node(str(level[0]) + str(tree.root.values))

    while (len(lst) != 0):
        x = lst.pop(0)
        lev = level.pop(0)
        if (x.check_leaf == False):
            for i, item in enumerate(x.keys):
                print(item.values)
        else:
            for i, item in enumerate(x.keys):
                print(item.values)
            if (flag == 0):
                lev_leaf = lev
                leaf = x
                flag = 1

# Main code
record_len = 3
bplustree = BplusTree(record_len)
bplustree.search('5')

printTree(bplustree)

if(bplustree.search('5')):
    print("Found")
else:
    print("Not found")

Output
Found

Time Complexity: O(log n)
Auxiliary Space: O(1)

B+ Tree in Python

In computer science, data structures are crucial in efficiently managing and organizing data. Among these, the B+ tree is a powerful and important data structure, widely used in databases and file systems. In this article, we will discuss the concept of B+ trees, exploring their structure, operations, and implementation in Python.

B+ Tree in Python

Table of Content

  • What is a B+ Tree?
  • Key Characteristics of B+ Trees
  • Operations on B+ Trees
  • Searching in B+ Tree
  • Insertion in B+ Tree
  • Deletion in B+ Tree

Similar Reads

What is a B+ Tree?

A B+ tree is a self-balancing tree data structure designed for efficient storage and retrieval of data in secondary memory such as disk storage. It is a variant of the B-tree, characterized by its ability to store multiple keys in each node, with only the leaf nodes containing actual data pointers. The internal nodes act as index nodes, facilitating fast searching and traversal....

Key Characteristics of B+ Trees:

Ordered Structure: Keys in a B+ tree are stored in sorted order, enabling efficient searching through binary search.Balanced Tree: B+ trees are self-balancing, ensuring that operations such as insertion and deletion maintain a balanced tree structure, which results in optimal performance.Leaf Node Linked List: All leaf nodes are linked together, forming a linked list. This feature facilitates a range of queries and sequential access....

Operations on B+ Trees:

Search: Searching in a B+ tree involves traversing the tree from the root node to the leaf node, and performing a binary search to locate the desired key.Insertion: Inserting a new key-value pair into a B+ tree begins with a search operation to find the appropriate leaf node. If the leaf node has space, the key-value pair is inserted. Otherwise, the node is split, and the insertion is propagated upwards.Deletion: Deleting a key-value pair from a B+ tree follows a similar process to insertion. After locating the leaf node containing the key to be deleted, the key is removed. If the node becomes underflowed, it may borrow keys from sibling nodes or merge with them to maintain balance....

Searching in B+ Tree:

Searching in a B-tree involves traversing the tree from the root to find the node that might contain the desired key....

Insertion in B+ Tree:

Insertion in a B+ tree involves placing a new key-value pair into the appropriate leaf node and ensuring tree balance through splits and updates....

Deletion in B+ Tree:

Deletion in a B+ tree involves removing a key-value pair, adjusting the structure of the tree, and potentially merging or redistributing nodes to maintain B+ tree properties....

Contact Us