Q4 a.
Binary tree computations always involves traversal of the tree. With your
knowledge in each type of traversal.
i. Perform and write the algorithm of all the types of traversal methods on a
binary tree. (AP)10 marks
ii. Perform a space and time complexity of each traversal type been
implemented in i. above. (AN)5 marks
3b. Using C++ or java create a square matrix A[][] of order (M*M) where ‘M’ is
the number of rows and columns such that M must be greater than 2 and less
than 20. Allow the user to input integers in the matrix. Display appropriate error
message for an invalid input.
Perform the following task on the matrix data
i. Display the input matrix from the user.
ii. Create a mirror of the inputted matrix
iii. Display the mirror image matrix (AP) 10 marks
For instance:
Example 1.
Where M=3
OUTPUT: Original Matrix
4 16 12
8 2 14
6 1 3
OUTPUT: ORIGINAL MATRIX
4 16 12
Example 2.
INPUT: M =22
OUTPUT: SIZE

Q4 a Binary tree computations always involves traversal of the tree With your knowledge in each type of traversal i Perform and write the algorithm of all the t class=

Respuesta :

The Java program that accepts a matrix of M × N order and then interchanges diagonals of the matrix is given below:

Steps:  

  • 1. We can only interchange diagonals for a square matrix.
  • 2. Therefore, we would have to create a square matrix of size [M × M].
  • 3. We would check whether the matrix is a square matrix or not. If the matrix is square then follow step 3 else terminate the program.
  • 4. Apply logic for interchange diagonal of the matrix some logic is given below.

Java Code

//  Java Program to Accept a Matrix of Order M x N &

//  Interchange the Diagonals

import java.util.Scanner;

public class InterchangeDiagonals {

   public static void main(String[] args)

   {

       // declare variable

       int m, n, i, j, temp;

       // create a object of scanner class

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter number of rows ");

       // take number of rows

       m = sc.nextInt();

       System.out.print("Enter number of columns ");

       // take number of columns

       n = sc.nextInt();

       // declare a mxn order array

       int a[][] = new int[m][n];

       // if block it's execute when m is equals to n

       if (m == n) {

           System.out.println(

               "Enter all the values of matrix ");

           // take the matrix inputs

           for (i = 0; i < m; i++) {

               for (j = 0; j < n; j++) {

                   a[i][j] = sc.nextInt();

               }

           }

           System.out.println("original Matrix:");

           // print the original matrix

           for (i = 0; i < m; i++) {

               for (j = 0; j < n; j++) {

                   System.out.print(a[i][j] + " ");

               }

               System.out.println("");

           }

          // perform interchange

           for (j = 0; j < m; j++) {

               temp = a[j][j];

               a[j][j] = a[j][n - 1 - j];

               a[j][n - 1 - j] = temp;

           }

           System.out.println(

               " after interchanging diagonals of matrix ");

           // print interchanged matrix

           for (i = 0; i < m; i++) {

               for (j = 0; j < n; j++) {

                   System.out.print(a[i][j] + " ");

               }

               System.out.println("");

           }

       }

       // else block it's only execute when m is not equals

       // to n

       else {

           System.out.println("Rows not equal to columns");

       }

   }

}

Read more about java programming here:

https://brainly.com/question/18554491

#SPJ1