In last couple of articles, we have learned about pre-order and in-order tree traversal in Java and today, you will learn about the post order traversal in binary tree. The post order traversal is also a depth-first algorithm because you go deep before you visit other nodes in same level. In post order traversal, you first visit left subtree, then right subtree and finally you print the value of node or root. That's why the value of root is always printed last on post order traversal. Like many tree algorithms, the easiest way to implement post-order traversal is by using recursion. In fact, if you know how to write pre-order using recursion, you can use the same algorithm with bit of adjustment to implement post order traversal. All you need to do is instead of printing the value of node first, just call the recursive method with left subtree as shown in our example.
Hello guys, continuing the tradition of this week, where I have mostly published articles about coding exercises for Java beginners, today also I am going to share an interesting coding problem, many of you have solved in your college or school days. Yes, it's about writing a Java program to transpose a matrix. In the last couple of tutorials, we have learned to how to add and subtract two matrices in Java (see here) and how to multiply two matrices in Java (see here). In this tutorial, I'll show you how to transpose a matrix in Java. The transpose of a matrix is a new matrix whose rows are the columns of the original. This means when you transpose a matrix the columns of the new matrix becomes the rows of the original matrix and vice-versa. In short, to transpose a matrix, just swap the rows and columns of the matrix. For example, if you have a matrix with 2 rows and 3 columns then transpose of that matrix will contain 3 rows and two columns.
This is the second program in the series of matrices related programming exercises in Java. In the last program, you have learned matrix multiplication and in this program, you will learn how to perform addition and subtraction of two matrices in Java. We'll create methods to calculate both sum and difference of two matrices in Java program. In Mathematics, a matrix is a rectangular array with two dimensions known as rows and columns. In Java, your can use a two-dimensional array to represent a matrix because it also has two dimensions rows and columns. Since a 2D array is nothing but an array of the array in Java, the length of the outer array is equal to the number of rows and length of sub-array is equal to the number of columns.