C++ Program to Implement Fusion Tree



A fusion tree is a tree data structure that implements an associative array on w-bit integers. Here, W is the number of bits in the integer.

A fusion tree is used to maintain the ordered set of elements. It uses a combination of a B-tree and a hash table that helps reduce the time complexity of the operations like insertion, deletion, and searching in the tree.

How Fusion Tree Works?

The following are the factors that should be considered while implementing the fusion tree:

  • Bit manipulation: The tree extracts specific bits from stored integers and processes them in order to speed up the operations.
  • Parallelism: It uses 64 or 128 bits machine words to compare and manipulate multiple keys at same time.
  • Sketching: Summarizing integers into smaller representation (called as sketches) which can be used to compare the integers. It is done by extracting most important bits from the integers. that reduces the number of comparisons it usually requires to compare two integers.
  • Precomputed Operations: It pre-computes the operations like min, max, successor and predecessor which are used frequently in the tree.

Operations on Fusion Tree

Following are the operations that can be performed on a fusion tree:

  • Insertion
  • Deletion
  • Searching

Implementation of the Fusion Tree

Following are the steps to create and insert elements in the fusion tree:

  1. Create a struct node and define the root node as null.
  2. Insert the elements in the tree.
  3. If the tree is empty, create a new node and insert the element in it.
  4. If the tree is not empty, find the appropriate node to insert the element.
  5. If the node is full, split the node and insert the element in the appropriate node.
  6. Repeat the steps 4 and 5 until all the elements are inserted.

Example

Now, let's implement the fusion tree in C++ how we can create and insert nodes:

#include <iostream>
using namespace std;

#define W 32
#define B 4

struct node {
   int keys[B];
   struct node *child[B + 1];
   int n;
   int leaf;
};

struct node *root = NULL;

struct node *createNode() {
   struct node *newNode = new node;
   newNode->n = 0;
   newNode->leaf = 1;
   for (int i = 0; i <= B; i++) {
      newNode->child[i] = NULL;
   }
   return newNode;
}

void splitChild(struct node *parent, int i, struct node *fullChild) {
   struct node *halfChild = createNode();
   halfChild->leaf = fullChild->leaf;
   halfChild->n = B / 2;

   for (int j = 0; j < B / 2; j++) {
      halfChild->keys[j] = fullChild->keys[j + B / 2];
   }

   if (fullChild->leaf == 0) {
      for (int j = 0; j < B / 2 + 1; j++) {
         halfChild->child[j] = fullChild->child[j + B / 2];
      }
   }

   fullChild->n = B / 2;

   for (int j = parent->n; j > i; j--) {
      parent->child[j + 1] = parent->child[j];
   }
   parent->child[i + 1] = halfChild;

   for (int j = parent->n - 1; j >= i; j--) {
      parent->keys[j + 1] = parent->keys[j];
   }
   parent->keys[i] = fullChild->keys[B / 2];
   parent->n++;
}

void insertNonFull(struct node *current, int key) {
   int i = current->n - 1;
   if (current->leaf) {
      while (i >= 0 && current->keys[i] > key) {
         current->keys[i + 1] = current->keys[i];
         i--;
      }
      current->keys[i + 1] = key;
      current->n++;
   } else {
      while (i >= 0 && current->keys[i] > key) {
         i--;
      }
      i++;
      if (current->child[i]->n == B) {
         splitChild(current, i, current->child[i]);
         if (current->keys[i] < key) {
            i++;
         }
      }
      insertNonFull(current->child[i], key);
   }
}
void insert(int key) {
   if (root == NULL) {
      root = createNode();
      root->keys[0] = key;
      root->n = 1;
   } else {
      if (root->n == B) {
         struct node *newNode = createNode();
         newNode->child[0] = root;
         root = newNode;
         splitChild(root, 0, newNode->child[0]);
      }
      insertNonFull(root, key);
   }
}

void display(struct node *root) {
   if (root == NULL) {
      return;
   }

   for (int i = 0; i < root->n; i++) {
      if (root->leaf == 0) {
         display(root->child[i]);
      }
      cout << root->keys[i] << " ";
   }

   if (root->leaf == 0) {
      display(root->child[root->n]);
   }
}
int main() {
   insert(10);
   insert(15);
   insert(20);
   insert(25);
   cout << "Fusion Tree keys: ";
   display(root);
   return 0;
}

Following is the output of the code:

Fusion Tree keys: 10 15 20 25 

Search And Delete Operations in Fusion Tree

