i need some help with binary trees. i belive i have finish with insert and displaying the tree methods. but i can not be sure but i am getting a error in main.

error in main:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Cannot make a static reference to the non-static field current_node

line which has the error in main:

    m.inOrderTraversal(current_node);

i also tried changing current_node to static variable. but no luck.

public class D06_Binary_Tree
{
    /*** CREATE NODE ***/
    private class node
    {
        private String name;
        private int age;
        private node left_child;
        private node right_child;

        /*** Constructor ***/
        public node()
        {
            name = "";
            age = 07;
            left_child = null;
            right_child = null;
        }
        public node(String n, int a)
        {
            name = n;
            age = a;
            left_child = null;
            right_child = null;
        }/*** End of constructor ***/
    }/*** End of Node class ***/





    /*** Linked List Constructor ***/
    private node root_node;
    private node current_node;
    private node parent_node;
    private node new_node;

    public D06_Binary_Tree()
    {
        // TODO Auto-generated constructor stub
    }



    /*** Insert Method ***/
    public void insert(String n, int a)
    {
        current_node = root_node;
        parent_node = root_node;

        if(root_node == null)  //if root is empty
        {
            root_node = new node(n, a);
            root_node.left_child = null;
            root_node.right_child = null;
        }
        else
        {
            while(true)
            {
                parent_node = current_node; //follow current_node in loop
                if(current_node.age > a)    //LEFT HAND SIDE
                {
                    current_node = current_node.left_child;  //move current node
                    if(current_node == null)                 //if empty
                    {
                        new_node = new node(n, a);            //create new node
                        parent_node.left_child = new_node;    //parent is same as current cude
                        break;
                    }
                }
                else /*** RIGHT SIDE OF TREE ***/
                {
                    current_node = current_node.right_child;
                    if(current_node == null)
                    {
                        new_node = new node(n, a);
                        parent_node.right_child = new_node;
                        break;
                    }
                }
            }
        }
    }/*** End of Insert Method ***/





    /*** IN ORDER TRAVERSAL METHOD ***/
    //in order traversal - print all value lowest to heightest
    public void inOrderTraversal(node current_node)
    {
        current_node = root_node;

        if(current_node == null)
        {
            System.out.println("Binary Tree is empty!");
        }
        else 
        {
            inOrderTraversal(current_node.left_child);
            System.out.println(current_node);
            inOrderTraversal(current_node.right_child);
        }
    }/*** END OF IN ORDER TRAVERSAL METHOD ***/






    /*** Main Method ***/
    public static void main(String[] args)
    {
        D06_Binary_Tree m = new D06_Binary_Tree();

        m.insert("a", 50);
        m.insert("b", 25);
        m.insert("c", 15);
        m.insert("d", 30);
        m.insert("5", 75);
        m.insert("4", 85);
        m.inOrderTraversal(current_node);

    }
}

Recommended Answers

All 5 Replies

you are trying to use the value of variable current_node in your main method, but it has no value. you'll have to provide one, or it won't work.

current_node is an instance variable. It has one value (maybe null) for each instance of the class. You cannot use it without an instance. Your main method is static - it executes without an instance of the class - so you can't use current_node unless you specify explicity an instance when you refer to it.
That's what "Cannot make a static reference to the non-static field current_node" means.

(ps stultiske - that's not the same as an uninitialised variable problem. "providing a value" won't work help because the attempt to assign a value will also generate a "Cannot make a static reference to the non-static field current_node" error unless the instance is specified explicitly)

if the variable is initialised for an instance within the main method, it is :)
nyeah, should 've been more complete.

i just remove "current__node = root_node on line 94. bc its a recursion function and its setting current note to root node all the times. so it never goes ahead.

2nd thing i changed was line 124.

m.inOrderTraversal(m.current_node);

now there are no errors. but the problem is that when i run this code. it only prints this line.

System.out.println("Binary Tree is empty!");

With line 124 like that, what is the value of m.current_node? The current_node variable is used widely within your methods, with no guarantees about how it will be left afterwards. Looks like the insert method (the last one called before the inOrderTraversal) always terminates with current_node == null, so that will be its value when you call inOrderTraversal, hence the "empty" output.
Maybe your inOrderTraversal should start with the root node???

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.