Results 1 to 4 of 4
  1. #1
    tuckker is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Question Preorder Traversal problem...

    I have:


    Java Code:
    public void preOrderTrav() {
            CustNode current = root;
            Tree t;
            printCustNode(current);
     
            System.out.println(current.key + "," + current.name);
            for (int i = 0; i < current.rentVid.size(); 
                 i++) { //Loop to add all videos in each node
     
                System.out.print("," + current.rentVid.get(i));
                if (current.left != null)
                    current.left.preOrderTrav();
            }
        }
    HOwever obviously current.left.preOrderTrav(); won't work, since the above code is in a Tree.java.

    How do I do an iterative of preOrderTrav() on the left Node?

    FYI:

    Java Code:
    class CustNode {
       int key; //Which is also the tel No since tel no is unique
        String name;
        Vector rentVid = new Vector();
         CustNode left;
        CustNode right;
    }
    This method is in the Tree.java. Other than this method, this Tree.java contains other methods which includes the insert, delete, findNode methods etc...

    Let me know if more info is needed ;)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public String getPreOrderSort() {
        String s = "";
        s += name + " ";
        if(left != null)
            s += left.getPreOrderSort() + " ";
        if(right != null)
            s += right.getPreOrderSort() + " ";
        return s;
    }

  3. #3
    tuckker is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default

    Where should that be? inthe node class file or in the tree class file?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Try it in your CustNode class. Call it on the root (or any) node in your Tree class.

Similar Threads

  1. Array traversal issues
    By sondratheloser in forum New To Java
    Replies: 3
    Last Post: 08-13-2012, 12:49 AM
  2. Bidirectional Traversal with ListIterator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  3. JTable Focus Traversal
    By helios_lie in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2007, 10:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •