gpt4 book ai didi

iphone - 如何使用相同类型的子对象计算自定义对象的深度?

转载 作者:行者123 更新时间:2023-12-01 19:14:58 25 4
gpt4 key购买 nike

我有一个名为CEKnot的对象,它具有称为子结的属性,它是一组类似的CEKnot对象的数组。现在我要计算这棵树的深度。这不是二叉树,因为子结可以超过两个。这是我尝试过的代码。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CEProjectDTO* project = [self.tblViewDatasource objectAtIndex:indexPath.row];

CEServiceSession *serviceSession = [CEServiceSession new];

[CEProgressHUD showWithTitle:@"Loading..."];

[serviceSession.projectPhaseService getProjectPhasesWithProjectID:project.i3D onFinish:^(NSArray * phaseList){
self.phaseDTOList = phaseList;
NSLog(@"Count is %d",[self.phaseDTOList count]);
[CEProgressHUD hide];
}onError:^(NSString *title, NSString *message) {
[CEProgressHUD hide];
[CEAlertViewHelper showOKAlertViewWithTitle:title message:message];
}];
self.mindMapLevelObjectCount = nil;
self.mindMapLevelObjectCount = [[NSMutableDictionary alloc] init];
self.level = 1;

self.numberOfKnotsOnThisLevel = 0;

CEProjectToKnotConverter* converter = [CEProjectToKnotConverter new];
CEProjectKnot* baseKnot = [converter convertProject:project withPhases:self.phaseDTOList];

self.numberOfKnotsOnThisLevel = self.numberOfKnotsOnThisLevel + [baseKnot.subKnots count];
[self.mindMapLevelObjectCount setObject:[NSNumber numberWithInt:self.numberOfKnotsOnThisLevel] forKey:[NSString stringWithFormat:@"%d",self.level]];
self.numberOfKnotsOnThisLevel = 0;

self.level = 2;


[self calculateMindMapLevels:baseKnot];

NSLog(@"MIND MAP LEVEL OBJE %@", self.mindMapLevelObjectCount);


CEMindMapViewController *mindMapViewController = [[CEViewControllerManager sharedInstance] createViewController:[CEMindMapViewController class]];
mindMapViewController.baseKnot = baseKnot;
[self presentModalViewController:mindMapViewController animated:YES];


}
-(void)calculateMindMapLevels:(CEKnot*)projectKnot{


for (CEKnot* subKnot in projectKnot.subKnots) {
self.numberOfKnotsOnThisLevel = self.numberOfKnotsOnThisLevel + [subKnot.subKnots count];
NSLog(@"Level %d total of nots %d project not subknot %d", self.level,self.numberOfKnotsOnThisLevel, [subKnot.subKnots count]);
}
[self.mindMapLevelObjectCount setObject:[NSNumber numberWithInt:self.numberOfKnotsOnThisLevel] forKey:[NSString stringWithFormat:@"%d",self.level]];


for (CEKnot* subKnot in projectKnot.subKnots) {
[self calculateMindMapLevels:subKnot];
self.numberOfKnotsOnThisLevel = 0;
}
self.level = _level +1;

}

我没有正确获得深度,并尝试了多种算法,但无法获得正确的深度。任何帮助表示赞赏。

最佳答案

只要您的树不存储其深度,您就必须获取每个结的Level并返回最高值。

递归-未试用的方法

int numberOfLevels(CEKnot *knot, int parentLevel) {
int ownLevel = parentLevel+1;
int highestLevel = ownLevel;
for (CEKnot* subKnot in knot) {
int subLevel = numberOfLevels(subKnot, ownLevel);
highestLevel = MAX(highestLevel, subLevel);
}
return highestLevel;
}

关于iphone - 如何使用相同类型的子对象计算自定义对象的深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com