Python简单前序创建二叉树及二叉树的遍历

一、学习要点:
1.python中不能用结构体,C和C++实现二叉树多用结构体,python中只用对象;
二、代码实现:

class Tree():
	def __init__(self,data=-1,lchild=None,rchild=None):
		self.data=data
		self.lchild=lchild
		self.rchild=rchild
	def CreateTree(self):
		data=int(input("please input the data:"))
		if(data==-1):
			return
		else:
			self.data=data
			self.lchild=Tree()
			self.lchild.CreateTree()
			self.rchild.CreateTree()
	def preorder_print(self):
		if(self.data==-1):
			return
		else:
			print('%d'%(self.data))
			self.lchild.preorder_print()
			self.rchild.preorder_print()

三、运行结果
Python简单前序创建二叉树及二叉树的遍历