当前位置:网站首页>1. AVL tree: left-right rotation -bite
1. AVL tree: left-right rotation -bite
2022-07-07 05:29:00 【Aeolian u】
AVL Trees
AVL The definition of a tree
The absolute value of the height difference between the left and right subtrees does not exceed 1 Binary search tree of .
Yes n Nodes , The time complexity is log With n Bottom 2 The logarithmic ;
AVL Insertion of trees
Left spin : Left rotation means that the left subtree of the right subtree of the rotating node becomes its new right subtree , At the same time, the rotation node becomes the left subtree of the previous right subtree
Single rotation at right end : Right rotation means that the right subtree of the left subtree of the rotating node becomes its new left subtree , At the same time, the rotation node becomes the right subtree of the previous left subtree
When to use left-handed , Right hand , Double left and right , Right and left double choice ?
When adding a node to the right subtree of the right subtree of the node to be rotated : left-handed , When on the left subtree of the right subtree of the node to be rotated : Right left double rotation .
When adding nodes to the left subtree of the left subtree of the node to be rotated : Right hand , When on the right subtree of the left subtree of the node to be rotated : Double left and right .
Create a node class :
public class TreeNode {
public int val;
public int bf;
public TreeNode left;
public TreeNode right;
public TreeNode parent;
public TreeNode(int val){
this.val = val;
}
}
Insert elements :
public TreeNode root;// The root node
public boolean insert(int val) {
TreeNode node = new TreeNode(val);
if(root == null) {
root = node;
return true;
}
TreeNode parent = null;
TreeNode cur = root;
Give Way node Parent node of node (parent)
while (cur != null) {
if(cur.val < val) {
parent = cur;
cur = cur.right;
}else if(cur.val == val) {
return false;
}else {
parent = cur;
cur = cur.left;
}
}
//cur == null
Judge node, This insert parent Which subtree of .
if(parent.val < val) {
parent.right = node;
}else {
parent.left = node;
}
//
node.parent = parent;
cur = node;
// Modification of balance factor
while (parent != null) {
// First look at cur yes parent Left or right The balance factor is ++ still --
if(cur == parent.right) {
// If it is a right tree , Then the height of the right tree increases Balance factor ++
parent.bf++;
}else {
// If it is a left tree , Then the height of the left tree increases Balance factor --
parent.bf--;
}
// Check the current balance factor Is it an absolute value 1 0 -1
if(parent.bf == 0) {
// It shows that it has been balanced
break;
}else if(parent.bf == 1 || parent.bf == -1) {
// Continue upward to modify the balance factor
cur = parent;
parent = cur.parent;
}else {
if(parent.bf == 2) {
// Right tree height -》 You need to lower the height of the right tree
if(cur.bf == 1) {
// left-handed
rotateLeft(parent);
}else {
//cur.bf == -1
// Right left double rotation
rotateRL(parent);
}
}else {
//parent.bf == -2 Zuo Shugao -》 You need to lower the height of the left tree
if(cur.bf == -1) {
// Right hand
rotateRight(parent);
}else {
//cur.bf == 1
// Double left and right
rotateLR(parent);
}
}
// The above code is balanced
break;
}
}
return true;
}
Left spin : Left rotation means that the left subtree of the right subtree of the rotating node becomes its new right subtree , At the same time, the rotation node becomes the left subtree of the previous right subtree
private void rotateLeft(TreeNode parent) {
// Get the right subtree of the left rotation node
TreeNode subR = parent.right;
// Get the left subtree of the right subtree of the left rotation node
TreeNode subRL = subR.left;
// The right subtree of the left-hand node becomes the left subtree of the old right subtree of the left-hand node
parent.right = subRL;
// The left subtree of the old right subtree becomes a left-handed node
subR.left = parent;
// If the left subtree of the current old right subtree is not empty , Then let its parent node point to the left-handed node .
if(subRL != null) {
subRL.parent = parent;
}
// First save the parent node of the left rotation point , So that it points to subR
TreeNode pParent = parent.parent;
parent.parent = subR;
Determine whether the left rotation node is the root node
if(root == parent) {
root = subR;
root.parent = null;
}else {
// Determine which node of the left-handed node is the parent node .
if(pParent.left == parent) {
pParent.left = subR;
}else {
pParent.right = subR;
}
subR.parent = pParent;
}
// Adjust the balance factor
subR.bf = parent.bf = 0;
}
Single rotation at right end : Right rotation means that the right subtree of the left subtree of the rotating node becomes its new left subtree , At the same time, the rotation node becomes the right subtree of the previous left subtree
private void rotateRight(TreeNode parent) {
TreeNode subL = parent.left;
TreeNode subLR = subL.right;
parent.left = subLR;
subL.right = parent;
// No, subLR
if(subLR != null) {
subLR.parent = parent;
}
// You must first record
TreeNode pParent = parent.parent;
parent.parent = subL;
// Check Is it currently the root node
if(parent == root) {
root = subL;
root.parent = null;
}else {
// Not the root node , Judge whether this subtree is a left subtree or a right subtree
if(pParent.left == parent) {
pParent.left = subL;
}else {
pParent.right = subL;
}
subL.parent = pParent;
}
subL.bf = 0;
parent.bf = 0;
}
Double left and right : When adding nodes to the left subtree of the left subtree of the node to be rotated : Right hand , When on the right subtree of the left subtree of the node to be rotated : Double left and right .
private void rotateLR(TreeNode parent) {
TreeNode subL = parent.left;
TreeNode subLR = subL.right;
int bf = subLR.bf;
rotateLeft(parent.left);
rotateRight(parent);
// from dubLT.bf Value (-1/1) Adjust the balance factor .
if(bf == -1) {
subL.bf = 0;
subLR.bf = 0;
parent.bf = 1;
}else if(bf == 1){
subL.bf = -1;
subLR.bf = 0;
parent.bf = 0;
}
}
Right left double rotation : When adding a node to the right subtree of the right subtree of the node to be rotated : left-handed , When on the left subtree of the right subtree of the node to be rotated : Right left double rotation .
// Right left double rotation
private void rotateRL(TreeNode parent) {
TreeNode subR = parent.right;
TreeNode subRL = subR.left;
int bf = subRL.bf;
rotateR(parent.right);
rotateL(parent);
if(bf == -1){
parent.bf = 0;
subR.bf = 0;
subRL.bf = 1;
}else if(bf == 1){
parent.bf = -1;
subR.bf = 0;
subRL.bf = 0;
}
}
Judge whether it is AVL Trees
private int height(TreeNode root) {
if(root == null) return 0;
int leftH = height(root.left);
int rightH = height(root.right);
return leftH > rightH ? leftH+1 : rightH+1;
}
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
int leftH = height(root.left);
int rightH = height(root.right);
if(rightH-leftH != root.bf) {
System.out.println(" This node :"+root.val+" The balance factor is abnormal ");
return false;
}
return Math.abs(leftH-rightH) <= 1
&& isBalanced(root.left)
&& isBalanced(root.right);
}
AVL Delete tree ( undetermined )
边栏推荐
猜你喜欢
Autowired注解用于List时的现象解析
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
数字化如何影响工作流程自动化
Make web content editable
JHOK-ZBL1漏电继电器
Lombok插件
阿里云的神龙架构是怎么工作的 | 科普图解
人体传感器好不好用?怎么用?Aqara绿米、小米之间到底买哪个
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
Record a pressure measurement experience summary
随机推荐
拿到PMP认证带来什么改变?
ScheduledExecutorService定时器
1.AVL树:左右旋-bite
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
K6el-100 leakage relay
[JS component] date display.
Mysql database learning (8) -- MySQL content supplement
Leetcode (417) -- Pacific Atlantic current problem
Mybaits之多表查询(联合查询、嵌套查询)
利用OPNET进行网络仿真时网络层协议(以QoS为例)的使用、配置及注意点
【oracle】简单的日期时间的格式化与排序问题
batch size设置技巧
Talk about mvcc multi version concurrency controller?
As we media, what websites are there to download video clips for free?
创始人负债10亿,开课吧即将“下课”?
高压漏电继电器BLD-20
阿里云的神龙架构是怎么工作的 | 科普图解
Pytest testing framework -- data driven
Summary of the mean value theorem of higher numbers
Timer创建定时器