This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need help with trees Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Need help with trees" Watch "Need help with trees" New topic
Author

Need help with trees

Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26496
    
  78

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Emili Calonge
Ranch Hand

Joined: May 17, 2003
Posts: 84
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

Joined: Feb 03, 2004
Posts: 456
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

Joined: May 17, 2003
Posts: 84
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Need help with trees
 
Similar Threads
Yet another Ranchin' Question
Binary search tree questions...
Need urgent help with tree
Reducing duplicate/repetative code
Binary Search Tree