当前位置:网站首页>[interview ordinary people vs Expert Series] understanding of B tree and B + tree
[interview ordinary people vs Expert Series] understanding of B tree and B + tree
2022-04-21 10:58:00 【Front end technology station】
Share a practical interview question applet WeChat search MST Question treasure house

Data structure and algorithm problems , Troubled countless little friends .
Many small partners have a misunderstanding about data structure and algorithm , I don't think I use... In my work , Why do interviews ask , Asked can solve practical problems ?
Turing prize winner : Niklaus Wirth Said : Program = data structure + Algorithm , In other words, we are dealing with data structures all the time .
Just as Java Development , Due to the high maturity of the technical system , Makes most people think : The program should be equal to frame + SQL ah ?
Today, let's analyze a topic of data structure :”B Trees and B+ Trees “.
On this question , Let's take a look at the answers of ordinary people and experts !
Ordinary people :#
Um. . I think … Um. … Mysql It seems to use B+ Tree to index ! then …
master :#
In order to answer this question more clearly , I intend to answer from three aspects :
- Learn about binary trees 、AVL Trees 、B The concept of tree
- B Trees and B+ Tree application scenarios
- B A tree is a multi-path balanced search tree , For a better understanding .
Binary tree , Each node supports a tree structure of two branches , Compared with one-way linked list , One more branch .
Binary search tree , A rule is added to the binary tree , The value of all nodes in the left subtree is less than its root node , All child nodes of the right subtree are larger than its root node .

Skew tree problem will occur in binary search tree , Resulting in increased time complexity , Therefore, a balanced binary tree is introduced , It has all the characteristics of binary search tree , At the same time, a rule is added :” The absolute value of the height difference between its left and right subtrees shall not exceed 1“. Balanced binary trees will use left-handed 、 Right handed way to achieve balance .

and B A tree is a multi-path balanced search tree , It satisfies the rules of balanced binary tree , But it can have multiple subtrees , The number of subtrees depends on the number of keywords , For example, the root node in this figure has two keywords 3 and 5, Then the number of sub paths it can have = Key words +1.
Therefore, from this feature , When the same amount of data is stored , The height of balanced binary tree should be greater than B Trees .

B+ Trees , In fact B Enhancement based on tree , There are two big differences :
-
- B The data is stored on the tree of each node , and B+ The data in the tree is stored in leaf nodes , And connect the data in the leaf node through a linked list .
- B+ The number of sub paths of the tree is equal to the number of key words
This is B The storage structure of the tree , from B It can be seen from the tree that each node will store data .

This is B+ Trees ,B+ All the data of the tree is stored in the leaf node , And the data of leaf nodes is associated with a two-way linked list .

2.B Trees and B+ Trees , It is generally used in file system and database system , Used to reduce disk IO Performance loss .
With Mysql Medium InnoDB For example , When we go through select Statement to query a piece of data ,InnoDB Need to read data from disk , This process involves disks IO And random disk IO

We know that disks IO The performance of is particularly low , Especially random disks IO.
because , disk IO It works by , First, the system will transfer the logical address of the data to the disk , The disk control circuit translates the logical address into the physical address according to the addressing logic , That is to determine which track the data to be read is on , Which sector? .
In order to read the data of this sector , You need to put the head on top of this sector , To achieve this , The disk will keep spinning , Rotate the target sector under the head , Make the head find the corresponding track , This involves seek events and rotation time .
Obviously , disk IO The performance overhead of this process is very large , Especially when there is a large amount of data to query .
So in InnoDB in , Simply build an index on the data stored on the disk block , Then put the index data and the disk address corresponding to the index column , With B+ Store... In the form of a tree .
As shown in the figure , When we need to query the target data , According to the index from B+ Find the target data in the tree , because B+ There are many branches , So only a few disks are needed IO You can find it .

3. Why B Trees or B+ The tree is used as the index structure ? as a result of AVL Trees are taller than trees B The height of the tree should be high , And height means disk IO The number of . So in order to reduce the number of disks IO The number of times , File system or database will adopt B Trees or B+ Trees .
So that's me right B Trees and B+ Understanding of trees !
summary #
Data structure is very common in practical development , For example, array 、 Linked list 、 Double linked list 、 Red and black trees 、 Skip list 、B Trees 、B+ Trees 、 Line up, etc .
in my opinion , Data structure is one of the most important basic skills in programming .
Learned the sequence list and the chain list , We can know that sequential tables should be used in scenarios with more query operations , A linked list should be used in scenarios with many modification operations .
After learning the queue , I knew for FIFO In the scene , Queues should be used .
After learning the structure of trees , You will find the original scene of finding classes , It can also further improve query performance .
Basic skills determine the height you can reach in the technical post .
well , Ordinary people in this issue VS That's the end of the expert interview series , Remember to like your friends .
If you encounter some problems in scene class and scheme design class recently , Welcome to write to me , I'll give you an answer in the following content !
Share a practical interview question applet WeChat search MST Question treasure house

版权声明
本文为[Front end technology station]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211055570344.html
边栏推荐
- O2OA二次开发-使用开源平台搭建完整OA(3)-开发企业报销审批
- MATLAB GUI应用---摇奖(动画演示)
- GDI+中TGPImage从流中加载图像
- Go language error handling
- AcWing 1737. Transmission (classified discussion)
- MySQL修改最大连接数限制
- make the inifile support unicode in delphi
- O2oa secondary development - use the open source platform to build a complete OA (3) - development enterprise reimbursement approval
- Rhino software plug-in rhino plug-in visual studio create your first plug-in
- 【生活中的逻辑谬误】对人不对事和两难陷阱
猜你喜欢

IoT平台如何实现业务配置中心

Openshift 4 - improve client access API server security
![[非线性控制理论]1_Lyapunov直接方法](/img/ad/68bceb288d40ae98b60dbb83e0b91d.png)
[非线性控制理论]1_Lyapunov直接方法

【acwing】1459. 奶牛体操(模拟、思维)

Copyright loopholes in NFT: product design needs to consider the legal level

AcWing 1761. Block billboard (computational geometry, intersection of two rectangles)

MATLAB---坐标轴多图片显示

P4 Tutorials---- source routing

左程云 - 大厂刷题班 - 绳子覆盖最多的点

00000000000000000000000
随机推荐
后缀数组应用
Suffix Array
Six practices of Windows operating system security attack and defense
00000000000000000000000
手把手教你在云上开发一个图片压缩工具
The PHP file contains: require, require_ once; include,include_ once
便利店卷疯了:便利蜂、罗森、易捷“激战”
Browser plug-in (BD new tab) wallpaper appreciation
实践六 Windows操作系统安全攻防
【acwing】1459. Cow Gymnastics (simulation, thinking)
Mysql 002 DDL
RMQ & segment tree review
MATLAB---采用坐标轴及其子对象制作模拟时钟(动画演示)
GO 使用channel进行同步 (channel 1)
Go language reflection mechanism
An error occurred while processing your request... enable the Development environment by setting ...
AC自动机&后缀数组复习
拓扑排序&关键路径&数位dp
(坐标型动态规划)lintcode中等248 · 统计比给定整数小的数的个数
What if there are not enough mobile phones in Showcase? Cloud real machine platform atxserver2