title: Generic tree structure on iOS
body: <p>I am having a problem with generics tree structure in Objective C. I am mainly Java programmer, so I guess I need help from you guys here. On Android I am using this code without a problem:</p>
  
  <pre><code>public class Tree&lt;T&gt; {
  
  
  
  private T head;
  
    private ArrayList&lt;Tree&lt;T&gt;&gt; leafs = new ArrayList&lt;Tree&lt;T&gt;&gt;();
  
    private Tree&lt;T&gt; parent = null;
  
    private HashMap&lt;T, Tree&lt;T&gt;&gt; locate = new HashMap&lt;T, Tree&lt;T&gt;&gt;();
  
    public Tree(T head) {
      this.head = head;
      locate.put(head, this);
    }
  
    public void addLeaf(T root, T leaf) {
      if (locate.containsKey(root)) {
        locate.get(root).addLeaf(leaf);
      } else {
        addLeaf(root).addLeaf(leaf);
      }
    }
  
    public Tree&lt;T&gt; addLeaf(T leaf) {
      Tree&lt;T&gt; t = new Tree&lt;T&gt;(leaf);
      leafs.add(t);
      t.parent = this;
      t.locate = this.locate;
      locate.put(leaf, t);
      return t;
    }
  
    public Tree&lt;T&gt; setAsParent(T parentRoot) {
      Tree&lt;T&gt; t = new Tree&lt;T&gt;(parentRoot);
      t.leafs.add(this);
      this.parent = t;
      t.locate = this.locate;
      t.locate.put(head, this);
      t.locate.put(parentRoot, t);
      return t;
    }
  
    public T getHead() {
      return head;
    }
  
    public Tree&lt;T&gt; getTree(T element) {
      return locate.get(element);
    }
  
    public Tree&lt;T&gt; getParent() {
      return parent;
    }
  
    public Collection&lt;T&gt; getSuccessors(T root) {
      Collection&lt;T&gt; successors = new ArrayList&lt;T&gt;();
      Tree&lt;T&gt; tree = getTree(root);
      if (null != tree) {
        for (Tree&lt;T&gt; leaf : tree.leafs) {
          successors.add(leaf.head);
        }
      }
      return successors;
    }
  
    public Collection&lt;Tree&lt;T&gt;&gt; getSubTrees() {
      return leafs;
    }
  
    public static &lt;T&gt; Collection&lt;T&gt; getSuccessors(T of, Collection&lt;Tree&lt;T&gt;&gt; in) {
      for (Tree&lt;T&gt; tree : in) {
        if (tree.locate.containsKey(of)) {
          return tree.getSuccessors(of);
        }
      }
      return new ArrayList&lt;T&gt;();
    }
  
  }
  </code></pre>
  
  <p>But when I tried to port it as it is to Objective-C it simply doesn't work. Here is the code:</p>
  
  <pre><code>@implementation Tree {
      Node* head;
      NSMutableArray* leafs;
      Tree* parent;
      NSMutableDictionary* locate;
  
  }
  @synthesize head;
  
  -(id) initWithHead:(Node *) Head {
      self = [super init];
      if (self)  {
          head = Head;
          leafs = [[NSMutableArray alloc]init];
          locate = [[NSMutableDictionary alloc] init];
          [locate setObject:head forKey:(id)self];
      }
      return self;
  }
  
  
  
  -(void) addLeafWithRoot:(Node *)Root leaf:(Node *)Leaf {
      if([locate objectForKey:Root] != nil) {
          [[locate objectForKey:Root] addLeaf:Leaf];
      }else{
          [[self addLeaf:Root] addLeaf:Leaf];
      }
  }
  
  -(Tree *) addLeaf:(Node*) Leaf {
      Tree * t = [[Tree alloc] initWithHead:Leaf];
      [leafs addObject:t];
      [t setParent:t];
      [self setLocate:[t getLocate]];
      [locate setObject:Leaf forKey:(id)t];
      return t;
  }
  
  -(Tree*) getTree:(Node *) Node {
      return [locate objectForKey:Node];
  }
  
  -(NSMutableArray *) getSuccesor:(Node *) root {
      NSMutableArray *successors = [[NSMutableArray alloc] init];
      Tree* tree = [self getTree:root];
      if(tree != nil) {
          for (Tree* leaf in [tree getLeafs]) {
              [successors addObject:[leaf getHead]];
          }
      }
      return successors;
  }
  
  -(void) setParent:(Tree *) Tree {
      parent = Tree;
  }
  
  -(void) setLocate:(NSMutableDictionary *) Locate {
      locate = Locate;
  }
  
  -(NSMutableDictionary *) getLocate {
      return locate;
  }
  
  -(NSMutableArray *) getLeafs {
      return leafs;
  }
  
  -(Node *) getHead {
      return head;
  }
  
  -(id)copyWithZone:(NSZone *)zone
  {
      Tree *another = [[[self class] allocWithZone:zone] init];
      another.head = self.head;
      return another;
  }
  
  @end
  </code></pre>
  
  <p>I guess that is due to, that on Android I am using Tree class Node type and that's simply I cannot use in Objective-C. Can I modify this solution, to works on iOS, or do I have to come with something totally different? Thanks in advance.</p>
  
  <p>By the way, Node class is basic class with getters and setters to hold some text and int information.</p>