Start New Discussion within our Software Development Community

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 ; = degree ;
      subtree = null ;

   public  NaryTree(int degree, Object key){
      this.key = key ; = degree ;
      subtree = new NaryTree[degree] ;
      for(int i = 0; i < degree; i++)  
         subtree[i] = new NaryTree(degree);
   // . . .

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.

//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.

This article has been dead for over six months. Start a new discussion instead.