Skip to main content

tree code error in java

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
sugir1987
Offline
Joined: 2012-01-17

can you help me to clear the error?

import java.util.*;

class Node{

protected Object data;

protected static ArrayList child;

protected Node next;



public Node(){

next = null;

data = null;

child=new ArrayList();

}

public Node(Object d,Node n){

data = d;

next = n;

n.child=new ArrayList();

}



}



public class Tree{

protected Node head;



public Tree(){

head = null;

}

public boolean isEmpty(){

return head == null;

}

public void insert(Object obj){

if(isEmpty()){

Node.child.add(obj);

head = new Node(obj,head);

}

}

public static void main(String args[])

{

Tree t=new Tree();

Integer j = null;

int i;

System.out.println("starting...");

for(i=0;i<5;i++){

j = new Integer((int)(Math.random() * 100));

t.insert(j);

System.out.println("insert: " + j);

}

System.out.println("Done ;-)");



}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bwillcott
Offline
Joined: 2007-06-01

You should use an IDE, such as Netbeans 7.1.
Results below:
=============================
import java.util.ArrayList;

/**
* @author Brad Willcott
*/
public class Node
{
protected ArrayList child;
protected Object data;
protected Node next;

public Node()
{
next = null;
data = null;
child = new ArrayList();
}

public Node(Object d, Node n)
{
data = d;
next = n;
next.child = new ArrayList();
}
}

public class Tree
{
protected Node head = new Node();

public Tree(){}

public void insert(Object obj)
{
head.child.add(obj);
}
}

public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
Tree t = new Tree();
Integer j = null;
int i;

System.out.println("starting...");

for (i = 0; i < 5; i++) {
j = new Integer((int) (Math.random() * 100));
t.insert(j);
System.out.println("insert: " + j);
}

System.out.println("Done ;-)");
}
}