当前位置:网站首页>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('')边栏推荐
- 【安全攻防】序列化与反序列,你了解多少?
- Application scheme of Puyuan ds1000z series digital oscilloscope in communication principle experiment
- Correct the classpath of your application so that it contains a single, compatible version of com. go
- 红队视角下的防御体系突破之第二篇案例分析
- Deep parsing structured exception handling (SEH) - by Matt Pietrek
- 1. Mx6u-alpha development board (simulating STM32 drive development experiment)
- Wechat official account infinite callback authorization system source code
- Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
- 网络设备应急响应指南
- Acwing game 58
猜你喜欢

Annexe VI: exposé sur les travaux de défense. Docx

分布式CAP理论

Redis: hash type data operation command

The five pictures tell you: why is there such a big gap between people in the workplace?

Redis: operation command for collecting set type data

A beautiful API document generation tool
![[Yugong series] go teaching course 001 in July 2022 - Introduction to go language premise](/img/f2/3b95f53d67cd1d1979163910dbeeb8.png)
[Yugong series] go teaching course 001 in July 2022 - Introduction to go language premise

附件六:防守工作簡報.docx
![[wechat applet] good looking carousel map component](/img/66/4ae6a72fff419c7ed1ca015eb94c03.jpg)
[wechat applet] good looking carousel map component

Exploration and practice of eventbridge in the field of SaaS enterprise integration
随机推荐
分享一些我的远程办公经验
A beautiful API document generation tool
通过dd创建asm disk
Redis: hash type data operation command
附件四:攻击方评分标准.docx
Acwing game 58
Network - vxlan
Experience sharing of epidemic telecommuting | community essay solicitation
Drozer tool
leetcode:1314. Matrix area and [2D prefix and template]
leetcode:1314. 矩阵区域和【二维前缀和模板】
【微信小程序】好看的轮播图组件
Eig launched Grupo Cerro, a renewable energy platform in Chile
Annexe VI: exposé sur les travaux de défense. Docx
Kivy教程之 自定义字体(教程含源码)
Beipiao programmer, 20K monthly salary, 15W a year, normal?
[security attack and Defense] how much do you know about serialization and deserialization?
MIN_ RTO dialog
6-5漏洞利用-SSH弱口令破解利用
Virtual commodity account trading platform source code_ Support personal QR code collection