Found 27758 Articles for Server Side Programming

C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree

George John
Updated on 24-Jun-2020 10:52:03

926 Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The preorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Root, Left, Right).An example of Preorder traversal of a binary tree is as follows.A binary tree is given as follows.Preorder Traversal is: 5 3 2 4 8 9A program to perform preorder non-recursive traversal is given as follows.Example Live Demo#include #include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct ... Read More

C++ Program to Find Factorial of a Number using Dynamic Programming

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

5K+ Views

The factorial of a positive integer n is equal to 1*2*3*...n. Factorial of a negative number does not exist. Here a C++ program is given to find out the factorial of a given input using dynamic programming.AlgorithmBegin    fact(int n):       Read the number n       Initialize       i = 1, result[1000] = {0}       result[0] = 1       for i = 1 to n          result[i] = I * result[i-1]    Print result EndExample Code#include using namespace std; int result[1000] = {0}; int fact(int n) {    if (n >= 0) {       result[0] = 1;       for (int i = 1; i

C++ Program to Sort an Array of 10 Elements Using Heap Sort Algorithm

Arjun Thakur
Updated on 12-Feb-2020 07:17:15

2K+ Views

Heap Sort is based on the binary heap data structure. In the binary heap the child nodes of a parent node are smaller than or equal to it in the case of a max heap, and the child nodes of a parent node are greater than or equal to it in the case of a min heap.An example that explains all the steps in Heap Sort is as follows.The original array with 10 elements before sorting is −207154101590237725This array is built into a binary max heap using max-heapify. This max heap represented as an array is given as follows.907720542515123710The root ... Read More

C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree

Chandu yadav
Updated on 24-Jun-2020 09:47:24

4K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The postorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Left, Right, Root).An example of postorder traversal of a binary tree is as follows.A binary tree is given as follows.Postorder Traversal is: 1 5 4 8 6The program to perform post-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree

George John
Updated on 24-Jun-2020 09:48:29

12K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The inorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Left, Root, Right).An example of Inorder traversal of a binary tree is as follows.A binary tree is given as follows.Inorder Traversal is: 1 4 5 6 8The program to perform in-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree

Ankith Reddy
Updated on 24-Jun-2020 09:49:39

6K+ Views

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The preorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Root, Left, Right).An example of Preorder traversal of a binary tree is as follows.A binary tree is given as follows.Preorder Traversal is: 6 4 1 5 8The program to perform pre-order recursive traversal is given as follows.Example Live Demo#include using namespace std; struct node {    int data;    struct node *left;    struct node *right; }; struct node *createNode(int val) ... Read More

C++ Program to Check Multiplicability of Two Matrices

Arjun Thakur
Updated on 24-Jun-2020 09:50:27

214 Views

Two matrices are said to be multiplicable if they can be multiplied. This is only possible if the number of columns of the first matrix is equal to the number of rows of the second matrix. For example.Number of rows in Matrix 1 = 3 Number of columns in Matrix 1 = 2 Number of rows in Matrix 2 = 2 Number of columns in Matrix 2 = 5 Matrix 1 and Matrix 2 are multiplicable as the number of columns of Matrix 1 is equal to the number of rows of Matrix 2.A program to check the ... Read More

C++ Program to Compute Determinant of a Matrix

Chandu yadav
Updated on 24-Jun-2020 09:51:44

9K+ Views

The determinant of a square matrix can be computed using its element values. The determinant of a matrix A can be denoted as det(A) and it can be called the scaling factor of the linear transformation described by the matrix in geometry.An example of the determinant of a matrix is as follows.The matrix is: 3 1 2 7 The determinant of the above matrix = 7*3 - 2*1 = 21 - 2 = 19 So, the determinant is 19.The program to compute the determinant of a matrix is as follows.Example Live Demo#include #include using namespace std; int determinant( int matrix[10][10], int ... Read More

C++ Program to Check if a Matrix is Invertible

George John
Updated on 24-Jun-2020 09:53:26

226 Views

The determinant of a matrix can be used to find if it is invertible or not. The matrix is invertible if the determinant is non-zero. So if the determinant comes out to be zero, the matrix is not invertible. For example −The given matrix is: 4 2 1 2 1 1 9 3 2 The determinant of the above matrix is: 3 So the matrix is invertible.A program that checks if a matrix is invertible or not is as follows.Example Live Demo#include #include using namespace std; int determinant( int matrix[10][10], int n) {    int det = 0;    int ... Read More

C++ Program to Multiply two Matrices by Passing Matrix to Function

Ankith Reddy
Updated on 24-Jun-2020 09:55:04

2K+ Views

A matrix is a rectangular array of numbers that is arranged in the form of rows and columns.An example of a matrix is as follows.A 3*4 matrix has 3 rows and 4 columns as shown below.8 6 3 5 7 1 9 2 5 1 9 8A program that multiplies two matrices by passing the matrices to functions is as follows.Example Live Demo#include using namespace std; void MatrixMultiplication(int a[2][3],int b[3][3]) {    int product[10][10], r1=2, c1=3, r2=3, c2=3, i, j, k;    if (c1 != r2) {       cout

Advertisements