Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Build AVL Tree

 
mike fusc
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
mike fusc
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's the issue nothing is happening when i insert the first values at all
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you deleted part of the first post, mike fusc? Don't. It makes all the replies to it look like nonsense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic