Results 1 to 3 of 3
- 04-28-2007, 11:58 PM #1
Member
- Join Date
- Apr 2007
- Posts
- 1
- Rep Power
- 0
help needed regarding tree building
hey friends
I am facing problem converting a code that reads a file with a number in different line eg
6
4
7
8
2
then building a tree(binary search tree) from this input file. the code i wrote is:
The problem is i want to convert this code to a code which takes as input a file likeJava Code:import java.util.*; import java.io.*; public class des { public static void main(String[] args)throws IOException { String s=null; DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter the name of the file"); try { s = dis.readLine(); } catch(IOException e) { System.out.println("Nothing has been entered, please enter something"); } //function(s); new des().run(s); } static class Node { Node left; Node right; int value; public Node(int value) { this.value = value; } } public void run(String fn)throws IOException { int q=-1,i=0; int a[]=new int[100]; Node root=null; FileReader fr; BufferedReader bf=null; String line,str=null; System.out.println("Filename is "+fn); // try // { fr=new FileReader(fn); bf=new BufferedReader(fr); while((line=bf.readLine()) !=null) { StringTokenizer st = new StringTokenizer(line); //while(st.hasMoreTokens()) //{ q++; try { str=st.nextToken(); a[i]=Integer.parseInt(str); } catch(NumberFormatException e) { System.out.println(str+ " is not in the right format"); System.out.println(); q--; continue; } if(q==0) { root = new Node(a[i]); System.out.println("Building tree with root value " + root.value); } if(i>0) insert(root,a[i]); i++; } } public void insert(Node node, int value) { if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of "+ node.value); node.left = new Node(value); } } else if (value > node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted " + value + " to right of "+ node.value); node.right = new Node(value); } } } }
6 6
4 6
3 6
2 4
1 4
2 3
where the first number in a line is a child and the second number is the parent of that child. the first line indicates that 6 is a root node.
kindly help me in changing the code above for this input.
thanks in anticipation
- 04-29-2007, 10:39 AM #2levent Guest
As far as i understand, the second column in your input file is the parent of that node.
If so, it is simple.
First of all, you will get two integers instead of one for each line (use str=st.nextToken(); a[i]=Integer.parseInt(str); two times to get two integers).
You will make the first line as root node and for other nodes, you will find parent of that node. So for "4 6" input, you will look for the parent 6 starting from root node. Normally that will require you to traverse the tree but as far as i see you keep previous nodes inside an array. So you will only need to search that array. Once you find it, you will add new node under that parent using "insert".
Try to implement this algorithm. And if you need more help, let us know.
- 08-12-2008, 01:44 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Problem with String Building
By Albert in forum New To JavaReplies: 2Last Post: 04-30-2012, 12:49 AM -
building a house
By dc2acgsr99 in forum Java AppletsReplies: 4Last Post: 03-07-2008, 11:18 PM -
Building a document from a DOM
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:22 AM -
building file and variable names from variables
By madad2005 in forum New To JavaReplies: 2Last Post: 07-18-2007, 04:47 PM -
Building A Java Project In Eclipse
By JavaForums in forum EclipseReplies: 0Last Post: 05-22-2007, 09:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks