iOS上的通用树结构

时间:2014-12-19 13:43:10

标签: ios objective-c generics tree

我在Objective C中遇到了泛型树结构的问题。我主要是Java程序员,所以我想我们需要你们的帮助。在Android上我使用此代码没有问题:

public class Tree<T> {



private T head;

  private ArrayList<Tree<T>> leafs = new ArrayList<Tree<T>>();

  private Tree<T> parent = null;

  private HashMap<T, Tree<T>> locate = new HashMap<T, Tree<T>>();

  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<T> addLeaf(T leaf) {
    Tree<T> t = new Tree<T>(leaf);
    leafs.add(t);
    t.parent = this;
    t.locate = this.locate;
    locate.put(leaf, t);
    return t;
  }

  public Tree<T> setAsParent(T parentRoot) {
    Tree<T> t = new Tree<T>(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<T> getTree(T element) {
    return locate.get(element);
  }

  public Tree<T> getParent() {
    return parent;
  }

  public Collection<T> getSuccessors(T root) {
    Collection<T> successors = new ArrayList<T>();
    Tree<T> tree = getTree(root);
    if (null != tree) {
      for (Tree<T> leaf : tree.leafs) {
        successors.add(leaf.head);
      }
    }
    return successors;
  }

  public Collection<Tree<T>> getSubTrees() {
    return leafs;
  }

  public static <T> Collection<T> getSuccessors(T of, Collection<Tree<T>> in) {
    for (Tree<T> tree : in) {
      if (tree.locate.containsKey(of)) {
        return tree.getSuccessors(of);
      }
    }
    return new ArrayList<T>();
  }

}

但是当我尝试将它移植到Objective-C时,它根本不起作用。这是代码:

@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

我想这是因为,在Android上我使用的是Tree类Node类型,而这只是我不能在Objective-C中使用。我可以修改这个解决方案,适用于iOS,还是我必须提供完全不同的东西?提前谢谢。

顺便说一下,Node类是基本类,带有getter和setter来保存一些text和int信息。

1 个答案:

答案 0 :(得分:2)

您的实施存在内存管理问题。 (头部不会保留在init中,并且您没有实现dealloc。)它也没有树的好处,因为它不知道放置新节点的位置,并且没有搜索功能。

您应该使用CFTree或CFTree的Cocoa包装器,而不是编写自己的基本数据类型。有关详情,请参阅this answer