• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with trees  RSS feed

 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I'm not English so it may be quite difficult for me to explain exactly what my problem is, so I'll try to make it clear.

Ok, I'm first going to explain what I'm trying to do:
I'm writing this program where a user can add thousands of files(pictures), the objective of the program is to give a powerful search engine that finds "fast" the files wanted by the user. The part I'm wornking on now is search based on file size, I'm doing it whith AVL trees because this way I can easily search with querys like "size < 200".

Now the second part, the problem(I don't know if the sames names are given in English, I work with Nodes wich have a value, a right son and a left son):
I'm doing the add function to the tree recursively, but I've got lots of problems always with the same thing. I want to add a file to the tree, so I want to do it this way:
if(empty){
create Node
}else{
add(tree.right_son(),file)
}
Obviously this is not my java code, in case someone is going to tell me this is not java syntax.
The problem comes when tree.right_son() returns empty, it gives a NullPointer Exception, I know I could check if it is empty before calling the right_son() function, but I've got lots of function calls like this. What I need is being able to call a function with a null and then checking inside the function if the parameter is null, is there any way to do this, or does anybody know how to do this??

My real problem is that I'm "translating" this function in one I've got writen in C++, and that I have no time to do this, so any help will be really appreciated. Thanks a lot.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emili,
You can do a null check:


Or if tree.right_son() is doing something involved, you can put it in a local variable so the method only gets called once.
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I can do this, but it's not usefull to this, I need to call a function with a null value, I need to know if this can be done, checking all the time is not an option for me.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi emili,

i'd say this is not a java problem, but an algorithmical one.

from my understanding of your needs and your problem, your algorithm should guarantee that always exactly one of the following conditions is true:

- (empty)
- (tree.getSon() != null)

if i got you right, then this is not yet the case - so it seems to me that your algorithm is not 100% clean.

:-)

hope it helps,
jan
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made a decission, my algorithm is such a mess, I'm deleting it, I'll use the java avl trees or make it another way, it's not possible to do what I'm intending to do and I don't have time to recode all my functions, so if java has one I'm using it, maybe when I have time I'll redo my avl class, but now I don't have time. Thanks for the help anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!