Solved

Perform EulerTour for a given directory

Posted on 2007-03-26
4
273 Views
Last Modified: 2013-11-23
I need write a class DirectoryTour to persorm an Euler Tour of a given File (i.e. a root of a directory) and i also write a main program to test it, it will call the methods beforeFirst, afterLast and inBetween. the method beforeFirst should be called the first time the node is vistited, the method afterLast should be called the last time the node is vistited, the method inBetween should be called in between each pair of children.  Finally, the default definiation of these methods should be print out the method name,

i do not know how to write a traversal methods, pls give me some tips

the a root of a directory like this:
c:\test0                        ------folder
c:\test0\one                -------folder
c:\test0\one\one-a      -------file
c:\test0\one\one-b      -------file
c:\test0\three               -------folder
c:\test0\two             -------folder
c:\test0\two\two-a      -------file
c:\test0\two\two-b      -------file


for example, out output should be like:
       beforeFirst: test0
       beforefirst: one
       beforefirst: one-a
       afterLast: one-a
       inBetween: one
       beforefirst: one-b
       afterLast: one-b
       afterLast: one
       inBetween: test0
0
Comment
Question by:ericylr
  • 2
  • 2
4 Comments
 

Author Comment

by:ericylr
ID: 18798762
this is a template from website

import jds.util.BinaryNode;

public class EulerTour {
      public final Object traverse (BinaryNode n, Object info) {
            info = beforeLeft(n, info);
            if (n.leftChild != null)
                  info = traverse(n.leftChild, info);
            info = inBetween(n, info);
            if (n.rightChild != null)
                  info = traverse(n.rightChild, info);
            info = afterRight(n, info);
            return info;
      }

      public Object beforeLeft (BinaryNode nd, Object info)
            { return info; }

      public Object inBetween (BinaryNode nd, Object info)
            { return info; }

      public Object afterRight (BinaryNode nd, Object info)
            { return info; }
}
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 18799284
Tree traversal:
http://en.wikipedia.org/wiki/Traversal

Eulerian path
http://en.wikipedia.org/wiki/Euler_tour

You should take the time to read and understand the concepts.

When you have a tree node (root) with several children, what you can do is recursively traverse and "visit"  each child. The simplest example on a "visit" is to print out the node's content.

Look at the traversal link for a clear illustration of a binary tree traversal (traversal methods).
0
 

Author Comment

by:ericylr
ID: 18801345
I write the DirectoryTour class, is it correct?

import java.lang.*;

public class DirectoryTour{

  public static class File{
        File leftChild, rightChild;
  }

  public Object traverse(File f, Object info) {
      info=beforeFirst(f, info);
      if (f.leftChild!=null)
            info=traverse(f.leftChild, info); // recursive traversal      
      info=inBetween(f, info);
      if(f.rightChild!=null)
            info=traverse(f.rightChild, info); // recursive traversal
      info=afterLast(f, info);
        return info;
  }

  // methods that can be redefined by the subclasses

  public Object beforeFirst(File f, Object info) {
        return info;
  }
 
  public Object inBetween(File f, Object info) {
        return info;
  }
 
  public Object afterLast(File f, Object info) {
        return info;
  }
0
 
LVL 20

Accepted Solution

by:
gatorvip earned 500 total points
ID: 18801699
1. Don't use "File" for the node type, you will confuse that with java.io.File which you will most likely need to use at some point in the project.
2. Think conceptually, a directory can have more than just two children (subfolder) so you can't really use a binary node.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
jsp login check 12 52
ejb entity bean example issue 2 25
learn programming 8 66
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question