Solved

Perform EulerTour for a given directory

Posted on 2007-03-26
4
261 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now