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++ > Computer Graphics Code Examples

Program to Implement Ternary Tree

/* Program to Implement Ternary Tree This C++ program implements the ternary tree, is a tree data structure in which each node has at most three child nodes, usually distinguished as "left", "mid" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. */ #include<stdio.h> #include<stdlib.h> #include<iostream> #include<conio.h> using namespace std; struct Node { char data; unsigned isEndOfString: 1; struct Node *left, *eq, *right; }*temp = NULL; struct Node* newNode(char data) { temp = new Node; temp->data = data; temp->isEndOfString = 0; temp->left = temp->eq = temp->right = NULL; return temp; } void insert(struct Node** root, char *word) { if (!(*root)) *root = newNode(*word); if ((*word) < (*root)->data) insert(&( (*root)->left ), word); else if ((*word) > (*root)->data) insert(&( (*root)->right ), word); else { if (*(word+1)) insert(&( (*root)->eq ), word+1); else (*root)->isEndOfString = 1; } } void traverseTSTUtil(struct Node* root, char* buffer, int depth) { if (root) { traverseTSTUtil(root->left, buffer, depth); buffer[depth] = root->data; if (root->isEndOfString) { buffer[depth+1] = '\0'; cout<<buffer<<endl; } traverseTSTUtil(root->eq, buffer, depth + 1); traverseTSTUtil(root->right, buffer, depth); } } void traverseTST(struct Node* root) { char buffer[50]; traverseTSTUtil(root, buffer, 0); } int searchTST(struct Node *root, char *word) { if (!root) return 0; if (*word < (root)->data) return searchTST(root->left, word); else if (*word > (root)->data) return searchTST(root->right, word); else { if (*(word+1) == '\0') return root->isEndOfString; return searchTST(root->eq, word+1); } } int main() { struct Node *root = NULL; insert(&root, "cat"); insert(&root, "cats"); insert(&root, "up"); insert(&root, "bug"); cout<<"Following is traversal of ternary search tree\n"; traverseTST(root); cout<<"\nFollowing are search results for cats, bu and cat respectively\n"; searchTST(root, "cats")? cout<<"Found\n": cout<<"Not Found\n"; searchTST(root, "bu")? cout<<"Found\n": cout<<"Not Found\n"; searchTST(root, "cat")? cout<<"Found\n": cout<<"Not Found\n"; getch(); }

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.

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.

#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.

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++.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

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.

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.

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.











First ask to enter the array size then it will ask to enter the array elements, then it will finally ask to enter a number to be search in array to check whether it is present in the array or not