wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Build AVL Tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Build AVL Tree" Watch "Build AVL Tree" New topic
Author

Build AVL Tree

mike fusc
Greenhorn

Joined: Mar 07, 2010
Posts: 15
I have everything set, just when I use the java app and print everything is blank. The issue has to be within makeAVLTree, but i can't figure it out. even System.out.print doesn't print anything.

Even if i use the sortedarray.txt(has five values sorted in ascending order) and try to print it is still blank

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Nicely formatted though it may be, that's a lot of code to ask people to look over. If you have no idea what's going wrong, then the best plan would be to step through it with a debugger. You can generally find problems like this in minutes that way.


[Jess in Action][AskingGoodQuestions]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The problem is in the put() method.

What happens when you put the first value in? What is left as the root? What about the second value? What is the root set to? What about the third? etc...


Steve
mike fusc
Greenhorn

Joined: Mar 07, 2010
Posts: 15
that's the issue nothing is happening when i insert the first values at all
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Correct, because you have this:

Which will always be true. If root is null to begin with (which it is) then you immediately return null, and do no work. You have to change that behavior to something that makes sense (like do some work first, then return null...)
mike fusc
Greenhorn

Joined: Mar 07, 2010
Posts: 15
gotcha if i remove that line I get NullPointer at AVLApp lines 94 and 87, but that's not even my code that was already given
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Right, you don't want to remove that line of code. When root is null, it is a special case which must be taken care of. You can't just ignore it.

The question is, what do you think needs to happen when root is null?
mike fusc
Greenhorn

Joined: Mar 07, 2010
Posts: 15
i changed it so that when root is null it initalizes to insert the first value

AVLTreeNode insert = new AVLTreeNode(key, value);
if(root==null){
root= insert;
return null;
}

but that doesnt help
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Well, I think that will help. I think you then need to start tracing what happens with the first, second, third etc... entries. Take a piece of paper and work it out on paper. See where it goes wrong and fix it. I still think your problem is in the put() method. I think you are mixing up what is being stored in the parent, ptr, ptr.left, and ptr.right variables. But working it out on paper should help you out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Have you deleted part of the first post, mike fusc? Don't. It makes all the replies to it look like nonsense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Build AVL Tree