473,418 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Tree

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.awt.event.*; 
  5.  
  6. class Node
  7. {
  8.     String ortu,isi;
  9.     int jumanak,jumchild;
  10.     Node child[] = new Node[5];
  11. public Node(String parent, String a)
  12.     {
  13.         ortu=parent;
  14.         isi=a;
  15.                 jumanak=jumchild;
  16.     }
  17. }
  18.  
  19. public class tree 
  20. {
  21. private Node root;
  22.  
  23.        private boolean isEmpty()
  24.        {
  25.         return (root == null);
  26.        }
  27.  
  28.        public void insert(String x,String y, int pos)
  29.        {
  30.         int coba;
  31.         coba = 0;
  32.                 Node temp = new Node(x,y);
  33.         if (isEmpty())
  34.         {
  35.             root = temp;
  36.                         System.out.println("root awal:"+root.isi); 
  37.                         root.jumchild=0;
  38.         }
  39.  
  40.         else 
  41.         {
  42.                         root.jumchild++;
  43.                         System.out.println("jum child:"+root.jumchild+"y:"+y); 
  44.             Node cursor = root,
  45.             parent = null;
  46.                         coba = 1;
  47.             for ( int i=1;i<root.jumchild;i++)
  48.                         {
  49.                 if ( root.child[i-1].isi == y )
  50.                                 {
  51.                                   coba = 0;   
  52.                   System.out.println("child ke "+root.child[i-1].isi+" sama dengan "+y);                              
  53.                                 }
  54.                 else
  55.                                 {
  56.                                  coba = 1;
  57.                                 }
  58.  
  59.             }
  60.              if ( coba == 1 )
  61.              {
  62.             while (cursor != null)
  63.             {
  64.                 parent = cursor;
  65.                 cursor = cursor.child[pos];
  66.                 parent.child[pos] = temp;
  67.                 System.out.println("parent : "+root.child[pos].ortu+" isi : "+root.child[pos].isi);
  68.             }
  69.                      }
  70.         }
  71.     }
  72.  
  73.     public void level2()
  74.     {
  75.         Node tempo=new Node("C1","C1.1");
  76.         Node curs=root.child[0], ortu=null;
  77.         ortu=curs;
  78.         curs=curs.child[0];
  79.         ortu.child[0] = tempo;
  80.     System.out.println("parent : "+root.child[0].child[0].ortu+" isi : "+root.child[0].child[0].isi);
  81.     }
  82.  
  83.     public void baca()
  84.     {
  85.         String file="bacatulis.txt";
  86.         String [] kolomtxtcek;
  87.         String baristxtcek,a,b;
  88.         int c;
  89.         int jum=0;
  90.         try
  91.         {
  92.             BufferedReader baca = new BufferedReader(new FileReader(file));
  93.             while ((baristxtcek = baca.readLine()) != null)
  94.             {
  95.                 kolomtxtcek = baristxtcek.split(",");
  96.                 a=kolomtxtcek[0];
  97.                 b=kolomtxtcek[1];
  98.                 c=Integer.parseInt(kolomtxtcek[2]);
  99.                 insert(a,b,c);
  100.             jum++;
  101.             }    
  102.         }
  103.         catch (IOException e) 
  104.         {
  105.              System.err.println("Error: " + e);
  106.         }
  107.         System.out.println(jum);
  108.     }    
  109.  
  110.     public static void main (String[] args) 
  111.     {
  112.         tree pohon=new tree();
  113.     pohon.baca();
  114.         pohon.level2();    
  115.     }
  116.  }
  117.  
this is the error output......
Exception in thread "main" java.lang.NullPointerException
at tree.insert(tree.java:55)
at tree.baca(tree.java:106)
at tree.main(tree.java:137)
May 7 '10 #1
1 1492
jkmyoung
2,057 Expert 2GB
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.
May 7 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
2
by: New | last post by:
Why does this code insert a node into a binary search tree correctly? If I only inserting going by first digit it works properly but when I try inserting going by the whole ip and the port number...
5
by: Mike | last post by:
Why does this code insert a node into a binary search tree correctly? If I only inserting going by first digit it works properly but when I try inserting going by the whole ip and the port number...
2
by: Kiran | last post by:
Hello all, I am using a tree to display stuff, and it is constantly updated, but what I have noticed is in the lowest level, there is clearly noticable cutoff of the text I place there. The cutoff...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.