logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Implement B-Tree

Posted on February 3, 2014 by Manish
This C++ program implements the B-Tree data structure. B-tree is a tree data structure that keeps data sorted and allows searches, sequential access and insertions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can have more than two children

Here is the source code of the C++ program to display a B-Tree with a degree of 6. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Implement B-Tree
  3.  */
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<iostream>
  7. using namespace std;
  8. struct BTreeNode
  9. {
  10.     int *data;
  11.     BTreeNode **child_ptr;
  12.     bool leaf;
  13.     int n;
  14. }*root = NULL, *np = NULL, *x = NULL;
  15. BTreeNode * init()
  16. {
  17.     int i;
  18.     np = new BTreeNode;
  19.     np->data = new int[5];
  20.     np->child_ptr = new BTreeNode *[6];
  21.     np->leaf = true;
  22.     np->n = 0;
  23.     for (i = 0; i < 6; i++)
  24.     {
  25.         np->child_ptr[i] = NULL;
  26.     }
  27.     return np;
  28. }
  29. void traverse(BTreeNode *p)
  30. {
  31.     cout<<endl;
  32.     int i;
  33.     for (i = 0; i < p->n; i++)
  34.     {
  35.         if (p->leaf == false)
  36.         {
  37.             traverse(p->child_ptr[i]);
  38.         }
  39.         cout << " " << p->data[i];
  40.     } 
  41.     if (p->leaf == false)
  42.     {
  43.         traverse(p->child_ptr[i]);
  44.     }
  45.     cout<<endl;
  46. }
  47. void sort(int *p, int n)
  48. {
  49.     int i, j, temp;
  50.     for (i = 0; i < n; i++)
  51.     {
  52.         for (j = i; j <= n; j++)
  53.         {
  54.             if (p[i] > p[j])
  55.             {
  56.                 temp = p[i];
  57.                 p[i] = p[j];
  58.                 p[j] = temp;
  59.             }
  60.         }
  61.     }
  62. }
  63. int split_child(BTreeNode *x, int i)
  64. {
  65.     int j, mid;
  66.     BTreeNode *np1, *np3, *y;
  67.     np3 = init();
  68.     np3->leaf = true;
  69.     if (i == -1)
  70.     {
  71.         mid = x->data[2];
  72.         x->data[2] = 0;
  73.         x->n--;
  74.         np1 = init();
  75.         np1->leaf = false;
  76.         x->leaf = true;
  77.         for (j = 3; j < 5; j++)
  78.         {
  79.             np3->data[j - 3] = x->data[j];
  80.             np3->child_ptr[j - 3] = x->child_ptr[j];
  81.             np3->n++;
  82.             x->data[j] = 0;
  83.             x->n--;
  84.         }
  85.         for (j = 0; j < 6; j++)
  86.         {
  87.             x->child_ptr[j] = NULL;
  88.         }
  89.         np1->data[0] = mid;
  90.         np1->child_ptr[np1->n] = x;
  91.         np1->child_ptr[np1->n + 1] = np3;
  92.         np1->n++;
  93.         root = np1;
  94.     }
  95.     else
  96.     {
  97.         y = x->child_ptr[i];
  98.         mid = y->data[2];
  99.         y->data[2] = 0;
  100.         y->n--;
  101.         for (j = 3; j < 5; j++)
  102.         {
  103.             np3->data[j - 3] = y->data[j];
  104.             np3->n++;
  105.             y->data[j] = 0;
  106.             y->n--;
  107.         }
  108.         x->child_ptr[i + 1] = y;
  109.         x->child_ptr[i + 1] = np3; 
  110.     }
  111.     return mid;
  112. }
  113. void insert(int a)
  114. {
  115.     int i, temp;
  116.     x = root;
  117.     if (x == NULL)
  118.     {
  119.         root = init();
  120.         x = root;
  121.     }
  122.     else
  123.     {
  124.         if (x->leaf == true && x->n == 5)
  125.         {
  126.             temp = split_child(x, -1);
  127.             x = root;
  128.             for (i = 0; i < (x->n); i++)
  129.             {
  130.                 if ((a > x->data[i]) && (a < x->data[i + 1]))
  131.                 {
  132.                     i++;
  133.                     break;
  134.                 }
  135.                 else if (a < x->data[0])
  136.                 {
  137.                     break;
  138.                 }
  139.                 else
  140.                 {
  141.                     continue;
  142.                 }
  143.             }
  144.             x = x->child_ptr[i];
  145.         }
  146.         else
  147.         {
  148.             while (x->leaf == false)
  149.             {
  150.             for (i = 0; i < (x->n); i++)
  151.             {
  152.                 if ((a > x->data[i]) && (a < x->data[i + 1]))
  153.                 {
  154.                     i++;
  155.                     break;
  156.                 }
  157.                 else if (a < x->data[0])
  158.                 {
  159.                     break;
  160.                 }
  161.                 else
  162.                 {
  163.                     continue;
  164.                 }
  165.             }
  166.                 if ((x->child_ptr[i])->n == 5)
  167.                 {
  168.                     temp = split_child(x, i);
  169.                     x->data[x->n] = temp;
  170.                     x->n++;
  171.                     continue;
  172.                 }
  173.                 else
  174.                 {
  175.                     x = x->child_ptr[i];
  176.                 }
  177.             }
  178.         }
  179.     }
  180.     x->data[x->n] = a;
  181.     sort(x->data, x->n);
  182.     x->n++;
  183. }
  184. int main()
  185. {
  186.     int i, n, t;
  187.     cout<<"enter the no of elements to be inserted\n";
  188.     cin>>n;
  189.     for(i = 0; i < n; i++)
  190.     {
  191.           cout<<"enter the element\n";
  192.           cin>>t;
  193.           insert(t);
  194.     }
  195.     cout<<"traversal of constructed tree\n";
  196.     traverse(root);
  197.     getch();
  198. }

advertisement
Output
 
enter the no of elements to be inserted
8
enter the element
10
enter the element
20
enter the element
5
enter the element
6
enter the element
12
enter the element
30
enter the element
7
enter the element
17
traversal of constructed tree
 5 6 7
 10
 12 17 20 30

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ program to Solve Tower of Hanoi Problem using Binary Value
» Next Page - C++ program to Solve Tower of Hanoi Problem using Stacks

« Linux Program to Duplicate a File Descriptor with fcntl()
C++ Program to Describe the Representation of Graph using Adjacency List »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Hard Graph Problems & Algorithms
  2. C Programming Examples on Graph Problems & Algorithms
  3. C Programming Examples on Data-Structures
  4. Java Programming Examples on Graph Problems & Algorithms
  5. C Programming Examples using Recursion
  6. C# Programming Examples on Data Structures
  7. C Programming Examples on Linked List
  8. C++ Programming Examples on Graph Problems & Algorithms
  9. C Programming Examples without using Recursion
  10. C Programming Examples on Trees
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.