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

Recommended Answers

All 4 Replies

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

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

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.

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

So you can add Objects to it later

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.