Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching File in a directory recursively using Pre-Order Post Order and InOrder

Tags:

java

tree

When I write a code to search file in a directory recursively, for example I apply the method below:

public void list(File file) {
    System.out.println(file.getName());
    File[] children = file.listFiles();
    for (File child : children) {
        list(child);
    }
}

If I need to show it in pre-order, in-order and post-order traversal, How can I do it?

I am not able to related tree traversal with this file search.

like image 955
Sunny Gupta Avatar asked Jan 17 '26 17:01

Sunny Gupta


1 Answers

Your code is in pre-order, because the parent is processed (printed) before the children. If you moved the print to after the loop, it would be post-order. In-order would not make too much sense in this case. If you had a binary tree, it would be if you processed the parent in-between processing each child.

like image 179
Eduardo Avatar answered Jan 20 '26 08:01

Eduardo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!