The search operation is used to find the element in the tree, and the delete operation is used to delete the element from the tree. Both operations in the fusion tree are similar to the insertion operation. We can implement these operations by modifying the above code:

Following are the steps to implement search and delete operations in a fusion tree:

  1. Search the element in the tree.
  2. If the element is found, delete the element from the tree.
  3. If the element is not found, display the message "Element not found".

Example

Let's see how we can implement search and delete operations in a fusion tree in C++:

#include <iostream>
using namespace std;

#define W 32
#define B 4

struct node{
   int keys[W];
   struct node *child[W+1];
   int n;
   int leaf;
};

struct node *root = NULL;

struct node *createNode(){
   struct node *newNode = new node;
   newNode->n = 0;
   newNode->leaf = 1;
   return newNode;
}

// Split a full child node
void splitChild(struct node *parent, int i, struct node *fullChild){
   struct node *halfChild = createNode();
   halfChild->leaf = fullChild->leaf;
   halfChild->n = B / 2;
   
   // Move second half of keys to the new child
   for (int j = 0; j < B / 2; j++){
      halfChild->keys[j] = fullChild->keys[j + B / 2];
   }
   
   if (!fullChild->leaf){
      // Move second half of children to the new child
      for (int j = 0; j < B / 2 + 1; j++){
         halfChild->child[j] = fullChild->child[j + B / 2];
      }
   }
   
   fullChild->n = B / 2;
   
   // Move children of parent to make space
   for (int j = parent->n; j > i; j--){
      parent->child[j + 1] = parent->child[j];
   }
   
   parent->child[i + 1] = halfChild;
   
   // Move keys in parent to make space
   for (int j = parent->n - 1; j >= i; j--){
      parent->keys[j + 1] = parent->keys[j];
   }
   
   parent->keys[i] = fullChild->keys[B / 2];
   parent->n++;
}

// Insert key into a non-full node
void insertNonFull(struct node *node, int key){
   int i = node->n - 1;
   if (node->leaf){
      // Find position to insert the key
      while (i >= 0 && node->keys[i] > key){
         node->keys[i + 1] = node->keys[i];
         i--;
      }
      node->keys[i + 1] = key;
      node->n++;
   }
   else{
      // Find child to insert into
      while (i >= 0 && node->keys[i] > key){
         i--;
      }
      i++;
      if (node->child[i]->n == B){
         splitChild(node, i, node->child[i]);
         if (node->keys[i] < key){
            i++;
         }
      }
   insertNonFull(node->child[i], key);
   }
}

// Insert key into the tree
void insert(int key){
   if (root == NULL){
      root = createNode();
      root->keys[0] = key;
      root->n = 1;
   }
   else{
      if (root->n == B){
         struct node *newNode = createNode();
         newNode->child[0] = root;
         root = newNode;
         splitChild(root, 0, newNode->child[0]);
      }
      insertNonFull(root, key);
   }
}

// Display the keys in the tree
void display(struct node *root){
   if (root != NULL){
      for (int i = 0; i < root->n; i++){
         if (root->leaf == 0){
            display(root->child[i]);
         }
         cout << root->keys[i] << " ";
      }
      if (root->leaf == 0){
         display(root->child[root->n]);
      }
      cout << endl;
   }
}

// Search the tree for a key
void search(int key){
   for (int i = 0; i < root->n; i++){
      if (root->keys[i] == key){
         cout << "Element " << key << "found " <<endl;
         return;
      }
   }
   cout << "Element " << key << " not found " << endl;
}

// Delete the element from the tree
void deleteElement(int key){
   for (int i = 0; i < root->n; i++){
      if (root->keys[i] == key){
         for (int j = i; j < root->n - 1; j++){
            root->keys[j] = root->keys[j + 1];
         }
         root->n--;
         cout << "Element " << key << " deleted " << endl;
         return;
      }
   }
   cout << "Element " << key << " not found " << endl;
}

int main(){
   insert(10);
   insert(15);
   insert(20);
   insert(25);
   cout << "Fusion Tree keys: ";
   display(root);
   search(15);
   deleteElement(15);
   search(15);
   cout << "Fusion Tree after deletion: ";
   display(root);
   return 0;
}

Following is the output:

Fusion Tree keys: 10 15 20 25 
Element 15found
Element 15 deleted
Element 15 not found
Fusion Tree after deletion: 10 20 25
Updated on: 2025-05-27T16:33:56+05:30

622 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements