Results 1 to 17 of 17
  1. #1
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default What's wrong with my codes?

    public class TreeNode {
    int item;
    TreeNode left;
    TreeNode right;
    }
    static int countNodes( TreeNode root ) {

    if ( root == null )
    return 0;
    else {
    int count = 1;
    count += countNodes(root.left);

    count += countNodes(root.right);

    return count;
    }
    }
    static void preorderPrint( TreeNode root ) {

    if ( root != null ) {
    System.out.print( root.item + " " );
    preorderPrint( root.left );
    preorderPrint( root.right );
    }
    }
    static void postorderPrint( TreeNode root ) {

    if ( root != null ) {
    postorderPrint( root.left );
    postorderPrint( root.right );
    System.out.print( root.item + " " );
    }
    }

    static void inorderPrint( TreeNode root ) {

    if ( root != null ) {
    inorderPrint( root.left );
    System.out.print( root.item + " " );
    inorderPrint( root.right );
    }
    }

    in this programme i try to do Inorder and Preorder Traversal tree but i don't know what's the wrong on it the message that i get it from the programme is:

    class, interface, or enum expected so what the wrong and how can i solve it ???

  2. #2
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    You have your brackets in the wrong place and you are trying to declare methods outside of your class. I changed the name to node sorry.

    public class node {
    int item;
    node left;
    node right;

    static int countNodes( node root ) {

    if ( root == null )
    return 0;
    else {
    int count = 1;
    count += countNodes(root.left);

    count += countNodes(root.right);

    return count;
    }
    }
    static void preorderPrint( node root ) {

    if ( root != null ) {
    System.out.print( root.item + " " );
    preorderPrint( root.left );
    preorderPrint( root.right );
    }
    }
    static void postorderPrint( node root ) {

    if ( root != null ) {
    postorderPrint( root.left );
    postorderPrint( root.right );
    System.out.print( root.item + " " );
    }
    }

    static void inorderPrint( node root ) {

    if ( root != null ) {
    inorderPrint( root.left );
    System.out.print( root.item + " " );
    inorderPrint( root.right );
    }
    }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when post any code in a thread. It's helpful to all other members to read and replay. :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Much better,

    Java Code:
    public class TreeNode {
    
        int item;
        TreeNode left;
        TreeNode right;
    
        static int countNodes( TreeNode root ) {
            if ( root == null )
                return 0;
            else {
                int count = 1;
                count += countNodes(root.left);
                count += countNodes(root.right);
                return count;
            }
        }
    
        static void preorderPrint( TreeNode root ) {
            if ( root != null ) {
                System.out.print( root.item + " " );
                preorderPrint( root.left );
                preorderPrint( root.right );
            }
        }
    
        static void postorderPrint( TreeNode root ) {
            if ( root != null ) {
                postorderPrint( root.left );
                postorderPrint( root.right );
                System.out.print( root.item + " " );
            }
        }
    
        static void inorderPrint( TreeNode root ) {
            if ( root != null ) {
                inorderPrint( root.left );
                System.out.print( root.item + " " );
                inorderPrint( root.right );
            }
        }
    }

  5. #5
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default

    but now when i compile it i get that answer :
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.

  6. #6
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default

    ok anther time i will post it like you say

  7. #7
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    It compiles fine for me... You don't have a main method in that class anyway.

    And sorry for not using code tags, will do in future!

  8. #8
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default

    aha so i need to add main method in that clsss

    public static void main(String args[]);

    that what you mean???

  9. #9
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    Java Code:
    public class node {
        int item;
        node left;
        node right;
        
        public static void main(String[] args) {
            System.out.println("Hello");
        }
        
        static int countNodes( node root ) {
            
            if ( root == null )
                return 0;
            else {
                int count = 1;
                count += countNodes(root.left);
                
                count += countNodes(root.right);
                
                return count;
            }
        }
        static void preorderPrint( node root ) {
            
            if ( root != null ) {
                System.out.print( root.item + " " );
                preorderPrint( root.left );
                preorderPrint( root.right );
            }
        }
        static void postorderPrint( node root ) {
            
            if ( root != null ) {
                postorderPrint( root.left );
                postorderPrint( root.right );
                System.out.print( root.item + " " );
            }
        }
        
        static void inorderPrint( node root ) {
            
            if ( root != null ) {
                inorderPrint( root.left );
                System.out.print( root.item + " " );
                inorderPrint( root.right );
            }
        }    
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to implement a main method in your class. Main method is the entry point to start your process of the class.

  11. #11
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default

    sorry about that so when i need to get my output like that what should i do ??

    preorderPrint outputs: 1 2 4 5 3 6

    postorderPrint outputs: 4 5 2 6 3 1

    inorderPrint outputs: 4 2 5 1 3 6

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What you want to do? I mean how did you get numbers(result). Just looking some numbers we can't say what you have to do. Clearly tell me what you want to do?

  13. #13
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    To be honest I am not exactly sure what it is that you are trying to achieve, if you could explain maybe I could help a little more.

    If this is school homework then you would be better off trying to implement some working code and then post up any problems you have.

  14. #14
    ayoood is offline Member
    Join Date
    May 2008
    Posts
    39
    Rep Power
    0

    Default

    finish i done what i want to do

    thank you for your help bro

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    ayoood, don't worry to post any question you have. But remember provide more details about requirements. Because if not, how we know about your requirement.

    And also keep in mind. No here to write code for you. Try something and if you stuck, ask here. Have a great time. :)

    Now you can mark this thread as solved. It's much better.

  16. #16
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Oh, and besides, you really should follow the code conventions. Class names start with a capital letter, so Node in this case. ;)
    I die a little on the inside...
    Every time I get shot.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. PLZ...PLZ!!!HELP ME TO WRITE THE TASK 2's CODES
    By hhoa87 in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2008, 11:50 PM
  2. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:15 AM
  3. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 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
  •