本文整理汇总了Python中Node.Node.addChild方法的典型用法代码示例。如果您正苦于以下问题:Python Node.addChild方法的具体用法?Python Node.addChild怎么用?Python Node.addChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Node.Node
的用法示例。
在下文中一共展示了Node.addChild方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ID3
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def ID3(X, Y, attrs, currentDepth, maxDepth=7):
currentDepth += 1
# If all examples are positive, Return the single-node tree Root, with label = +.
# If all examples are negative, Return the single-node tree Root, with label = -.
if len(set(Y)) == 1:
return Leaf(Y[0])
# If the number of predicting attributes is empty, then return a single node tree with
# Label = most common value of the target attribute in the examples
if len(attrs) == 0 or currentDepth == maxDepth:
return Leaf(majorityElement(Y))
# A = The Attribute that best classifies examples.
A = findBestFeatureIndex(X, Y, attrs)
newAttrs = [attr for attr in attrs if attr != A]
# Decision Tree attribute for Root = A.
root = Node(A)
# For each possible value, vi, of A
for possible_value in [0, 1]:
# Let Examples(vi) be the subset of examples that have the value vi for A
X_s, Y_s = splitData(X, Y, A, possible_value)
if len(X_s) == 0 or len(Y_s) == 0:
# Then below this new branch add a leaf node with label = most common target value in the examples
root.addChild(possible_value, Leaf(majorityElement(Y)))
else:
# Else below this node add a subtree ID3(Examples_vi, TargetAttr, Attrs - {A})
root.addChild(possible_value, ID3(X_s, Y_s, newAttrs, currentDepth, maxDepth))
return root
示例2: generateTree
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def generateTree(self, indsize=3):
default = "def namedfunc(x):\n\t"
rootNode = Node(default)
returnNode = Node("\n\treturn ")
self.pickChild(rootNode, rootNode)
rootNode.addChild(returnNode)
self.pickChild(returnNode, rootNode)
return rootNode
示例3: getNode
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def getNode(self, k, gDict = None):
if k not in self._nodeMap:
newNode = Node(k)
self._nodeMap[k] = newNode
for v in gDict[k]:
newNode.addChild(self.getNode(v, gDict))
return self._nodeMap[k]
示例4: testContainsGrandChildNode
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testContainsGrandChildNode(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
grandChild1 = Node("grandChild1")
grandChild2 = Node("grandChild2")
grandChild3 = Node("grandChild3")
root.addChild(child1)
root.addChild(child2)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
self.assertTrue(self.tree.contains(grandChild1))
self.assertTrue(self.tree.contains(grandChild2))
self.assertFalse(self.tree.contains(grandChild3))
示例5: testFindGrandChildNode
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testFindGrandChildNode(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
grandChild1 = Node("grandChild1")
grandChild2 = Node("grandChild2")
grandChild3 = Node("grandChild3")
root.addChild(child1)
root.addChild(child2)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
foundGC1 = self.tree.find(grandChild1)
foundGC2 = self.tree.find(grandChild2)
self.assertEquals("grandChild1", foundGC1.getData())
self.assertEquals("grandChild2", foundGC2.getData())
self.assertEquals(None, self.tree.find(grandChild3))
示例6: setUp
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def setUp(self):
self.bfs=BFS()
tree=Tree()
node1=Node()
node1.setValue(1)
node2=Node()
node2.setValue(2)
node3=Node()
node3.setValue(3)
node4=Node()
node4.setValue(4)
node5=Node()
node5.setValue(5)
node1.addChild(node2)
node1.addChild(node3)
node2.addChild(node4)
node3.addChild(node5)
node4.addChild(node2)
tree.setRoot(node1)
self.bfs.setTree(tree)
示例7: testContainsDescendantNodes
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testContainsDescendantNodes(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
child3 = Node("child3")
grandChild1 = Node("grandChild1")
grandChild11 = Node("grandChild11")
grandChild2 = Node("grandChild2")
grandChild22 = Node("grandChild22")
descendant1 = Node("descendant1")
descendant11 = Node("descendant11")
descendant2 = Node("descendant2")
descendant22 = Node("descendant22")
descendant3 = Node("descendant3")
root.addChild(child1)
root.addChild(child2)
root.addChild(child3)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
grandChild1.addChild(descendant1)
grandChild1.addChild(descendant11)
grandChild2.addChild(descendant2)
grandChild2.addChild(descendant22)
self.assertTrue(self.tree.contains(Node("descendant1")))
self.assertTrue(self.tree.contains(Node("descendant11")))
self.assertTrue(self.tree.contains(Node("descendant2")))
self.assertTrue(self.tree.contains(Node("descendant22")))
self.assertFalse(self.tree.contains(descendant3))
示例8: testFoundNodeBuildWithChildren
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testFoundNodeBuildWithChildren(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
child3 = Node("child3")
grandChild1 = Node("grandChild1")
grandChild11 = Node("grandChild11")
grandChild2 = Node("grandChild2")
grandChild22 = Node("grandChild22")
descendant1 = Node("descendant1")
descendant11 = Node("descendant11")
descendant2 = Node("descendant2")
descendant22 = Node("descendant22")
descendant3 = Node("descendant3")
root.addChild(child1)
root.addChild(child2)
root.addChild(child3)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
grandChild1.addChild(descendant1)
grandChild1.addChild(descendant11)
grandChild2.addChild(descendant2)
grandChild2.addChild(descendant22)
foundNode = self.tree.find(Node("child2"))
expectedString = ( "1 child2\n"
"2 grandChild2\n"
"3 descendant2\n"
"3 descendant22\n")
self.assertEquals(expectedString, foundNode.buildWithChildren(True))
示例9: testTreeToString
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testTreeToString(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
child3 = Node("child3")
grandChild1 = Node("grandChild1")
grandChild11 = Node("grandChild11")
grandChild2 = Node("grandChild2")
grandChild22 = Node("grandChild22")
descendant1 = Node("descendant1")
descendant11 = Node("descendant11")
descendant2 = Node("descendant2")
descendant22 = Node("descendant22")
descendant3 = Node("descendant3")
root.addChild(child1)
root.addChild(child2)
root.addChild(child3)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
grandChild1.addChild(descendant1)
grandChild1.addChild(descendant11)
grandChild2.addChild(descendant2)
grandChild2.addChild(descendant22)
expectedString = ( "0 hello world\n"
"1 child1\n"
"2 grandChild1\n"
"3 descendant1\n"
"3 descendant11\n"
"1 child2\n"
"2 grandChild2\n"
"3 descendant2\n"
"3 descendant22\n"
"1 child3\n")
self.assertEquals(expectedString, self.tree.toString(True))
示例10: testFindDescendantNodes
# 需要导入模块: from Node import Node [as 别名]
# 或者: from Node.Node import addChild [as 别名]
def testFindDescendantNodes(self):
root = self.tree.getRoot()
child1 = Node("child1")
child2 = Node("child2")
child3 = Node("child3")
grandChild1 = Node("grandChild1")
grandChild11 = Node("grandChild11")
grandChild2 = Node("grandChild2")
grandChild22 = Node("grandChild22")
descendant1 = Node("descendant1")
descendant11 = Node("descendant11")
descendant2 = Node("descendant2")
descendant22 = Node("descendant22")
descendant3 = Node("descendant3")
root.addChild(child1)
root.addChild(child2)
root.addChild(child3)
child1.addChild(grandChild1)
child2.addChild(grandChild2)
grandChild1.addChild(descendant1)
grandChild1.addChild(descendant11)
grandChild2.addChild(descendant2)
grandChild2.addChild(descendant22)
foundD1 = self.tree.find(Node("descendant1"))
foundD11 = self.tree.find(Node("descendant11"))
foundD2 = self.tree.find(Node("descendant2"))
foundD22 = self.tree.find(Node("descendant22"))
self.assertEquals(descendant1.getData(), foundD1.getData())
self.assertEquals(descendant11.getData(), foundD11.getData())
self.assertEquals(descendant2.getData(), foundD2.getData())
self.assertEquals(descendant22.getData(), foundD22.getData())
self.assertEquals(None, self.tree.find(descendant3))