1,105,229 Community Members

NaryTrees

Member Avatar
alikhandro
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi all,hope anyone can describe for me what is happining in this code,its about NaryTrees and am new with trees.

public class  NaryTree extends AbstractTree 
{
   protected  Object  key ;
   protected  int  degree ;
   protected  NaryTree[ ]  subtree ;

   public  NaryTree(int degree){
      key = null ; this.degree = degree ;
      subtree = null ;
   }

   public  NaryTree(int degree, Object key){
      this.key = key ;
      this.degree = degree ;
      subtree = new NaryTree[degree] ;
      for(int i = 0; i < degree; i++)  
         subtree[i] = new NaryTree(degree);
   }
   // . . .
}
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Ask specific questions about the parts you don't understand. All you posted was a constructor - not much going on there.

Member Avatar
alikhandro
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i know its a constructor :) why should i implement a new subtree that all are empty?

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

The class tree works similar to a multi-way tree. The different is that you can initialise how many branches (degree) you want in the tree at the constructor level.

//i.e.
NaryTree(4);
//This will create a node with degree of 4 and leaves are not initialised

NaryTree(3, "NewKey");
//This will create a node with degree of 3, and each leave will be set
//  to have 3 degree as well.

Nothing else from the class. If you understand a binary tree (always have 2 branches), it works similar to the binary tree but a lot more flexible to use.

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

why should i implement a new subtree that all are empty?

So you can add Objects to it later

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article