我是一个新手,如果大家在操作中发现有什么bug还请大家都评论一下,这也是我第一次发这个文章,还望大家多多指教。里面有部分参考内容,如果侵犯了您的权益,还望联系我进行删除。我也是第一次发,有些规矩可能不懂,还请求不要过分追究我的责任……
代码为先序输入二叉树中结点的值(每个结点一个字符,#表示表示空树),用递归算法建立一棵二叉树。实现先序中序后续遍历,交换两子树,统计度为0,1,2的结点,删除叶子结点功能
def is_number(s):#这个是为了判断字符串里面是否为整形或者浮点型
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
class Tree(object):
def __init__(self,data='#'):
self.data = data
self.lchild = None
self.rchild = None
def add1(self,fs=0): #fs是一个判断结点是否都是数值型的一个玩意
while 1:
data=input('输入:\n')
if data=='':
print('请重新输入\n')
else:
if is_number(data)==0 and data!='#':
fs=1
self.data=data
break
if self.data =='#':
return fs
else:
self.lchild=Tree()
fs=self.lchild.add1(fs)
self.rchild=Tree()
fs=self.rchild.add1(fs)
return fs
# 先序遍历
def preorder(self,array=[]):
if self.data == '#':
return []
else:
array.append(self.data)
self.lchild.preorder(array)
self.rchild.preorder(array)
return array
def inorder(self,array=[]) :#中序遍历
if self.data == '#':
return []
else:
self.lchild.inorder(array)
array.append(self.data)
self.rchild.inorder(array)
return array
def lastorder(self,array=[]):#后序遍历
if self.data == '#':
return []
else:
self.lchild.lastorder(array)
self.rchild.lastorder(array)
array.append(self.data)
return array
def leaf(self,a=[]):#统计叶子
if self.lchild.data!='#'and self.rchild.data!='#':
self.lchild.leaf(a)
self.rchild.leaf(a)
elif self.lchild.data=='#'and self.rchild.data!='#':
self.rchild.leaf(a)
elif self.rchild.data=='#'and self.lchild.data!='#':
self.lchild.leaf(a)
elif self.lchild.data=='#'and self.rchild.data=='#':
a.append(1)
return a
return a
def degree1(self,a=[]):#统计度为1的
if self.lchild.data!='#'and self.rchild.data!='#':
self.lchild.degree1(a)
self.rchild.degree1(a)
elif self.lchild.data=='#'and self.rchild.data=='#':
return a
elif self.lchild.data=='#'and self.rchild.data!='#':
a.append(1)
self.rchild.degree1(a)
elif self.rchild.data=='#'and self.lchild.data!='#':
a.append(1)
self.lchild.degree1(a)
return a
def degree2(self,a=[]):#统计度为2的
if self.lchild.data!='#'and self.rchild.data!='#':
a.append(1)
self.lchild.degree2(a)
self.rchild.degree2(a)
elif self.lchild.data=='#'and self.rchild.data=='#':
return a
elif self.lchild.data=='#'and self.rchild.data!='#':
self.rchild.degree2(a)
elif self.rchild.data=='#'and self.lchild.data!='#':
self.lchild.degree2(a)
return a
def exchange(self):#交换两个子树若子树只有一个则不交换
if self.lchild.data!='#'and self.rchild.data!='#':
self.lchild.exchange()
self.rchild.exchange()
self.lchild,self.rchild=self.rchild,self.lchild
elif self.lchild.data=='#'and self.rchild.data=='#':
return self
elif self.lchild.data=='#'and self.rchild.data!='#':
self.rchild.exchange()
elif self.rchild.data=='#'and self.lchild.data!='#':
self.lchild.exchange()
return self
def defleaf(self):#删除叶子
if self.lchild.data!='#'and self.rchild.data!='#':
if self.lchild.defleaf()==1:
self.lchild.data='#'
self.lchild.lchild=None
self.lchild.rchild=None
if self.rchild.defleaf()==1:
self.rchild.data='#'
self.rchild.lchild=None
self.rchild.rchild=None
elif self.lchild.data=='#'and self.rchild.data!='#':
if self.rchild.defleaf()==1:
self.rchild.data='#'
self.rchild.lchild=None
self.rchild.rchild=None
elif self.rchild.data=='#'and self.lchild.data!='#':
if self.lchild.defleaf()==1:
self.lchild.data='#'
self.lchild.lchild=None
self.lchild.rchild=None
elif self.lchild.data=='#'and self.rchild.data=='#':
return 1
return self
if __name__ == '__main__':
tree=Tree()
k1=tree.add1()
pre=tree.preorder([])
if k1==0:
print('最大值为',max(list(map(eval,pre))))#统计元素最大值
else:
print('二叉树内有非字符元素')
in1=tree.inorder([])
last=tree.lastorder([])
leaf=len(tree.leaf([]))
degree1=len(tree.degree1([]))
degree2=len(tree.degree2([]))
print('前序为:',pre)
print('中序为:',in1)
print('后序为:',last)
print('节点个数为%d'%len(pre))
print('叶子个数为%d'%leaf)
print('度为1结点个数%d'%degree1)
print('度为2结点个数%d'%degree2)
t2=tree.exchange()
pre2=t2.preorder([])
in2=t2.inorder([])
last2=t2.lastorder([])
print('交换后的前序为:',pre2)
print('交换后的中序为:',in2)
print('交换后的后序为:',last2)
tree.exchange()#由于前面交换了子树所以现在要交换回来
t3=tree.defleaf()
if t3 ==1:
print('已经是空表了')
else:
pre3=t3.preorder([])
in3=t3.inorder([])
last3=t3.lastorder([])
print('删除叶子后的前序为:',pre3)
print('删除叶子后的中序为:',in3)
print('删除叶子后的后序为:',last3)
?
|