Results 1 to 3 of 3
  1. #1
    invincible_me is offline Member
    Join Date
    Apr 2007
    Posts
    1
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    levent Guest

    Default

    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.

  3. #3
    g25451 is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Problem with String Building
    By Albert in forum New To Java
    Replies: 2
    Last Post: 04-30-2012, 01:49 AM
  2. building a house
    By dc2acgsr99 in forum Java Applets
    Replies: 4
    Last Post: 03-08-2008, 12:18 AM
  3. Building a document from a DOM
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:22 AM
  4. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 05:47 PM
  5. Building A Java Project In Eclipse
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-22-2007, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •