Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't understand the question
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider what would happen if either right or left is null ...
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone??
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that ok??

[ February 20, 2008: Message edited by: donaldth smithts ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:
is that ok??
. . .
Have you tried it?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't return anything if left and right are not null
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manually step through your code assuming right and left are not null. Which return statement is called ?
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run it and see.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic