二叉树的遍历和重构,二叉搜索树(python)

二叉树遍历

构建树节点

class TreeNode(object):
	def __init__(self,data):
		self.data=data
		self.lchild=None
		self.rchild=None

前序遍历

root-左-右

def pre_track(root):
	if root:
		print(root,end=",")
		pre_track(root.lchild)
		pre_track(root.rchild)

中序遍历

左-root-右

def in_track(root):
	if root:
		in_track(root.lchild)
		print(root.data,end=",")
		in_track(root.rchild)

后序遍历

左-右-root

def post_track(root):
	if root:
		post_track(root.lchild)
		post_track(root.rchild)
		print(root.data,end=",")

层次遍历

思路:借助队列,如果节点没有孩子,就会出队,直到队为空,所有的节点被遍历。

from collections import deque
def level_track(root):
	if root:
		queue=deque()
		queue.append(root)
		while len(queue)>0:
			node=queue.popleft()
			print(node.data,end-",")
			if node.lchild:
				queue.append(node.lchild)
			if node.rchild:
				queue.append(node.rchild)			

重构

已知前序和中序,如何重构二叉树?
思路:前序的第一个是root,在中序里面找到root的位置,root左边为root的左子树,右边为root的右子树。取出左子树的前序和中序,继续上面的过程。递归直到没有节点。

def TreeReconstruction(tpre,tin):
	if len(tpre)==0
		return None
	root=TreeNode(tpre[0])
	tin_index=tin.index(tpre[0])
	left_tree=TreeReconstruction(tpre[1:tin_index+1], tin(:tin_index))
	right_tree=TreeReconstruction(tpre[tin_index+1:], tin[tin_index+1:])
	return root	

二叉搜索树

特点

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。没有键值相等的节点。

构建及相关操作

递归写法-增删查

def add(root,val):
	if not root:
		root=TreeNode(val)
	else:
		if root.data>val:
			root.lchild=add(root.lchild,val)
		if root.data<val:
			root.rchild=add(root.rchild,val)
	return root
def find(root,val):
	if not root:
		return False
	if root.data==val:
		return True
	elif root.data>val:
		return find(root.lchild,val)
	else:
		return find(root.rchild.val)
def delete(root,val):
	if not root:
		return
	if root.data>val:
		root.lchild = delete(root.lchild,val)
	elif root.data<val:
		root.lchild = delete(root.rchild,val)
	else:
		if not root.rchild and not root.lchild:
			root=None
		elif root.rchild:
			root=root.rchild
		elif root.lchild:
			root=root.lchild	
		else:
			cur=root
			while cur.lchild:
				cur=cur.lchild
			root=cur
			root.rchild=delete(root.rchild,cur.data)
	return root

非递归写法-增查

class BST(object):
	def __init__(self,data=None):
		self.root=TreeNode(data)
	def find(self,val):
		if not self.root:
			return False
		cur=self.root
		while cur:
			if cur.data==val:
				return True
			elif cur.data>val:
				cur=cur.lchild
			else:
				cur=cur.rchild
		return False
	def add(self,val):
		node=TreeNode(val
		if not self.root:
			self.root=node
			return
		if self.find(val):
			return
		cur=self.root
		while cur:
			pre=cur
			if cur.data>val:
				cur=cur.lchild
			else:
				cur=cur.rchild
		if pre.data>val:
			pre.lchild=node
		else:
			pre.rchild=node	

简单拓展:AVL

二叉搜索树如果根节点不小心选择了最大值或者最小值,就会出现只有一边有树,所有的操作达到最大时间复杂度O(n),为了解决这个问题提出了AVL树。AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值