当前位置:网站首页>Balanced binary tree (AVL)
Balanced binary tree (AVL)
2022-07-07 16:16:00 【Ceylon_ CC】
Catalog
The definition of balanced binary tree
Balanced binary tree insertion
LL Balance rotation ( Right single rotation )
RR Balance rotation ( Left single rotation )
LR Balance rotation ( First left then right double rotation )
RL Balance rotation ( First right then left double rotation )
Construction of balanced binary tree
Search of balanced binary tree
The definition of balanced binary tree
Q: What is a binary sort tree
A: Binary sort tree or an empty tree , Or a binary tree with the following properties
- 1) If its left subtree is not empty , be The left subtree Values of all nodes on All less than The value of its root
- 2) If its right subtree is not empty , be Right subtree Values of all nodes on All are greater than The value of its root
- 3) Left 、 The right subtree is also a binary sort tree
Q: What is a balanced binary tree
A: It could be an empty tree , Or a binary sort tree with the following properties : The absolute value of the depth difference between the left subtree and the right subtree does not exceed 1, And its left and right subtrees are a balanced binary tree .
The height difference between the left subtree and the right subtree of a node is defined as the balance factor of the node , Then the balance factor of the balanced binary tree node can only be -1,0,1.
Balanced binary tree insertion
Whenever you insert... In a binary sort tree ( Or delete ) When a node , First, check whether the nodes on the insertion path are unbalanced due to this operation . If it causes an imbalance , Then find the absolute value of the balance factor on the path closest to the insertion node is greater than 1 The node of A, Then to A Root subtree , On the premise of maintaining the characteristics of sorting tree , Adjust the position relationship of each node , Bring it back to balance .
The first half of the insertion process of balanced binary tree is the same as that of binary sort tree , But after the new node is inserted , If a node on the search path is no longer balanced , Then you need to make corresponding adjustments . The law of adjustment can be summarized as follows 4 In this case :
LL Balance rotation ( Right single rotation )
Because at the node A The left child (L) The left subtree (L) A new node is inserted on ,A The equilibrium factor of is 1 to 2 , Lead to A The root of the subtree is out of balance , A right rotation operation is required , take A The left child B Rotate right up instead of A Become the root node , take A The node rotates down to the right to become B The root node of the right subtree of , and B The original right subtree of is used as A The left subtree of the node .
RR Balance rotation ( Left single rotation )
Because at the node A The right child (R) The right subtree (R) A new node is inserted on ,A The equilibrium factor of is −1 Be reduced to −2, Lead to A The root of the subtree is out of balance , A rotation to the left is required . take A The right child B Rotate left and up instead of A Become the root node , take A The node rotates to the left and down to become B The root node of the left subtree of , and B The original left subtree of is used as A Right subtree of node .
LR Balance rotation ( First left then right double rotation )
stay A The left child (L) The right subtree (R) Insert new node on ,A The equilibrium factor of is 1 to 2, Lead to A The root of the subtree is out of balance , Two rotations are required , Rotate left first, then right . First the A The left child of the node B The root node of the right subtree of C Rotate upward to the left to lift to B The location of the node , Then put the C Rotate the node to the right and up to A The location of the node .
RL Balance rotation ( First right then left double rotation )
Because in A The right child (R) The left subtree (L) Insert new node on ,A The equilibrium factor of is −1 Be reduced to −2, Lead to A The root of the subtree is out of balance , Two rotations are required , First right then left . First the A The right child of the node B The root node of the left subtree of C Rotate upward to the right to lift to B The location of the node , Then put the C Rotate the node to the left and up to A The location of the node .
Construction of balanced binary tree
Assume that the keyword sequence is {6,1,2,5,4,3}, The process of generating binary sort tree through this sequence is as follows :
First step : Create an empty tree
The second step : Insert node 6
The third step : Insert node 1
Step four : Insert node 2
Step five :LR rotate
Step six : Insert node 5
Step seven : Insert node 4
Step eight :LL rotate
Step nine : Insert node 3
Step 10 :RL rotate
Search of balanced binary tree
The process of searching on a balanced binary tree is the same as that of a binary sort tree . therefore , During the search , The number of keywords compared with the given value does not exceed the depth of the tree . Suppose nh, The depth is h The minimum number of nodes in the balanced tree . obviously ,nh Yes n0=0,n1=1,n2=2, And there are nh=nh−1+nh−2+1. Can prove that , contain n The maximum depth of a balanced binary tree of nodes is O(log2n), Therefore, the average search length of balanced binary tree is O(log2n).
边栏推荐
- Leetcode-136-只出现一次的数(用异或来解答)
- Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
- Plate - forme de surveillance par étapes zabbix
- markdown公式编辑教程
- Logback日志框架第三方jar包 免费获取
- 模仿企业微信会议室选择
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- 招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
- 安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
- Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
猜你喜欢
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Xcode Revoke certificate
深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
You Yuxi, coming!
Logback logging framework third-party jar package is available for free
C4D learning notes 1- animation - animation key frames
企业级日志分析系统ELK
What about the pointer in neural network C language
Talk about the cloud deployment of local projects created by SAP IRPA studio
随机推荐
Leetcode-136-只出现一次的数(用异或来解答)
C4D learning notes 3- animation - animation rendering process case
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
There are many ways to realize the pause function in JS
JS array foreach source code parsing
Sysom case analysis: where is the missing memory| Dragon lizard Technology
TCP framework___ Unity
What are compiled languages and interpreted languages?
Mysql database backup script
Three. JS introductory learning notes 15: threejs frame animation module
Plate - forme de surveillance par étapes zabbix
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
Three singleton modes of unity (hungry man, lazy man, monobehavior)
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?