Solved

Perform EulerTour for a given directory

Posted on 2007-03-26
4
272 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
table example 4 32
tomcat administrtor 12 61
hibernate jars 4 45
difference between sorce folder and folder in eclipise 3 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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