I got a Binary tree project, but to get things started, i decided to put all the elements into a LinkedList first.
Anyway, it seems i forgot how to go back to the root of my linkedList. I can traverse through the LinkedList, but it seems that whenever I try to call it again, it's already at the end of the List. My linkedList consist of a Integer, and a the next Node is called "left" atm.
Here's my code:
class MyTree { //class definition
class Node {
int data;
Node left;
Node right;
}
private Node root;
private int [] rdata;
Node m = new Node();
BufferedReader in;
public void generate(int n, int range){
Random r = new Random();
rdata = new int[n];
for (int i = 0; i<n; i++) {
rdata[i] = r.nextInt(range);
}
Node b = new Node();
for (int j = 0; j<n; j++){
b.data = rdata[j];
m.left = b;
m = m.left;
System.out.println("m.data " + m.data);
}
Now whenever I try to call it back again, It would only print the last integer in the LinkedList, so I had to use an array to set it up it again basically.
public void show() {
Node b = new Node();
for (int j = 0; j<rdata.length; j++){
b.data = rdata[j];
m.left = b;
m = m.left;
System.out.println("m.data " + m.data);
}
}
I'll like to traverse through the list again without setting up the linked list every time.
Thanks in Advance.
Message was edited by:
Vertix
Message was edited by:
Vertix
Message was edited by:
Vertix
Message was edited by:
Vertix
Message was edited by:
Vertix