1

I would like to implement a generic TreeNode and a Generic Tree. I've seen few examples on stackoverflow and other places, but couldn't understand completely.

public interface Node<K, V>
{
    K getId();         
    V getData();
    K getParentId();  
    void addChild(Node<K, V> child);
    List<Node<K, V>> getChildren();
}

public class Tree<K, V> 
{
   private Node<K, V> root;
   public void loadData(Map<K, V> data)
   {
         // by figuring out the root node from map, populate tree
         // root node is the one with parentId as null
         // and the logic follows. Simple level order insertion 
   } 
}

Although the above code servers my purpose, I want to understand if there is a better way to achieve this.

Like declare Tree as

public class Tree<T extends Node<?, ?> 
{
   private T root;
   public void loadData(Map<K, V> data) 
   {
         // by figuring out the root node from map, populate tree
         // root node is the one with parentId as null
         // and the logic follows. Simple level order insertion 
   } 
}

In the above case, where should my data load method reside? Some Util class?

Basically, I want to understand the principles to be followed when creating Classes that are generic over types which in turn are generic.

Thanks.

Gopal
  • 1,292
  • 3
  • 19
  • 41
  • How would your second code work? Your type is only generic in `T` - so where do `K` and `V` come from? – Jon Skeet Jun 25 '12 at 05:43
  • 1
    I think what you have is good. Unless you want to allow "Trees of anything" and have something like `interface Tree` and define a class `NodeTree implements Tree>` – Bohemian Jun 25 '12 at 05:49
  • http://stackoverflow.com/questions/10265836/declare-a-binary-tree-which-takes-a-generic-type-of-node-which-contains-a-generi?rq=1 This is perfectly what I was looking for. However, since it is just a day since I posted this, I want to wait for few more days to see if there is a better answer. – Gopal Jun 25 '12 at 15:33
  • Very confusing question. Classes are types. As mentioned earlier, your last Tree code example bounds the Node type to wildcards, but then has a loadData method that describes 'K' and 'V' without defining what they mean. In terms of 'generic principles', just think through what this class does. If your Tree should only contain nodes of the same K and V, then your tree should be defined as Tree> with loadData method (Map data). If they can be of any types, then allowing for Tree> and a loadData method of (Map, ?> data) ought to suffice. – drobert Sep 12 '12 at 20:22

1 Answers1

0

If you need the hole tree types generics, this was the only option I could figure out:

    public class Tree<K extends Object, V extends Object, T extends Node<K, V>>
    {
       private T root;
       public void loadData(Map<K, V> data) 
       {
             // ...
       } 
    }

I'm not sure if I got your point, it's not really clear from the question what are your needs, so if this is pointless or just not exactly what you needed please let me know prior to downvote me (:

Regards.

HericDenis
  • 1,364
  • 12
  • 28