当前位置:网站首页>Binary tree and figure 1
Binary tree and figure 1
2022-06-10 14:09:00 【tnan2522】
Binary tree
The binary tree is divided into Full binary tree , Perfect binary tree , Ordinary binary tree
Full binary tree

A full binary tree has two leaf nodes for each node , The nodes of the whole tree are full , This is the full binary tree
Perfect binary tree

A complete binary tree consists of all but the last layer of leaf nodes , All parent nodes are full binary trees , And the leaf nodes of the last layer are distributed from left to right , A full binary tree must be a complete binary tree , A complete binary tree is not necessarily a full binary tree
Ordinary binary tree

Besides completing binary tree and full binary tree , The others are ordinary binary trees
Binary tree storage
Complete binary tree storage is suitable for storing in an array , Because a complete binary tree is continuous , It is more suitable to store in an array
Incomplete binary trees are not suitable for storing in arrays , You can use a linked list to store
Binary tree linked list storage python Code implementation
class Node:
def __init__(self, item=None):
self.data = item
self.left_next = None
self.right_next = None
class HashList:
def __init__(self):
self.item = None
def add(self, item):
node = Node(item)
if self.item is None:
self.item = node
return
head = self.item
head_item = head
while head:
head_item = head
head = head.left_next if item < head.data else head.right_next
if item < head_item.data:
head_item.left_next = node
else:
head_item.right_next = node
def preorder(self, item):
if item is not None:
print(item.data, end=" ")
self.preorder(item.left_next)
self.preorder(item.right_next)
def search(self, item):
head = self.item
if not head:
print(item, " non-existent ")
return False
while head:
if head.data == item:
print(item, " There is ")
return True
head = head.left_next if item < head.data else head.right_next
print(item, " non-existent ")
return False
def rm(self, item):
head = self.item
head_itme = None
while head:
if head.data == item:
# If the data to be deleted is a leaf node ,
if not head.right_next and not head.left_next:
# here head_itme It is the root node of this leaf node , Determine whether the data to be deleted is on the left or right of the root node
if item < head_itme.data:
head_itme.left_next = None
else:
head_itme.right_next = None
return
elif head.left_next and not head.right_next:
# If it's not a leaf node , And only the left leaf node , Then the root node's left_next Set as... Of the data to be deleted left_next
head_itme.left_next = head.left_next
return
elif head.right_next and not head.left_next:
# If it's not a leaf node , And only the right leaf node , Then the root node's right_next Set as... Of the data to be deleted right_next
head_itme.right_next = head.right_next
return
else:
# If you find the node to delete , When a node has left and right branches , Replace the minimum value of the right branch of the node
# Get the right node of this node
head1 = head.right_next
head2 = None
# Determine whether the right node has left and right next node
if head1.right_next and head1.left_next:
# Loop to get the minimum leaf node of the right node
while head1.left_next:
head2 = head1
head1 = head1.left_next
# At present head1 It is the smallest leaf node down the right side of the data to be deleted
head.data = head1.data
# head2 It is the root node of this leaf node , Put this left_next That is, the leaf node to be replaced is deleted
head2.left_next = None
return
else:
# This indicates that the right leaf node of the node to be deleted has no left or right leaf nodes , It is equivalent to a single chain ,
# Then, the whole single chain will be used to replace the nodes to be deleted
head.data = head1.data
# Determine whether the right leaf node of the node to be deleted has a left leaf node or a right leaf node ,
# If there is a right , Then directly replace the replaced right node , If there is a left , Then replace the replaced node
# The right node of is connected to the left node
if head1.right_next:
head.right_next = head1.right_next
else:
head.right_next = head1.left_next
return
head_itme = head
head = head.right_next if head.data < item else head.left_next
def find_max(self):
head = self.item
if not head:
return
while head.right_next:
head = head.right_next
print(" The maximum is :", head.data)
def find_min(self):
head = self.item
if not head:
return
while head.left_next:
head = head.left_next
print(" The minimum is :", head.data)
if __name__ == '__main__':
lists = [10, 8, 50, 6, 9, 40, 55, 2, 3, 77, 54, 1, 2.5, 0]
map_hash = HashList()
for i in lists:
# Loop to insert data into the binary tree
map_hash.add(i)
# Output the data stored in the binary tree
map_hash.preorder(map_hash.item)
print()
# Find out if the data exists
map_hash.search(1000)
# Delete data
map_hash.rm(10)
# Output deleted data
map_hash.preorder(map_hash.item)
print()
# Find the maximum and minimum values
map_hash.find_max()
map_hash.find_min()
The traversal of binary tree has Before the order Middle preface In the following order Traverse , What is in the current code is only the preorder traversal
The former sequence traversal Refer to , For any node in the tree , Print this node first , Then print its left subtree , Finally print its right subtree . In the sequence traversal Refer to , For any node in the tree , First print its left subtree , And then print it itself , Finally print its right subtree After the sequence traversal Refer to , For any node in the tree , First print its left subtree , Then print its right subtree , Finally, print the node itself .

Binary search tree
The binary tree is queried through the root node and Compare the data you want to find , If the data to be found is smaller than the root node , Then look down from the left side of the root node , Because data smaller than the root node is stored on the left , Data larger than the root node is stored on the right ,, It's a bit like dichotomy
边栏推荐
- What happened when the legendary login prompt failed to connect to the server? How to solve it?
- C#多线程学习笔记四
- 618. How to prepare for the great promotion
- erp odoo 权限管理5年系统设置经验小结 真经验分享
- Main features of IIC bus / communication process / read / write process
- Gin blog 总结1
- UE5如何将屏幕坐标转为世界坐标和世界方向
- Google Earth engine (GEE) -- batch download of DEM using MODIS leaf area index image mask
- 什么是CAS 以及 CAS 中的 ABA 问题
- 2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)
猜你喜欢
![[notes] notes on C language array pointer, structure + two-dimensional array pointer](/img/77/5923754b12ccfe0bcba7e46617de86.png)
[notes] notes on C language array pointer, structure + two-dimensional array pointer
![[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use](/img/b4/f7838a7e12379190e2bc9b869839f0.png)
[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use

C#多线程学习笔记三
![[discrete mathematics review series] II. First order logic (predicate logic)](/img/f3/c7e61462a012ca1b88dca7b1ecdb25.png)
[discrete mathematics review series] II. First order logic (predicate logic)

What does the multi cloud management platform CMP mean? Who can explain it clearly

架构实战营 第 6 期 模块八课后作业

【解决】每次加载已经训练好的模型,生成的向量会有不同

UE5如何將屏幕坐標轉為世界坐標和世界方向

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

【原创】POI 5.x XSSF和HSSF使用自定义字体颜色
随机推荐
[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use
40 necessary methodologies for large factories
net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
[discrete mathematics review series] III. concept and operation of sets
Use of 5.8G microwave radar module, working principle and introduction of 5.8G microwave radar module
[operation tutorial] how to correctly use the Hikvision demo tool to configure the channel to go online?
Allan方差与随机误差辨识
C multithreading learning note 1
NC|王军/宋默识结合三代测序解析肠道菌群结构变异和功能
What does the multi cloud management platform CMP mean? Who can explain it clearly
C multithreading learning note 4
还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
Brief description of adaptive function
Solve the problem of cross sea high concurrent crash? so easy
The relocation of Apple's production line shows that 5g industrial interconnection and intelligent manufacturing have limited help for manufacturing in China
New features mail GPU counter module adds GPU primitive processing and GPU shader cycles
【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
C#多线程学习笔记三
2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试
Google Earth engine (GEE) -- batch download of DEM using MODIS leaf area index image mask