1 # 12-1 FP树的类定义 2 class treeNode: 3 def _init_(self,nameValue,numOccur,parentNode): 4 self.name=nameValue; # 节点的名字 5 self.count=numOccur; # 出现次数 6 self.nodeLink=None; # 链接相似的元素项 7 self.parent=parentNode; # 父节点 8 self.children={}; # 子节点 9 def inc(self,numCoour): # 方法inc()对count变量增加给定值 10 self.count+=numOccur; 11 def disp(self,ind=1): # 方法disp()用于将树以文本形式显示 12 print(' '*ind,self.name,' ',self.count); 13 for child in self.children.values(): 14 child.disp(ind+1);
上面的定义了一个类treeNode,方法有_init_(self,nameValue,numOccur,parentNode),inc(self,numCoour),disp(self,ind=1);
初始化错误方式:
1 >>> 2 >>> import fpGrowth; 3 >>> rootnode=treeNode('pyramid',9,None); 4 Traceback (most recent call last): 5 File "<pyshell#5>", line 1, in <module> 6 rootnode=fpGrowth.treeNode('pyramid',9,None); 7 TypeError: object() takes no parameters
正确方式:
1 >>> 2 >>> import fpGrowth; 3 >>> oS=treeNode(); 4 >>> rootnode=oS._init_('pyramid',9,None) 5 >>> rootnode 6 >>> type(rootnode) 7 <class 'NoneType'> 8 >>>
正确使用:
1 >>> 2 >>> import fpGrowth; 3 >>> rootNode=treeNode(); 4 >>> rootNode._init_('pyramid',9,None); 5 >>> rootNode.disp() 6 pyramid 9 7 >>>