Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Data Structures Code Examples

C++ Program to Implement Binomial Heap

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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
/* C++ Program to Implement Binomial Heap */ #include <iostream> #include <cstdlib> using namespace std; /* Node Declaration */ struct node { int n; int degree; node* parent; node* child; node* sibling; }; /* Class Declaration */ class BinomialHeap { private: node *H; node *Hr; int count; public: node* Initializeheap(); int Binomial_link(node*, node*); node* Create_node(int); node* Union(node*, node*); node* Insert(node*, node*); node* Merge(node*, node*); node* Extract_Min(node*); int Revert_list(node*); int Display(node*); node* Search(node*, int); int Decrease_key(node*, int, int); int Delete(node*, int); BinomialHeap() { H = Initializeheap(); Hr = Initializeheap(); int count = 1; } }; /* Initialize Heap */ node* BinomialHeap::Initializeheap() { node* np; np = NULL; return np; } /* Linking nodes in Binomial Heap */ int BinomialHeap::Binomial_link(node* y, node* z) { y->parent = z; y->sibling = z->child; z->child = y; z->degree = z->degree + 1; } /* Create Nodes in Binomial Heap */ node* BinomialHeap::Create_node(int k) { node* p = new node; p->n = k; return p; } /* Insert Nodes in Binomial Heap */ node* BinomialHeap::Insert(node* H, node* x) { node* H1 = Initializeheap(); x->parent = NULL; x->child = NULL; x->sibling = NULL; x->degree = 0; H1 = x; H = Union(H, H1); return H; } /* Union Nodes in Binomial Heap */ node* BinomialHeap::Union(node* H1, node* H2) { node *H = Initializeheap(); H = Merge(H1, H2); if (H == NULL) return H; node* prev_x; node* next_x; node* x; prev_x = NULL; x = H; next_x = x->sibling; while (next_x != NULL) { if ((x->degree != next_x->degree) || ((next_x->sibling != NULL) && (next_x->sibling)->degree == x->degree)) { prev_x = x; x = next_x; } else { if (x->n <= next_x->n) { x->sibling = next_x->sibling; Binomial_link(next_x, x); } else { if (prev_x == NULL) H = next_x; else prev_x->sibling = next_x; Binomial_link(x, next_x); x = next_x; } } next_x = x->sibling; } return H; } /* Merge Nodes in Binomial Heap */ node* BinomialHeap::Merge(node* H1, node* H2) { node* H = Initializeheap(); node* y; node* z; node* a; node* b; y = H1; z = H2; if (y != NULL) { if (z != NULL) { if (y->degree <= z->degree) H = y; else if (y->degree > z->degree) H = z; } else H = y; } else H = z; while (y != NULL && z != NULL) { if (y->degree < z->degree) { y = y->sibling; } else if (y->degree == z->degree) { a = y->sibling; y->sibling = z; y = a; } else { b = z->sibling; z->sibling = y; z = b; } } return H; } /* Display Binomial Heap */ int BinomialHeap::Display(node* H) { if (H == NULL) { cout<<"The Heap is empty"<<endl; return 0; } cout<<"The root nodes are: "<<endl; node* p; p = H; while (p != NULL) { cout<<p->n; if (p->sibling != NULL) cout<<"-->"; p = p->sibling; } cout<<endl; } /* Extract Minimum */ node* BinomialHeap::Extract_Min(node* H1) { Hr = NULL; node* t = NULL; node* x = H1; if (x == NULL) { cout<<"Nothing to Extract"<<endl; return x; } int min = x->n; node* p = x; while (p->sibling != NULL) { if ((p->sibling)->n < min) { min = (p->sibling)->n; t = p; x = p->sibling; } p = p->sibling; } if (t == NULL && x->sibling == NULL) H1 = NULL; else if (t == NULL) H1 = x->sibling; else if (t->sibling == NULL) t = NULL; else t->sibling = x->sibling; if (x->child != NULL) { Revert_list(x->child); (x->child)->sibling = NULL; } H = Union(H1, Hr); return x; } /* Reverse List */ int BinomialHeap::Revert_list(node* y) { if (y->sibling != NULL) { Revert_list(y->sibling); (y->sibling)->sibling = y; } else { Hr = y; } } /* Search Nodes in Binomial Heap */ node* BinomialHeap::Search(node* H, int k) { node* x = H; node* p = NULL; if (x->n == k) { p = x; return p; } if (x->child != NULL && p == NULL) p = Search(x->child, k); if (x->sibling != NULL && p == NULL) p = Search(x->sibling, k); return p; } /* Decrease key of a node */ int BinomialHeap::Decrease_key(node* H, int i, int k) { int temp; node* p; node* y; node* z; p = Search(H, i); if (p == NULL) { cout<<"Invalid choice of key"<<endl; return 0; } if (k > p->n) { cout<<"Error!! New key is greater than current key"<<endl; return 0; } p->n = k; y = p; z = p->parent; while (z != NULL && y->n < z->n) { temp = y->n; y->n = z->n; z->n = temp; y = z; z = z->parent; } cout<<"Key reduced successfully"<<endl; } /* Delete Nodes in Binomial Heap */ int BinomialHeap::Delete(node* H, int k) { node* np; if (H == NULL) { cout<<"\nHEAP EMPTY!!!!!"; return 0; } Decrease_key(H, k, -1000); np = Extract_Min(H); if (np != NULL) cout<<"Node Deleted Successfully"<<endl; } /* Main Contains Menu */ int main() { int n, m, l, i; BinomialHeap bh; node* p; node *H; H = bh.Initializeheap(); char ch; while (1) { cout<<"----------------------------"<<endl; cout<<"Operations on Binomial heap"<<endl; cout<<"----------------------------"<<endl; cout<<"1)Insert Element in the heap"<<endl; cout<<"2)Extract Minimum key node"<<endl; cout<<"3)Decrease key of a node"<<endl; cout<<"4)Delete a node"<<endl; cout<<"5)Display Heap"<<endl; cout<<"6)Exit"<<endl; cout<<"Enter Your Choice: "; cin>>l; switch(l) { case 1: cout<<"Enter the element to be inserted: "; cin>>m; p = bh.Create_node(m); H = bh.Insert(H, p); break; case 2: p = bh.Extract_Min(H); if (p != NULL) cout<<"The node with minimum key: "<<p->n<<endl; else cout<<"Heap is empty"<<endl; break; case 3: cout<<"Enter the key to be decreased: "; cin>>m; cout<<"Enter new key value: "; cin>>l; bh.Decrease_key(H, m, l); break; case 4: cout<<"Enter the key to be deleted: "; cin>>m; bh.Delete(H, m); break; case 5: cout<<"The Heap is: "<<endl; bh.Display(H); break; case 6: exit(1); default: cout<<"Wrong Choice"; } } return 0; }

Search range for subsequence. Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2). This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end. The function shall not modify any of its arguments.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the program.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

Return the smallest. Returns the smallest of a and b. If both are equivalent, a is returned. min() function is a library function of algorithm header, it is used to find the smallest value from given two values, it accepts two values and returns the smallest value and if both the values are the same it returns the first value. The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. The function uses operator< (or comp, if provided) to compare the values.



Function to insert a node in the tree. Inorder recursive & nonrecursive traversal. 'Preorder' recursive & nonrecursive traversal. 'Postrder' recursive traversal. Postorder non recursive







Operator is a symbol that is used to perform "mathematical" or "logical" manipulations. +, -, *, /, %, Addition, Subtraction, Division and Multiplication, Modulus, ++, -- Increment and