class BiTree:
def __init__(self,key):
self._root=key
self._left_child=None
self._right_child=None
@property
def get_left(self):
return self._left_child
@property
def get_right(self):
return self._right_child
@property
def get_root_key(self):
return self._root
def set_root_key(self,key):
self._root=key
def insert_left(self,newNode):
if self._left_child==None:
self._left_child=newNode
else:
newNode._left_child=self._left_child
self._left_child=newNode
def insert_right(self,newNode):
if self._right_child==None:
self._right_child=newNode
else:
newNode._right_child=self._right_child
self._right_child=newNode
这种定义方式用的是按节点插入
代码如下:
a=BiTree(1)
a.get_root_key #输出 1
print(a.get_left) #输出 None
print(a.get_right) #输出 None
b=BiTree(2)
a.insert_left(b)
a.get_left.get_root_key #输出 2
c=BiTree(3)
a.insert_right(c)
a.get_right.get_root_key #输出 3
按值插入,应该这样定义:
class BiTree:
def __init__(self,key):
self._root=key
self._left_child=None
self._right_child=None
@property
def get_left(self):
return self._left_child
@property
def get_right(self):
return self._right_child
@property
def get_root_key(self):
return self._root
def set_root_key(self,key):
self._root=key
def insert_left(self,newNode_key):
if self._left_child==None:
self._left_child=BiTree(newNode_key)
else:
t=BiTree(newNode_key)
t._left_child=self._left_child
self._left_child=t
def insert_right(self,newNode_key):
if self._right_child==None:
self._right_child=BiTree(newNode_key)
else:
t=BiTree(newNode_key)
t._right_child=self._right_child
self._right_child=t
代码运行如下:
a=BiTree(1)
a.get_root_key #输出 1
a.insert_left(2)
a.get_left.get_root_key #输出 2
a.insert_right(3)
a.get_right.get_root_key #输出 3
Notes:
在Python中,下划线("_
")的含义有如下几种:
①在解释器中,"_
"代表交互式解释器会话中上一条执行的语句的结果
②名称前的单下划线,指定该名称属性为私有,(类和子类都可以访问)
③名称前的双下划线,(或两个前导下划线,最多再加一个后续下划线),表示私有成员变量,只有类对象自己能访问
④前后都有的双下划线,对于Python系统来说,将确保不会与用户自己定义的名称冲突
类中的self
代表累的实例,而非类
更多类的知识还在继续学习,今天刷leetcode就不明白为什么递归调用函数为什么也要加self
,还得继续找资料。
递归实现前序:
根——>左——>右
def Pre(root):
if not root:
return None
print(root.get_root_key)
Pre(root.get_left)
Pre(root.get_right)
代码
Pre(a) #输出123
递归实现中序:
左——>根——>右
def In(root):
if not root:
return None
In(root.get_left)
print(root.get_root_key)
In(root.get_right)
代码
In(a) #输出213
递归实现后序:
左——>右——>根
def Post(root):
if not root:
return None
Post(root.get_left)
Post(root.get_right)
print(root.get_root_key)
代码
Post(a) #输出231
实现层次遍历:
def level(root):
queue=[]
queue.append(root)
while len(queue)>0:
temp=queue.pop(0)
print(temp.get_root_key)
if temp.get_left:
queue.append(temp.get_left)
if temp.get_right:
queue.append(temp.get_right)
输出
level(a) #输出123
非递归实现先序:
def Pre(root):
stack=[]
stack.append(root)
while len(stack)>0:
temp=stack.pop()
print(temp.get_root_key)
if temp.get_right:
stack.append(temp.get_right)
if temp.get_left:
stack.append(temp.get_left)
Pre(a) #输出 123
非递归实现中序
中序思想是这样的:如果left
存在,就一直进栈,不然先打印自己的val
,然后转入right
继续这个过程
def In(root):
if root==None:
return
stack=[]
while root!=None or len(stack)>0:
while root!=None:
stack.append(root)
root=root.left
root=stack.pop()
print(root.val)
root=root.right
非递归实现后序
def post(root):
if root==None:
return
stack1=[root]
stack2=[]
while len(stack1)>0:
temp=stack1.pop()
if temp.right!=None:
stack1.append(temp.right)
if temp.left!=None:
stack1.append(temp.left)
stack2.append(temp)
while len(stack2)>0:
print(stack2.pop().val)
终于全实现了!!!!!!!!!!!!!!!