# Trying to understand this method

Page 1 of 1

## 6 Replies - 244 Views - Last Post: 28 November 2017 - 01:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407900&amp;s=d0a47508613c6e6e956cfea8e3c819b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chloeCodes

Reputation: 4
• Posts: 178
• Joined: 05-January 17

# Trying to understand this method

Posted 28 November 2017 - 11:33 AM

Hi guys!

I am looking into Binary Tree data structure implementation. I understand that if the root node is null, meaning it is not referring to any other node, then the tree is empty, and the node we want to add can be given to the rootNode. I'm thinking should the traverseandaddnode method be in an else case, rather than just after the if? Or am I just confused?

I would appreciate some guidance!

``` public void add(String word)
{
//If the rootNode in the tree is not referring to any node, then the tree
//is empty, and the rootNode gets the node to be added.
if(rootNode == null)
{
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Trying to understand this method

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Trying to understand this method

Posted 28 November 2017 - 12:06 PM

Your analysis is correct. One thing to note: if the Tree is empty, then you need not invoke a helper TraverseAndAdd() method after the if statement. A return; statement is your friend here:
```if(root == null){
return; //this terminates the method add() and returns control to the invoking method
}

```

This is more efficient than your current implementation.

This post has been edited by macosxnerd101: 28 November 2017 - 12:07 PM

### #3 chloeCodes

Reputation: 4
• Posts: 178
• Joined: 05-January 17

## Re: Trying to understand this method

Posted 28 November 2017 - 12:10 PM

Hi Macos,

Thanks for getting back to me! Am I correct in thinking, if I put a return statement, then no need to put the call to the helper method in the else case. If I don't put the return, then the helper call method would need to call in the else case. But either way is fine right?

Many many thanks!

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Trying to understand this method

Posted 28 November 2017 - 12:12 PM

Yes- that is correct. Of course, you could just try it out and see for yourself too.

### #5 chloeCodes

Reputation: 4
• Posts: 178
• Joined: 05-January 17

## Re: Trying to understand this method

Posted 28 November 2017 - 12:17 PM

### #6 chloeCodes

Reputation: 4
• Posts: 178
• Joined: 05-January 17

## Re: Trying to understand this method

Posted 28 November 2017 - 01:29 PM

Hi!

I have another question. I want to describe the method add as an algorithm. However, I need to give a high-level description about how it works (so not a line by line description). I'm really confused on how to describe the algorithm. I would appreciate some pointers, I know how to explain it line by line though haha! If I was to describe the algorithm for the add method as a whole I would say:

The method add checks whether or not root refers to any objects. If it doesn't, then the tree is empty and root gets the node that we want to add to the tree. Otherwise we recursively call add's helper method, until we find the node that is referring to null, and it goes there. I would say something along these lines. However, I feel like I'm still describing the algorithm/code line by line. How would I improve and describe the algorithm as a whole. I am really struggling with this, and would truly appreciate some feedback.

Sorry after submitted, I realized that someone is going to want to take this out of the JAVA forum and into a Computer Science forum, that's fine! I'm just not sure how to do it myself

Many Thanks!!

This post has been edited by chloeCodes: 28 November 2017 - 01:32 PM

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12278
• Posts: 45,364
• Joined: 27-December 08

## Re: Trying to understand this method

Posted 28 November 2017 - 01:32 PM

You have two cases, really. Either the tree is empty or it is not empty. In each case, how would you insert a new element into the tree? Describe this procedure as if you were explaining the idea to a classmate. It might be a good idea to practice explaining this idea verbally to one of your classmates, without computers or code floating around.