B-Tree using Class
Write a C++ Program to implement B-Tree using Class. Here’s a Simple Program to implement B-Tree using Class using Linked Lists in C++ Programming Language.
What is Class and Objects in C++?
- The classes are the most important feature of C++ that leads to Object Oriented programming.
- Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
- The variables inside class definition are called as data members and the functions are called member functions.
- Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
- Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
- Each object has different data variables. Objects are initialized using special class functions called Constructors.
Below is the source code for Program to implement B-Tree using Class using Linked Lists which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
/* C++ Program to implement B-Tree using Class using Linked Lists */ #include <iostream> #include <stdlib.h> using namespace std; const int MAX = 4 ; const int MIN = 2 ; struct btnode { int count ; int value[MAX + 1] ; btnode *child[MAX + 1] ; } ; class btree { private : btnode *root ; public : btree( ) ; void insert ( int val ) ; int setval ( int val, btnode *n, int *p, btnode **c ) ; static btnode * search ( int val, btnode *root, int *pos ) ; static int searchnode ( int val, btnode *n, int *pos ) ; void fillnode ( int val, btnode *c, btnode *n, int k ) ; void split ( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode ) ; void del ( int val ) ; int delhelp ( int val, btnode *root ) ; void clear ( btnode *root, int k ) ; void copysucc ( btnode *root, int i ) ; void restore ( btnode *root, int i ) ; void rightshift ( int k ) ; void leftshift ( int k ) ; void merge ( int k ) ; void show( ) ; static void display ( btnode *root ) ; static void deltree ( btnode *root ) ; ~btree( ) ; } ; btree :: btree( ) { root = NULL ; } void btree :: insert ( int val ) { int i ; btnode *c, *n ; int flag ; flag = setval ( val, root, &i, &c ) ; if ( flag ) { n = new btnode ; n -> count = 1 ; n -> value[1] = i ; n -> child[0] = root ; n -> child[1] = c ; root = n ; } } int btree :: setval ( int val, btnode *n, int *p, btnode **c ) { int k ; if ( n == NULL ) { *p = val ; *c = NULL ; return 1 ; } else { if ( searchnode ( val, n, &k ) ) cout << endl << "Key value already exists." << endl ; if ( setval ( val, n -> child[k], p, c ) ) { if ( n -> count < MAX ) { fillnode ( *p, *c, n, k ) ; return 0 ; } else { split ( *p, *c, n, k, p, c ) ; return 1 ; } } return 0 ; } } btnode * btree :: search ( int val, btnode *root, int *pos ) { if ( root == NULL ) return NULL ; else { if ( searchnode ( val, root, pos ) ) return root ; else return search ( val, root -> child[*pos], pos ) ; } } int btree :: searchnode ( int val, btnode *n, int *pos ) { if ( val < n -> value[1] ) { *pos = 0 ; return 0 ; } else { *pos = n -> count ; while ( ( val < n -> value[*pos] ) && *pos > 1 ) ( *pos )-- ; if ( val == n -> value[*pos] ) return 1 ; else return 0 ; } } void btree :: fillnode ( int val, btnode *c, btnode *n, int k ) { int i ; for ( i = n -> count ; i > k ; i-- ) { n -> value[i + 1] = n -> value[i] ; n -> child[i + 1] = n -> child[i] ; } n -> value[k + 1] = val ; n -> child[k + 1] = c ; n -> count++ ; } void btree :: split ( int val, btnode *c, btnode *n, int k, int *y, btnode **newnode ) { int i, mid ; if ( k <= MIN ) mid = MIN ; else mid = MIN + 1 ; *newnode = new btnode ; for ( i = mid + 1 ; i <= MAX ; i++ ) { ( *newnode ) -> value[i - mid] = n -> value[i] ; ( *newnode ) -> child[i - mid] = n -> child[i] ; } ( *newnode ) -> count = MAX - mid ; n -> count = mid ; if ( k <= MIN ) fillnode ( val, c, n, k ) ; else fillnode ( val, c, *newnode, k - mid ) ; *y = n -> value[n -> count] ; ( *newnode ) -> child[0] = n -> child[n -> count] ; n -> count-- ; } void btree :: del ( int val ) { btnode * temp ; if ( ! delhelp ( val, root ) ) cout << endl << "Value " << val << " not found." ; else { if ( root -> count == 0 ) { temp = root ; root = root -> child[0] ; delete temp ; } } } int btree :: delhelp ( int val, btnode *root ) { int i ; int flag ; if ( root == NULL ) return 0 ; else { flag = searchnode ( val, root, &i ) ; if ( flag ) { if ( root -> child[i - 1] ) { copysucc ( root, i ) ; flag = delhelp ( root -> value[i], root -> child[i] ) ; if ( !flag ) cout << endl << "Value " << val << " not found." ; } else clear ( root, i ) ; } else flag = delhelp ( val, root -> child[i] ) ; if ( root -> child[i] != NULL ) { if ( root -> child[i] -> count < MIN ) restore ( root, i ) ; } return flag ; } } void btree :: clear ( btnode *root, int k ) { int i ; for ( i = k + 1 ; i <= root -> count ; i++ ) { root -> value[i - 1] = root -> value[i] ; root -> child[i - 1] = root -> child[i] ; } root -> count-- ; } void btree :: copysucc ( btnode *root, int i ) { btnode *temp = root -> child[i] ; while ( temp -> child[0] ) temp = temp -> child[0] ; root -> value[i] = temp -> value[1] ; } void btree :: restore ( btnode *root, int i ) { if ( i == 0 ) { if ( root -> child [1] -> count > MIN ) leftshift ( 1 ) ; else merge ( 1 ) ; } else { if ( i == root -> count ) { if ( root -> child[i - 1] -> count > MIN ) rightshift ( i ) ; else merge ( i ) ; } else { if ( root -> child[i - 1] -> count > MIN ) rightshift ( i ) ; else { if ( root -> child[i + 1] -> count > MIN ) leftshift ( i + 1 ) ; else merge ( i ) ; } } } } void btree :: rightshift ( int k ) { int i ; btnode *temp ; temp = root -> child[k] ; for ( i = temp -> count ; i > 0 ; i-- ) { temp -> value[i + 1] = temp -> value[i] ; temp -> child[i + 1] = temp -> child[i] ; } temp -> child[1] = temp -> child[0] ; temp -> count++ ; temp -> value[1] = root -> value[k] ; temp = root -> child[k - 1] ; root -> value[k] = temp -> value[temp -> count] ; root -> child[k] -> child [0] = temp -> child[temp -> count] ; temp -> count-- ; } void btree :: leftshift ( int k ) { btnode *temp ; temp = root -> child[k - 1] ; temp -> count++ ; temp -> value[temp -> count] = root -> value[k] ; temp -> child[temp -> count] = root -> child[k] -> child[0] ; temp = root -> child[k] ; root -> value[k] = temp -> value[1] ; temp -> child[0] = temp -> child[1] ; temp -> count-- ; for ( int i = 1 ; i <= temp -> count ; i++ ) { temp -> value[i] = temp -> value[i + 1] ; temp -> child[i] = temp -> child[i + 1] ; } } void btree :: merge ( int k ) { btnode *temp1, *temp2 ; temp1 = root -> child[k] ; temp2 = root -> child[k - 1] ; temp2 -> count++ ; temp2 -> value[temp2 -> count] = root -> value[k] ; temp2 -> child[temp2 -> count] = root -> child[0] ; for ( int i = 1 ; i <= temp1 -> count ; i++ ) { temp2 -> count++ ; temp2 -> value[temp2 -> count] = temp1 -> value[i] ; temp2 -> child[temp2 -> count] = temp1 -> child[i] ; } for ( int i = k ; i < root -> count ; i++ ) { root -> value[i] = root -> value[i + 1] ; root -> child[i] = root -> child[i + 1] ; } root -> count-- ; delete temp1 ; } void btree :: show( ) { display ( root ) ; } void btree :: display ( btnode *root ) { int i=0; if ( root != NULL ) { for ( i = 0 ; i < root -> count ; i++ ) { display ( root -> child[i] ) ; cout << root -> value[i + 1] << "\t" ; } display ( root -> child[i] ) ; } } void btree :: deltree ( btnode *root ) { int i=0; if ( root != NULL ) { for (i = 0 ; i < root -> count ; i++ ) { deltree ( root -> child[i] ) ; delete ( root -> child[i] ) ; } deltree ( root -> child[i] ) ; delete ( root -> child[i] ) ; } } btree :: ~btree( ) { deltree ( root ) ; } int main( ) { btree b ; int arr[ ] = { 27, 42, 22, 47, 32, 2, 51, 40, 13 } ; int sz = sizeof ( arr ) / sizeof ( int ) ; for ( int i = 0 ; i < sz ; i++ ) b.insert ( arr[i] ) ; cout << "B-tree of order 5:" << endl ; b.show( ) ; b.del ( 22 ) ; b.del ( 11 ) ; cout << "\n\nB-tree after deletion of values:" << endl ; b.show( ) ; cout<<"\n"; return 0; } |
OUTPUT : :
1 2 3 4 5 6 7 8 9 10 11 |
/* C++ Program to implement B-Tree using Class using Linked Lists */ B-tree of order 5: 2 13 22 27 32 40 42 47 51 Value 11 not found. B-tree after deletion of values: 2 13 27 32 40 42 47 51 Process returned 0 |
Above is the source code and output for C++ Program to implement B-Tree using Class using Linked Lists which is successfully compiled and run on Windows System to produce desired output.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.
Thanks for reading the post….