Link to home
Start Free TrialLog in
Avatar of ericylr
ericylr

asked on

Perform EulerTour for a given directory

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
Avatar of ericylr
ericylr

ASKER

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; }
}
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).
Avatar of ericylr

ASKER

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;
  }
ASKER CERTIFIED SOLUTION
Avatar of gatorvip
gatorvip
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial