を使用してツリーを作成しましたCFTree
。そのツリーをレベルごとにトラバースする必要があります。幅優先アルゴリズムをどのように実装すればよいですか?(少なくとも手がかりを提供してください) 現在、以下のコードは深さ優先を使用しています。
- (CFTreeRef)createTreeFromJson:(NSDictionary *)jsonDict
{
treeBacking = CreateMyTree(kCFAllocatorDefault, @"A");
CFTreeRef bNode = CreateChildWithColor(treeBacking, @"B");
CFTreeRef cNode = CreateChildWithColor(treeBacking, @"C");
CreateChildWithColor(bNode, @"D");
CreateChildWithColor(bNode, @"E");
CreateChildWithColor(cNode, @"F");
TraverseTree(treeBacking);
return treeBacking;
}
static CFTreeRef CreateMyTree(CFAllocatorRef allocator, NSString *name) {
NSString *info;
CFTreeContext ctx;
NSString *treeString = [[NSString alloc] initWithString:name];
info = treeString;
ctx.version = 0;
ctx.info = (__bridge void *)(info);
ctx.retain = CFRetain;
ctx.release = CFRelease;
ctx.copyDescription = NULL;
return CFTreeCreate(allocator, &ctx);
}
static CFTreeRef CreateChildWithColor(CFTreeRef tree, NSString *name)
{
CFTreeRef child;
CFTreeContext ctx;
child = CreateMyTree(CFGetAllocator(tree), name);
CFTreeGetContext(child, &ctx);
ctx.info = (__bridge void *)(name);
CFTreeAppendChild(tree, child);
return child;
}
static void TraverseTree(CFTreeRef tree) {
// If the node has children, then next node is the first child
CFTreeRef curChild = CFTreeGetFirstChild(tree);
for (; curChild; curChild = CFTreeGetNextSibling(curChild)) {
CFTreeContext ctx;
CFTreeGetContext(curChild, &ctx);
NSLog(@"Node Name : %@",ctx.info);
TraverseTree(curChild);
}
}