当前位置:网站首页>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('')边栏推荐
- [security attack and Defense] how much do you know about serialization and deserialization?
- Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises
- Asynchronous development process - touch your hand and lead you to realize a promise
- qt下开发mqtt的访问程序
- [cloud native] those lines of code that look awesome but have a very simple principle
- DCDC电源电流定义
- Deep parsing structured exception handling (SEH) - by Matt Pietrek
- 郑州正清园文化传播有限公司:针对小企业的7种营销技巧
- Leetcode 121 best time to buy and sell stock (simple)
- Drozer tool
猜你喜欢

Change the background color of Kivy tutorial (tutorial includes source code)

技术管理 - 学习/实践

Can closed data be deleted by DBCA? can

GUI 应用:socket 网络聊天室

Main applications of TDK lambda power supply

附件五:攻击过程简报.docx

Definition of DCDC power supply current

MySQL JDBC编程

6-5 vulnerability exploitation SSH weak password cracking and utilization

Unity中RampTex介绍和应用: 溶解特效优化
随机推荐
GUI 应用:socket 网络聊天室
[go] database framework Gorm
附件一:202x年xxx攻防演习授权委托书
戳气球和布尔运算问题(巨难)
西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
Kivy tutorial custom fonts (tutorial with source code)
陪驾注意事项 这23点要注意!
RPC技术
Architecture practice camp - graduation project of module 9 of phase 6
分布式CAP理论
Unity中RampTex介绍和应用: 溶解特效优化
How do good test / development programmers practice? Where to go
Leetcode 121 best time to buy and sell stock (simple)
Correct the classpath of your application so that it contains a single, compatible version of com. go
Beipiao programmer, 20K monthly salary, 15W a year, normal?
[wechat applet] good looking carousel map component
MIN_ RTO dialog
Can closed data be deleted by DBCA? can
Rhcsa 07 - user and group management
Rhcsa 08 - automount configuration