当前位置:网站首页>Binary search tree
Binary search tree
2022-07-04 04:47:00 【athrunsunny】
Binary search tree definition
Binary search tree (Binary Search Tree), Also known as binary sorting tree (Binary Sort Tree).
A binary search tree is a binary tree with the following properties :
(1) If left subtree is not empty , Then the values of all nodes on the left subtree are less than or equal to the values of its root nodes .
(2) If the right subtree is not empty , Then the value of all nodes on the right subtree is greater than or equal to the value of its root node .
(3) Left 、 The right subtree is also a binary search tree .
class TreeNode:
''' Definition of binary search tree node '''
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class OperationTree:
''' Binary search tree operation '''
def insert(self, root, val):
''' Binary search tree insertion operation '''
if root == None:
root = TreeNode(val)
elif val < root.val:
root.left = self.insert(root.left, val)
elif val > root.val:
root.right = self.insert(root.right, val)
return root
def query(self, root, val):
''' Binary search tree query operation '''
if root == None:
return False
if root.val == val:
return True
elif val < root.val:
return self.query(root.left, val)
elif val > root.val:
return self.query(root.right, val)
def findMin(self, root):
''' Find the minimum value point in the binary search tree '''
if root.left:
return self.findMin(root.left)
else:
return root
def findMax(self, root):
''' Find the maximum point in the binary search tree '''
if root.right:
return self.findMax(root.right)
else:
return root
def delNode(self, root, val):
''' Delete the median value of binary search tree as val The point of '''
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)
# When val == root.val when , There are three situations : Only the left subtree or only the right subtree 、 There are left and right subtrees 、 There is neither left subtree nor right subtree
else:
if root.left and root.right:
# There are both left and right subtrees , You need to find the minimum node in the right subtree
temp = self.findMin(root.right)
root.val = temp.val
# Then delete the minimum node in the right subtree
root.right = self.delNode(root.right, temp.val)
elif root.right == None and root.left == None:
# Left and right subtrees are empty
root = None
elif root.right == None:
# Only the left sub tree
root = root.left
elif root.left == None:
# Only the right subtree
root = root.right
return root
def printTree(self, root):
# Print binary search tree ( Medium order printing , An orderly sequence of numbers )
if root == None:
return
self.printTree(root.left)
print(root.val, end = ' ')
self.printTree(root.right)
if __name__ == '__main__':
List = [17,5,35,2,11,29,38,9,16,8]
root = None
op = OperationTree()
for val in List:
root = op.insert(root,val)
print(' Print binary search tree in middle order :', end = ' ')
op.printTree(root)
print('')
print(' The value of the root node is :', root.val)
print(' The maximum value in the tree is :', op.findMax(root).val)
print(' The minimum value in the tree is :', op.findMin(root).val)
print(' The value in the query tree is 5 The node of :', op.query(root, 5))
print(' The value in the query tree is 100 The node of :', op.query(root, 100))
print(' The value in the delete tree is 16 The node of :', end = ' ')
root = op.delNode(root, 16)
op.printTree(root)
print('')
print(' The value in the delete tree is 5 The node of :', end = ' ')
root = op.delNode(root, 5)
op.printTree(root)
print('')边栏推荐
- Use NRM and NVM to manage your NPM source and node versions
- What is context?
- 【云原生】那些看起来很牛X,原理却很简单的一行代码
- Can closed data be deleted by DBCA? can
- 戳气球和布尔运算问题(巨难)
- Senior developers tell you, how to write excellent code?
- [cloud native] those lines of code that look awesome but have a very simple principle
- Rhcsa 01 - create partitions and file systems
- EIG在智利推出可再生能源平台Grupo Cerro
- 新手找陪驾要注意什么
猜你喜欢

多位科技公司创始人向Entrepreneur First提供高达1.58亿美元的C轮融资,协助其投资下一代全球创新者

Redis: operation command for collecting set type data

Architecture training graduation design + summary

Redis: order collection Zset type data operation command

Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises

Statistical genetics: Chapter 3, population genetics

【云原生】那些看起来很牛X,原理却很简单的一行代码

The "functional art" jointly created by Bolang and Virgil abloh in 2021 to commemorate the 100th anniversary of Bolang brand will debut during the exhibition of abloh's works in the museum

Keysight n9320b RF spectrum analyzer solves tire pressure monitoring scheme

Rhcsa 04 - process management
随机推荐
Operate the server remotely more gracefully: the practice of paramiko Library
5张图告诉你:同样是职场人,差距怎么这么大?
20000 words will take you to master multithreading
Qt QTableView数据列宽度自适应
I.MX6U-ALPHA开发板(C语言版本LED驱动实验)
GUI 应用:socket 网络聊天室
Annexe VI: exposé sur les travaux de défense. Docx
附件一:202x年xxx攻防演习授权委托书
Rhcsa 07 - user and group management
博朗与Virgil Abloh于2021年为纪念博朗品牌100周年而联合打造的“功能性艺术”将在博物馆展出Abloh作品期间首次亮相
【微信小程序】好看的轮播图组件
Leader: who uses redis expired monitoring to close orders and get out of here!
附件2-2保密承诺书.docx
C language one-way linked list exercise
MIN_RTO 对话
How do good test / development programmers practice? Where to go
MIN_ RTO dialog
关闭的数据能用dbca删除吗? 能
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
软件设计文档示例模板 - 学习/实践