Firstly, this code is basically to check whether the given tree is a BST or not. It does not print the inorder traversal of the BST.
The idea behind this is that we store the value of the previous node and compare it with the value of the current node to check if it is smaller or greater. Now how does that help us?
If noticed carefully, in the inorder traversal , a value that appears before has to be smaller than a value that appears later, because the left child is smaller than the root and the root is smaller than the right child and the order we follow to traverse the BST is :
In inorder traversal, we traverse as follows :
Now the code :
The above portion is a recursive call to the function with the left child as argument. If a false value is returned from the left subtree then return false.
if (prev != NULL && root->data <= prev->data)
This part compares the previous value with the current ( the current is root ) and returns false if current <= previous.
Notice the <= . This is because BST does not allow two nodes with the same value.
prev = root;
The above statement assigns the current value to the previous variable and we go on to the right subtree to check for the condition of BST with the following code :
This returns the value received from a recursive call to the function with the right child as argument.
One important thing to note here is that the prev variable is made static, meaning, new instances are not created for it with successive calls to the function. It is not redeclared with successive calls to the isBST() function. This is necessary as we require the prev value that we stored earlier before calling the function.