(myTree)
| 224 | 2017-07-24 |
| 225 | """ |
| 226 | def getTreeDepth(myTree): |
| 227 | maxDepth = 0 #初始化决策树深度 |
| 228 | firstStr = next(iter(myTree)) #python3中myTree.keys()返回的是dict_keys,不在是list,所以不能使用myTree.keys()[0]的方法获取结点属性,可以使用list(myTree.keys())[0] |
| 229 | secondDict = myTree[firstStr] #获取下一个字典 |
| 230 | for key in secondDict.keys(): |
| 231 | if type(secondDict[key]).__name__=='dict': #测试该结点是否为字典,如果不是字典,代表此结点为叶子结点 |
| 232 | thisDepth = 1 + getTreeDepth(secondDict[key]) |
| 233 | else: thisDepth = 1 |
| 234 | if thisDepth > maxDepth: maxDepth = thisDepth #更新层数 |
| 235 | return maxDepth |
| 236 | |
| 237 | """ |
| 238 | 函数说明:绘制结点 |
no outgoing calls
no test coverage detected