搜索二叉树概念:
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为二叉搜索树
程序:
说明:delNode是递归删除结点。Delete是迭代删除树节点,其中涉及了查找最近父节点。
class Node():
"""创建树节点"""
def __init__(self,val=None, left = None, right=None):
self.val = val
self.left = left
self.right = right
class BSTree():
"""创建搜索二叉树"""
result = []
def __init__(self, Node_list):
self.root = Node(Node_list[0])
for data in Node_list[1:]:
self.insert(data)
def search(self,item):
node = self.root
while 1:
if not node:
return node
if node.val == item:
return node
elif node.val < item:
node = node.right
else:
node = node.left
def insert(self, item):
"""在搜索二叉树中插入元素,先找到父节点,在进行插入节点的操作"""
parent = self.root
while 1:
if item >= parent.val:
if parent.right == None:
parent.right = Node(item)
return
else:
parent = parent.right
else:
if parent.left is None:
parent.left = Node(item)
return
else:
parent = parent.left
def Ascend(self):
self.inorderTraverse(self.root)
print(self.result)
def inorderTraverse(self, root):
if root == None:
return []
self.inorderTraverse(root.left)
self.result.append(root.val)
self.inorderTraverse(root.right)
def printTree(self, root):
# 打印二叉搜索树(中序打印,有序数列)
if root == None:
return
self.printTree(root.left)
print(root.val, end=' ')
self.printTree(root.right)
def delNode(self,root,val):
"""删除二叉树中一个节点"""
if root == None:
return
if val < root.val:
root.left = self.delNode(root.left, val)
elif val > root.val:
root.right = self.delNode(root.right, val)
# 当val == root.val时,分为三种情况:只有左子树或者只有右子树、有左右子树、即无左子树又无右子树
else:
if root.left and root.right:
# 既有左子树又有右子树,则需找到右子树中最小值节点
temp = self.findMin(root.right)
root.val = temp.val
# 再把右子树中最小值节点删除
self.delNode(root.right, temp.val)
elif root.right == None and root.left == None:
# 左右子树都为空
root = None # 对地址的操作
elif root.right == None:
# 只有左子树
root = root.left
elif root.left == None:
# 只有右子树
root = root.right
return root
def GetParent(self, val):
if val is None:
print("val值为空")
return
return self.FindeParent(self.root, val)
def FindeParent(self,current, find):
if find is None:
return
if current is None:
return
if current.left is not None and current.left == find:
return current
elif current.right is not None and current.right == find:
return current
temp = self.FindeParent(current.left, find)
if temp is not None:
return temp
return self.FindeParent(current.right, find)
def Delete(self,val):
"""删除关键词为val的树节点"""
current = self.search(val) # 查找到该结点
if current is not None: # 如果该结点不为空
parent = self.GetParent(current)
if current.left is None: # 如果current左子结点为空
if parent is None: # 如果current为根结点
self.root = current.right
elif parent.left == current:
parent.left = current.right
else:
parent.right = current.right
else:
temp = current.left
while temp.right: # 查走current左子树关键词最大的树节点
temp = temp.right
temp.right = current.right # 将current的右子树接到左子树的最大结点上
if parent is None:
self.root = current.left
elif parent.left == current:
parent.left = current.left
else:parent.right =current.left
a = [49, 38, 65, 97, 60, 76, 13, 27, 5, 1,1]
bst = BSTree(a) # 创建二叉查找树
bst.Ascend()