Python implements examples of tree sorting algorithms for first middle and last order
- 2020-06-07 04:45:52
- OfStack
An example of Python tree is presented in this paper. To share for your reference, specific as follows:
#encoding=utf-8
class Tree():
def __init__(self,leftjd=0,rightjd=0,data=0):
self.leftjd = leftjd
self.rightjd = rightjd
self.data = data
class Btree():
def __init__(self,base=0):
self.base = base
# The former sequence traversal About the root
def qout(self,jd):
if jd == 0:
return
print jd.data
self.qout(jd.leftjd)
self.qout(jd.rightjd)
# In the sequence traversal Left root right
def mout(self,jd):
if jd == 0:
return
self.mout(jd.leftjd)
print jd.data
self.mout(jd.rightjd)
# After the sequence traversal About the root
def hout(self,jd):
if jd == 0:
return
self.hout(jd.leftjd)
self.hout(jd.rightjd)
print jd.data
jd1 = Tree(data=8)
jd2 = Tree(data=9)
base = Tree(jd1,jd2,7)
x = Btree(base)
x.qout(x.base)
print '\r\n'
x.mout(x.base)
print '\r\n'
x.hout(x.base)
For more information about Python, please refer to Python Data Structure and Algorithm Tutorial, Python Function Skills Summary, Python String Manipulation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Operation Skills Summary.
I hope this article has been helpful in Python programming.