Tree

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zealman
    New Member
    • May 2010
    • 1

    #1

    Tree

    Code:
    import java.util.*;
    import java.io.*;
    import java.math.*;
    import java.awt.event.*; 
    
    class Node
    {
    	String ortu,isi;
    	int jumanak,jumchild;
    	Node child[] = new Node[5];
    public Node(String parent, String a)
    	{
    		ortu=parent;
    		isi=a;
                    jumanak=jumchild;
    	}
    }
    
    public class tree 
    {
    private Node root;
     
       	private boolean isEmpty()
       	{
    		return (root == null);
       	}
      	
       	public void insert(String x,String y, int pos)
       	{
    		int coba;
    		coba = 0;
                    Node temp = new Node(x,y);
    		if (isEmpty())
    		{
    			root = temp;
                            System.out.println("root awal:"+root.isi); 
                            root.jumchild=0;
    		}
    
    		else 
    		{
                            root.jumchild++;
                            System.out.println("jum child:"+root.jumchild+"y:"+y); 
    			Node cursor = root,
    			parent = null;
                            coba = 1;
    			for ( int i=1;i<root.jumchild;i++)
                            {
    				if ( root.child[i-1].isi == y )
                                    {
                                      coba = 0;   
    				  System.out.println("child ke "+root.child[i-1].isi+" sama dengan "+y);                              
                                    }
    				else
                                    {
                                     coba = 1;
                                    }
    
    			}
    		     if ( coba == 1 )
    		     {
    			while (cursor != null)
    			{
    				parent = cursor;
    				cursor = cursor.child[pos];
    				parent.child[pos] = temp;
    				System.out.println("parent : "+root.child[pos].ortu+" isi : "+root.child[pos].isi);
    			}
                         }
    		}
    	}
    		
    	public void level2()
    	{
    		Node tempo=new Node("C1","C1.1");
    		Node curs=root.child[0], ortu=null;
    		ortu=curs;
    		curs=curs.child[0];
    		ortu.child[0] = tempo;
    	System.out.println("parent : "+root.child[0].child[0].ortu+" isi : "+root.child[0].child[0].isi);
    	}
    
    	public void baca()
    	{
    		String file="bacatulis.txt";
    		String [] kolomtxtcek;
    		String baristxtcek,a,b;
    		int c;
    		int jum=0;
    		try
    		{
    			BufferedReader baca = new BufferedReader(new FileReader(file));
    			while ((baristxtcek = baca.readLine()) != null)
    			{
    				kolomtxtcek = baristxtcek.split(",");
    				a=kolomtxtcek[0];
    				b=kolomtxtcek[1];
    				c=Integer.parseInt(kolomtxtcek[2]);
    				insert(a,b,c);
    			jum++;
    			}	
    		}
    		catch (IOException e) 
    		{
    			 System.err.println("Error: " + e);
    		}
    		System.out.println(jum);
    	}	
    	
    	public static void main (String[] args) 
    	{
    		tree pohon=new tree();
    	pohon.baca();
    		pohon.level2();	
    	}
     }
    this is the error output......
    Exception in thread "main" java.lang.NullP ointerException
    at tree.insert(tre e.java:55)
    at tree.baca(tree. java:106)
    at tree.main(tree. java:137)
    Last edited by Markus; May 7 '10, 11:28 AM. Reason: Added [code] tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You're incrementing only the root's # of children
    root.jumchild++ ;
    even when you add the node to one of the root's children. I don't entirely get your algorithm for adding nodes, but you want to instead increment the child count of the node you actually add the new node to.

    Comment

    Working...