wood burning stoves 2.0*
The moose likes Java in General and the fly likes i got a problem, with my test program on binary trees Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "i got a problem, with my test program on binary trees" Watch "i got a problem, with my test program on binary trees" New topic
Author

i got a problem, with my test program on binary trees

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
my gole is to make a sum from all the leaves in the right sub tree of the main root
and sum all the leave at the left subtree of the main root
and make
the differece between them.
in my code i got null point exeption
i dont know why
i just want the right code for making the difference of
the sums in the right subtree and the left one

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
The stack trace of the exception will tell you which line it occurred on. One of the references on that line is null. If you can't tell from looking at the code which one it is, try putting some ptint statements in.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
You do realise that binary tress accumulate null references; every new leaf node has two nulls attached until you put leaves on them and convert them to branches; then you get more leaf nodes with new nulls! The bit about returning -1 when the root is null doesn't look at all good to me.
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
so how can i say in java languege the following:
"if you see a leaf only a leaf not a local root"
than add it to the temporary variable
???
[ February 19, 2008: Message edited by: donaldth smithts ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
don't understand the question
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i am having trouble to solve this question
how doi write a method which adds all the leaves in the sub tree??
i tried to write that in a code
Ender Ak
Greenhorn

Joined: Aug 11, 2007
Posts: 4
Hi
you cant call a method of null object, in the difference()method of BTree class , you call root.left.sum() - root.right.sum(); but left and right instance variable of root object also BNode object therefore one of the left and right instance of these object comes null always. Try to dubug your application putting debug point on the line "return root.left.sum() - root.right.sum();" in the difference method. Comes null always because the sum() method of BNode is recursive. lets look at this your difference method. it checks root is null or not, but it does not check left and right instance variables of root object is null. And always there is one of them is null.therefore you always reach a null object. everytime when you create BNode object you also creeating null left and right object, therfore you cant call method of null object.

sorry for my english, i hope i helped
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok my algorithm for this is like
first add the leaves of the lest tree
then add the leaves of the right tree

and then make a difference out of both of them

first of all i am building the BTree for the root

and i put my "sum" function to work on the left side
and the right side
and i return their difference

i dont know how to build it
i dont know why i get here null point exeption


[ February 19, 2008: Message edited by: donaldth smithts ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Consider what would happen if either right or left is null ...


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok i have solved my null point exeption problem
but still i get a blank terminal

i am not sure my alogithm is ok

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
anyone??
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
is that ok??

[ February 20, 2008: Message edited by: donaldth smithts ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Originally posted by donaldth smithts:
is that ok??
. . .
Have you tried it?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
You don't return anything if left and right are not null
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
are yo suggesting me to remove the return line if they both
equal to null??

because in some point i have to return some solid number
in order to have a resolt

the other lines are for searching deeper into the tree
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
No. I'm saying your code won't compile because there is a path through the code which will mean the end of the sum() method is reached without hitting a return statement. This will happen if both left and right are not null.
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok you are saying that my method will end even if
there are some cases that dont have both right and left equals to null

can you tell me specificly were is my mistake in my code

i want to find the right answer
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Manually step through your code assuming right and left are not null. Which return statement is called ?
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
when i get to a node in which there is no way to go deeper
both right and left sides equals to null
i know that thats a leaf and i return it value

thats my return statement
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Originally posted by donaldth smithts:
when i get to a node in which there is no way to go deeper
both right and left sides equals to null
i know that thats a leaf and i return it value

thats my return statement


That's what your first if statement handles.
Your second if statement handles when left is null and right is not null.
Your third if statement handles when right is null and left is not null.
You don't have any code to handle when both left and right are not null.
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok
in the case of non of the sides equals to null
is that ok??

[ February 20, 2008: Message edited by: donaldth smithts ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Run it and see.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i got a problem, with my test program on binary trees
 
Similar Threads
i cant understand this solution..
how do i solve this recorsive function question??
how to split a number into two multipliyers
idont understand why i get this resolt
a binary tree question..