1.11M Members

NaryTrees

 
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);
   }
   // . . .
}
 
0
 

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

 
0
 

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

 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article