JavaCoder
JavaCoder

Reputation: 55

Binary search tree in java, main is not reading from BST

Binary search tree not working properly, i'm stucked and have no idea how to fix this. Any idea what am i doing wrong. The object does exist in BST so why the error.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(); //This is where i have the problem
        System.out.println("---------------In-Order-----------------");
        mytree.preprint(); //Object is there so why the error
    }
    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);
        }
    }

Upvotes: 0

Views: 184

Answers (2)

a_123
a_123

Reputation: 323

mytree.iprint() and mytree.preprint() expect an argument. You need to pass mytree.root to both of these functions in your main function.

Upvotes: 1

Jake Psimos
Jake Psimos

Reputation: 640

Perhaps you should step through the iprint function and find the exact node that raises the exception to determine the condition that is causing the problem.

It is difficult to give an appropriate answer because (I) do not know the contents.

Upvotes: 0

Related Questions