We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,267 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

NaryTrees

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);
   }
   // . . .
}
4
Contributors
4
Replies
18 Hours
Discussion Span
6 Months Ago
Last Updated
5
Views
alikhandro
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

Ezzaral
null
Moderator
16,111 posts since May 2007
Reputation Points: 3,292
Solved Threads: 873
Skill Endorsements: 28

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

alikhandro
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

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

So you can add Objects to it later

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0448 seconds using 2.44MB