Binary search tree not working, when it should. Any idea what am i doing wrong. any help would be much appreciated.
this is the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method iprint(Node1) in the type BST is not applicable for the arguments ()
    The method preprint(Node1) in the type BST is not applicable for the arguments ()



at MainBST.main(MainBST.java:38)

code for my main:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MainBST {

    public static void main(String[] args) throws FileNotFoundException 
    {

        BST mytree = new BST();
        String file2 = "infile2.txt";
        String file3 = "inFile3.txt";


        addData(file2, "[,\n\r]+", mytree);
        addData(file3, "[\t\n\r]+", mytree);




        System.out.println("-------------Pre-order------------------");
        mytree.iprint();
        System.out.println("---------------In-Order-----------------");
        mytree.preprint();
    }

    private static void addData(String filepath, String delimiter, BST tree) throws FileNotFoundException
    {
        File file = new File(filepath);
        Scanner sc = new Scanner(file).useDelimiter(delimiter);
        while(sc.hasNext())
        {
            tree.add(sc.next(), sc.next(), sc.nextInt());
        } 
    }
}

code for my BST

public class BST {

    Node1 root;

    public BST() {
        root = null;
    }

    public void add(String fname, String lname, int age) {
        Node1 NewNode = new Node1(lname, fname, age);
        Node1 compare = root;
        if (root == null)
            root = NewNode;
        else {
            while (true) {
                if (NewNode.age < compare.age) {
                    if (compare.lChild == null) {
                        compare.lChild = NewNode;
                        break;
                    }
                    compare = compare.lChild;
                } else {
                    if (compare.rChild == null) {
                        compare.rChild = NewNode;
                        break;
                    }
                    compare = compare.rChild;
                }
            }

        }
    }


    public void iprint(Node1 t) {
        if (t != null) {

            iprint(t.lChild);   // left
            System.out.println(t);   // data 
            iprint(t.rChild);   // right

        }
    }

    public void preprint(Node1 t) {
        if (t != null) {

            System.out.println(t);   // data 
            preprint(t.lChild);   // left
            preprint(t.rChild);   // right

        }
    }
}

code for my Node1

public class Node1 {



        String lname;
        String fname;
        int age;

        Node1 lChild;
        Node1 rChild;

        public Node1( String l, String f, int a)
        {
            this.lname = l;
            this.fname = f;
            this.age = a;

            lChild = null;
            rChild = null;
        }

        public String toString()
        {
            return(" the age for "+fname+" "+ lname +" is "+ age);
        }
    }

Recommended Answers

All 12 Replies

Are you sure the mytree print lines are not reversed in order?

System.out.println("-------------Pre-order------------------");
mytree.iprint();
System.out.println("---------------In-Order-----------------");
mytree.preprint();

Anyway, please show your input, and your output.

it reads from a file and has the following data in it

jackie chain 50
micheal jordan 60
Baby cray 27
Whocares Yourmama 48

there's 2 files with similar data type just different delimiters

So, what is the output from preprint() and iprint()?

there is no output. I'm getting an error stated in the very first line. The method does exist in BST so why the error???

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method iprint(Node1) in the type BST is not applicable for the arguments ()
    The method preprint(Node1) in the type BST is not applicable for the arguments ()
at MainBST.main(MainBST.java:38)

I'm not enterily sure about how java handles constructors.
But could the problem be that the only constructor in Node1 takes arguments, and that you're instantiating it using no arguments as the very first act in the BST constructor?

What would happen if you change the constructor in Node1 to take no arguments, and instead add properties to insert the values those arguments are for?

The message is saying:
Your iprint and preprint methods have been defined to require a parameter of type Node1, but when you call those methods you call them with no parameters.

It's aboslutely pointless trying to execute a program that has compile errors like that.

ps: Oxiegen: Where is the no-args instantiation of Node1 you referred to?

Yep, i had to do some modifications.

System.out.println("-------------Pre-order------------------");
        greenTree.iprint(greenTree.root);

         addData(file3, "[,\n\r]+", greenTree);
         addData(file2, "[\t\n\r]+", greenTree);
        System.out.println("---------------In-Order-----------------");
        greenTree.preprint(greenTree.root);

Although my delimiters are not working properly i think. It prints everything under preorder and not the line in-order just prints the word inorder, why?

Answer to JamesCherril:
Right here...

Node1 root;

public BST() {
    root = null;
}

Like I said. I'm not really a java coder. I was just following the statement that the error occured on the first line.
But I just realized that I misread that post. :)
Though, I figured it was worth checking out considering that the error message pointed out there were problems with the arguments.

OK, I get where you are coming from,that Java is not your main language, and thanks for trying to help.

But now, for the benfit of any Java learners reading this:
That code shows a constructor for the BST class, not the Node1 class.
It declares a variable of type Node1, which creates a new reference (like a pointer) but does not create a Node1 object. Its initial value is null, so the assignment on line 4 is not strictly neccessary.
None of that code actually instantiates a Node1 object, so no constructor of Node1 is ever called by it.

Huh. I thought it sort of shared the concept of the creation of the object at the declaration with any other object based language.
You know: Node1 root; creates an object. That was my reasoning.
Anyway. Now I know.
Do go on providing real help. :D

It's a very common cause of problems for Java learners. The obvious reading of
Thing t;
is that you just created a new Thing.
But the language spec defines variables like that as "reference" variables, and the value of a reference variable is a "reference", not an object. For anyone with a C background that declaration is much closer in meaning to
Thing *t;
The default value for a reference variable is null, ie it doesn't refer to any actual Thing. The only way to create an actual Thing object is via the new keyword.

If you think that's a bit obscure, just wait until you try to discuss whether Java method parameters are passed by value or reference! :)

Ooh. I understand. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.