這是網路上的一個版本,我自己做了一點修改,這是一個n叉樹,希望對你有所幫助
#!/usr/bin/python
#-*-coding:utf-8-*-
'''
Createdon2014-3-26
@author:qcq
'''
#==============================================================================
#tree.py:
#==============================================================================
#--
#$Revision:1.7$
#$Date:2000/03/2922:36:12$
#modifiedbyqcqin2014/3/27
#modifiedbyqcqin2014/4/23增加了遍歷樹的非遞歸版本的廣度優先和深度優先
#--
#================================================================
#Contents
#----------------------------------------------------------------
#classTree:Genericn-arytreenodeobject
#classNodeId:Representsthepathtoanodeinann-arytree
#----------------------------------------------------------------
#importstring
classTree:
"""Genericn-arytreenodeobject
Childrenareadditive;noprovisionfordeletingthem.
:0forthefirst
childadded,1forthesecond,andsoon.
modifiedbyqcqin2014/3/27.
Exports:
Tree(parent,value=None)Constructor
.parentIfthisistherootnode,None,otherwise
theparent'sTreeobject.
.childListListofchildren,zeroormoreTreeobjects.
.valueValuepassedtoconstructor;canbeanytype.
.birthOrderIfthisistherootnode,0,otherwisethe
indexofthischildintheparent's.childList
.nChildren()Returnsthenumberofself'schildren.
.nthChild(n)Returnsthenthchild;raisesIndexErrorif
nisnotavalidchildnumber.
.fullPath():.
.nodeId():ReturnspathtoselfasaNodeId.
"""
#---Tree.__init__---
def__init__(self,parent,value=None):
"""ConstructorforaTreeobject.
[if(parentisNoneoraTreeobject)->
if(parentisNone)->
,nochildren,
andvalue(value)
else->
ofparent(parent)andnochildren,andvalue(value)
]
"""
#--1--
self.parent=parent
self.value=value
self.childList=[]
#--2--
#-[if(parentisNone)->
#self.birthOrder:=0
#else->
#parent:=
#self.birthOrder:=sizeofparent's.childList
#-]
ifparentisNone:
self.birthOrder=0
else:
self.birthOrder=len(parent.childList)
parent.childList.append(self)
#---Tree.nChildren---
defnChildren(self):
"""[
]
"""
returnlen(self.childList)
#---Tree.nthChild---
defnthChild(self,n):
"""[if(nisaninteger)->
if(0<=n<(numberofself'schildren)->
returnself's(n)thchild,countingfrom0
else->
raiseIndexError
]
"""
returnself.childList[n]
#---Tree.fullPath---
deffullPath(self):
""".
[returnasequence[c0,c1,...,ci]suchthatselfis
root.nthChild(c0).nthChild(c1).....nthChild(ci),or
anemptylistifselfistheroot]
"""
#--1--
result=[]
parent=self.parent
kid=self
#--2--
#[result+:=childnumbersfromparenttoroot,in
#reverseorder]
whileparent:
result.insert(0,kid.birthOrder)
parent,kid=parent.parent,parent
#--3--
returnresult
#---Tree.nodeId---
defnodeId(self):
""".
"""
#--1--
#[fullPath:=sequence[c0,c1,...,ci]suchthatselfis
#root.nthChild(c0).nthChild(c1).....nthChild(ci),or
#anemptylistifselfistheroot]
fullPath=self.fullPath()
#--2--
returnNodeId(fullPath)
defequals(self,node):
'''
'''
returnself.value==node.value
#===========================================================================
#deletethenodefromthetree
#===========================================================================
defdelete(self):
ifself.parentisNone:
return
else:
#temp=self.birthOrder
'''
ifdeletethemiddletreeobject,
.
'''
self.parent.childList.remove(self.parent.childList[self.birthOrder])
fori,jinzip(range(self.birthOrder+1,self.parent.nChildren()),self.parent.childList[self.birthOrder+1:]):
j.birthOrder=j.birthOrder-1
defupdate(self,value):
'''
'''
self.value=value
def__str__(self):
return"The%dchildwithvalue%d"%(self.birthOrder,self.value)#-----classNodeId-----
classNodeId:
""".
Exports:
NodeId(path):
[->
-id]
.path:[aspassedtoconstructor]
.__str__():[returnselfasastring]
.find(root):
[ifrootisaTreeobject->
treerootedat(root)->
returnthe.valueofthatnode
else->
returnNone]
.isOnPath(node):
[ifnodeisaTreeobject->
->
return1
else->
return0]
"""
#---NodeId.__init__---
def__init__(self,path):
"""ConstructorfortheNodeIdobject
"""
self.path=path
#---NodeId.__str__---
def__str__(self):
"""Returnselfindisplayableform
"""
#--1--
#[L:=alistoftheelementsofself.pathconvertedtostrings]
L=map(str,self.path)
#--2--
#["/"]
returnstring.join(L,"/")
#---NodeId.find---
deffind(self,node):
"""
"""
returnself.__reFind(node,0)
#---NodeId.__reFind---
def__reFind(self,node,i):
"""Recursivenodefindingroutine.Startsatself.path[i:].
[if(nodeisaTreeobject)
and(0<=i<=len(self.path))->
ifi==len(self.path)->
returnnode'svalue
elseifself.path[i:]describesapathfromnode
tosometreeobjectT->
returnT
else->
returnNone]
"""
#--1--
ifi>=len(self.path):
returnnode.value#We'rethere!
else:
childNo=self.path[i]
#--2--
#[->
#child:=thatchildnode
#else->
#returnNone]
try:
child=node.nthChild(childNo)
exceptIndexError:
returnNone
#--3--
#[if(i+1)==len(self.path)->
#returnchild
#elseifself.path[i+1:]describesapathfromnodeto
#sometreeobjectT->
#returnT
#else->
#returnNone]
returnself.__reFind(child,i+1)
#---NodeId.isOnPath---
defisOnPath(self,node):
"""Isself'spathtoorthroughthegivennode?
"""
#--1--
#[nodePath:=pathlistfornode]
nodePath=node.fullPath()
#--2--
#[ifnodePathisaprefixof,oridenticaltoself.path->
#return1
#else->
#return0]
iflen(nodePath)>len(self.path):
return0#Nodeisdeeperthanself.path
foriinrange(len(nodePath)):
ifnodePath[i]!=self.path[i]:
return0#Nodeisadifferentroutethanself.path
return1
『貳』 我的世界怎麼設置一個人的重生點
命令方塊
首先輸入/give @p command_block 獲得一個命令方塊
然後放在地上,輸入/spawnpoint @p
旁邊放一個壓力板,就可以了。