Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2007, 12:58 AM
Member
 
Join Date: Apr 2007
Posts: 1
invincible_me is on a distinguished road
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:

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); } } } }
The problem is i want to convert this code to a code which takes as input a file like

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-29-2007, 11:39 AM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
building a house dc2acgsr99 Java Applets 4 03-08-2008 12:18 AM
Building a document from a DOM Java Tip Java Tips 0 01-03-2008 10:22 AM
building file and variable names from variables madad2005 New To Java 2 07-18-2007 05:47 PM
Problem with String Building Albert New To Java 0 07-09-2007 08:20 PM
Building A Java Project In Eclipse JavaForums Eclipse 0 05-22-2007 10:34 PM


All times are GMT +3. The time now is 12:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org