数据结构与算法实现——二叉搜索树
结构分析:
本结构主要使用 连链表来建立二叉树 与链表不同的是 每个对象存储的三个指针 为 parent left 和 right
使用两个类:节点建立类(节点对象的产生) 树操作类(节点对象的属性操作)
几个重点类方法:
(1)__init __:使用递归的方法产生树的结构 使用了一个 辅助函数_new_tree_helper来递归创建 用于操作节点类初始化、属性填充
(2)throught:遍历树函数 有一个可选参数 用于控制遍历方法(前序遍历 中序遍历 后序遍历) 分别对应三个辅助函数 (可以看出 这些辅助函数基本上都是用于执行递归的代码块)
(3)search:用中序遍历的方法 比较每个节点值与要search的值 将匹配值的节点对象放入list用于返回
(4)delete:作为类中最为复杂的函数 借助了search方法来寻找到要delete的节点 根据不同的节点情况 调用辅助函数1用于旋转树(或者叫做替换链的回归) 最后用辅助函数2 将函数1所得的树结构转变为最后的目标结构
#part three:二叉树链表
class Node_new_Tree():
def __init__(self,data,left,right,parent):
self.data=data
self.right=right
self.left=left
self.parent=parent
#part four:二叉搜索树
class Search_tree():
def __init__(self,list=[]):
if list==[]:
self.root=Node_new_Tree(None,None,None,None) #注意:一般不能对空树插入操作
else:
self.root=Node_new_Tree(list[0],None,None,None)
position=1 #输入list位置指针
while position<len(list):
self._new_tree_helper(list[position],self.root)
position+=1
def _new_tree_helper(self,data,node): #这是个递归的填充data的函数
if data>=node.data:
if node.right==None:
node.right=Node_new_Tree(data,None, None,node)
else:
return self._new_tree_helper(data,node.right)
elif data<node.data:
if node.left==None:
node.left=Node_new_Tree(data,None,None,node)
else:
return self._new_tree_helper(data,node.left)
def throught(self,method='center'):
self.throught_list=[] #初始化遍历存储器
if method=='center': #中序遍历
self.throught_center_helper(self.root)
elif method=='before': #前序遍历
self.throught_before_helper(self.root)
elif method=='after': #后序遍历
self.throught_after_helper(self.root)
else:
raise ValueError
return self.throught_list
def throught_center_helper(self,node): #中序遍历辅助函数
self.throught_list.append(node.data)
if node.left!=None:
self.throught_center_helper(node.left)
if node.right!=None:
self.throught_center_helper(node.right)
def throught_before_helper(self,node): #前序遍历辅助函数
if node.left!=None:
self.throught_before_helper(node.left)
self.throught_list.append(node.data)
if node.right!=None:
self.throught_before_helper(node.right)
def throught_after_helper(self,node): #后序遍历辅助函数
if node.right!=None:
self.throught_after_helper(node.right)
self.throught_list.append(node.data)
if node.left!=None:
self.throught_after_helper(node.left)
def insert(self,data):
self._new_tree_helper( data, self.root)
def search(self,search_data): #基于中序遍历的查找函数 返回node对象列表(当没有要查找时 返回空列表)
self.search_list=[]
self.search_helper(self.root,search_data)
return self.search_list
def search_helper(self,node,search_data):
if node.data==search_data:
self.search_list.append(node)
if node.left != None:
self.search_helper(node.left,search_data)
if node.right != None:
self.search_helper(node.right,search_data)
def delete(self,delete_data):
delete_object_list=self.search(delete_data)
for delete_object in delete_object_list:
if delete_object.parent!=None: #不是根节点
if delete_object.right==None and delete_object.left==None:
if delete_object==delete_object.parent.right:
delete_object.parent.right=None
else:
delete_object.parent.left=None
elif delete_object.left==None: #左子树为空
if delete_object==delete_object.parent.left: #为父节点的左节点
delete_object.parent.left=delete_object.right
delete_object.right.parent=delete_object.parent
else:
delete_object.parent.right=delete_object.right
delete_object.right.parent = delete_object.parent
elif delete_object.right==None:
if delete_object==delete_object.parent.left:
delete_object.parent.left=delete_object.left
delete_object.left.parent = delete_object.parent
else:
delete_object.parent.right=delete_object.left
delete_object.left.parent = delete_object.parent
else:
self.probe=delete_object.right
while self.probe.left!=None:
self.probe=self.probe.left
self._delete_helper2(self._delete_helper1(self.probe))
else:
if delete_object.left==None and delete_object.right==None:
del(self) #或者改为 不可删除单节点的树??
elif delete_object.left==None:
self.root=delete_object.right
delete_object.right.parent=None
elif delete_object.right==None:
self.root=delete_object.left
delete_object.left.parent=None
else:
self.root=delete_object.right
self.probe = delete_object.right
while self.probe.left != None:
self.probe = self.probe.left
self._delete_helper2(self._delete_helper1(self.probe))
def _delete_helper1(self,node): #这是一个translate的辅助函数
while node.parent.left==node:
node_father = node.parent
node_grandfather = node.parent.parent
node_rightSon = node.right
node.right = node_father
node.parent = node_grandfather
node_father.parent = node
node_father.left = node.right
node.right.parent = node_father
if node_grandfather.left==node_father:
node_grandfather.left=node
else:
node_grandfather.right = node
return node
def _delete_helper2(self,node):
node_father=node.parent
node.left=node.parent.left
node.parent=node_father.parent
if node.parent!=None:
if node.parent.right==node_father:
node.parent.right=node
else:
node.parent.left=node
def change(self,data_changeby,data_changed):
self.delete(data_changed)
self.insert(data_changeby)
def __repr__(self):
list=self.throught()
return 'length:' + str(len(list)) + '\nlink_dataList: ' + str(list)
def __len__(self):
return len(self.throught())
这段代码经过测试后无异常 可以直接复制使用
望大佬指点
若有异常 联系我