Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Trouble with a toy tree [SOLVED] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with a toy tree [SOLVED]" Watch "Trouble with a toy tree [SOLVED]" New topic
Author

Trouble with a toy tree [SOLVED]

Lido Scaggsinsky
Greenhorn

Joined: Sep 16, 2007
Posts: 9
I'm trying to build a basic tree so I can write some test algorithms against it, but I'm getting null pointers and some weird behavior while trying to load the tree with data. Any advice would be appreciated. Is using recursion to find the spot to insert a node a bad idea? What I don't understand is why it's trying to add Lass under Tass, and then after that seems to not go down the right side of the tree and starts placing nodes right under the root ("Mass"). Thanks. Three classes will follow:

Node:


Tree:


TreeTester:


Lido
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Hi, Lido. You should probably make your printTree method recursive, so it will print all the nodes in the tree, and it will prevent your code from throwing NullPointerExceptions.

As for your other problem, your addRight method doesn't seem to be working. Take a good look
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
A tree will always have nulls in. So when you are iterating the tree, recursively, using an inorder depth-first traversal, you would do it like this
  • 1: See whether the left value is null
  • 2: If it is null, do nothing with it.
  • 3: If it wasn't null, pass this method recursively to the left.
  • 4: Print the "value" field.
  • 5: See whether the "right" field is null.
  • 6: If it was null, leave it alone.
  • 7: If it wasn't null, pass this method to it recursively
  • That would look something like thisBy the way, your hasLeft() method is poor style. It would be far better to writeThe generic form is <T extends Comparable<? super T>>
    Lido Scaggsinsky
    Greenhorn

    Joined: Sep 16, 2007
    Posts: 9
    Stephan van Hulst wrote:Hi, Lido. You should probably make your printTree method recursive, so it will print all the nodes in the tree, and it will prevent your code from throwing NullPointerExceptions.

    As for your other problem, your addRight method doesn't seem to be working. Take a good look


    Ha! Thanks! No wonder....

    Yeah, that print method was just some sloppy debugging.
    Lido Scaggsinsky
    Greenhorn

    Joined: Sep 16, 2007
    Posts: 9
    Campbell Ritchie wrote:By the way, your hasLeft() method is poor style. It would be far better to writeThe generic form is <T extends Comparable<? super T>>


    Thanks, that hasLeft() does look cleaner.

    What is the difference between the generic form and what I used in my Tree class?

    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3615
        
      14

    Campbell's form is more flexible. It won't change the way your method works, but it will allow the client to use more argument types.

    For instance, if you have a class Fruit extends Comparable<Fruit>, and you have a class Pear extends Fruit, then your current method will only be able to accept Fruits, and not Pears. Using the wildcard, your method can take Pears as well, without any extra effort on your part.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    It's not my form; it's what it says in the API when you look up Comparable.
    Lido Scaggsinsky
    Greenhorn

    Joined: Sep 16, 2007
    Posts: 9
    Stephan van Hulst wrote:Campbell's form is more flexible. It won't change the way your method works, but it will allow the client to use more argument types.

    For instance, if you have a class Fruit extends Comparable<Fruit>, and you have a class Pear extends Fruit, then your current method will only be able to accept Fruits, and not Pears. Using the wildcard, your method can take Pears as well, without any extra effort on your part.


    Perfect, thanks!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Trouble with a toy tree [SOLVED]