6 Replies - 244 Views - Last Post: 28 November 2017 - 01:32 PM Rate Topic: -----

#1 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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)
 {    
    Node nodeToBeAdd = new Node(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)
    {
     rootNode = nodeToAdd;
    }
    TraverseAndAddNode(rootNode,nodeToBeAdd);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand this method

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • 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){
   root = nodeToBeAdded;
   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

Was This Post Helpful? 1
  • +
  • -

#3 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • 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. ;)
Was This Post Helpful? 1
  • +
  • -

#5 chloeCodes  Icon User is offline

  • D.I.C Head

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

Re: Trying to understand this method

Posted 28 November 2017 - 12:17 PM

:D
Was This Post Helpful? 0
  • +
  • -

#6 chloeCodes  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1