Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perform EulerTour for a given directory

Posted on 2007-03-26
4
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

670 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