The deletion of nodes has always been a difficult problem in the operation of the two-fork tree, and the node deletion in the balanced binary tree is more complex, since the deletion of the node operation should maintain its equilibrium characteristics.
So give us the removal of the operation brought some small trouble.
Previous article I wrote about the operation of the node insertion of the AVL tree, when I added a height parameter to the definition of the binary tree node, which represents the
The node is the depth of the subtree of the root. In this article I will delete this parameter and use a method depth () to find the depth of the node. If the reader is interested, take the action of this parameter yourself
Add in, the difficulty is not very big, logic on the OK. Although the efficiency is so much lower, but the difficulty reduced a lot.
OK still first the knot structure definition is called out:
typedef int ELEMTYPE;
typedef struct NODE
{
elemtype data;
struct Node * lchild;
struct Node * rchild;
} Avlnode,*avltree;
The operation of deleting a node still needs to be analyzed in several cases:
1, if the node to be deleted is a leaf, that is, the leaf knot point:
Then directly delete this node, and then from its parent node to determine whether the node is still satisfied with the balance of the characteristics, the use of recursion has been judged to the root node so far,
Using recursion is easy to solve.
2. Deletion of non-leaf nodes:
The deletion of non-leaf nodes is a little troublesome, there are 2 kinds of situations, and each of these two cases has a symmetrical situation.
A) The Zuozi of a node T is the deepest node that does not satisfy the equilibrium property after the node operation is deleted, and its left subtree depth is greater than the depth of the right sub-tree. i.e. (depth (l)-depth (r) = = 2)
This indicates that the deleted node is on the right subtree of the left subtree of T. This is where we use the right double rotation to balance it (the method of rotation is always described in the previous article, here I only write the method of rotation to not explain too much).
b) The Zuozi of a node T is the deepest node that does not satisfy the equilibrium property after the node operation is deleted, and its right subtree depth is greater than the depth of the left sub-tree. i.e. (Depth (r)-depth (l) = = 2)
The deleted node is on the right subtree of the left subtree of T. This is where we use the right single rotation to balance it.
Each of these two methods has a symmetric situation, which is solved by using the left rotation.
I'll write the method of rotation first:
Avltree Singlerotatewithleft (avltree K2)
{
avltree k1;
K1 = k2->lchild;
K2->lchild = k1->rchild;
K1->rchild = K2;
return k1;
}
Avltree singlerotatewithright (avltree K2)
{
avltree k1;
K1 = k2->rchild;
K2->rchild = k1->lchild;
K1->lchild = K2;
return k1;
}
Avltree doublerotatewithleft (avltree K1)
{
k1->lchild = singlerotatewithright (k1->lchild);
return Singlerotatewithleft (k1);
}
Avltree doublerotatewithright (avltree K1)
{
k1->rchild = Singlerotatewithleft (k1->rchild);
return Singlerotatewithright (k1);
}
These rotations are made exactly the same as the rotation operation when inserting.
Then there is the whole method of deleting the operation:
Because deleting a node may result in the rotation of its ancestor nodes, it is possible to save the ancestors of the nodes in advance. That is, the path from the root node to the T1 nodes. The adjustment is initiated from the parent node of the T1 to the mountain (which may persist to the root node), so its path is appropriate to be stored in the stack. This allows the node to be ejected from the stack in turn to determine if adjustments are required.
Note that if T1 is not a leaf node, you need to find the leftmost node of its left subtree or the leftmost node of the right subtree T2, at which point the adjustment starts from the parent node of T2, so you also need to record T1 to T2 path, that is, the resulting path is from the root node to T2.
When adjusting a node, remember that node is TC, assuming its parent node is TP. You need to point the left (or right) subtree pointer of the TP to the new TC that was adjusted.
Because deleting a node may result in the rotation of its ancestor nodes, it is possible to save the ancestors of the nodes in advance. That is, the path from the root node to the T1 nodes. The adjustment is initiated from the parent node of the T1 to the mountain (which may persist to the root node), so its path is appropriate to be stored in the stack. This allows the node to be ejected from the stack in turn to determine if adjustments are required.
Note that if T1 is not a leaf node, you need to find the leftmost node of its left subtree or the leftmost node of the right subtree T2, at which point the adjustment starts from the parent node of T2, so you also need to record T1 to T2 path, that is, the resulting path is from the root node to T2.
When adjusting a node, remember that node is TC, assuming its parent node is TP. You need to point the left (or right) subtree pointer of the TP to the new TC that was adjusted.
Avltree Delete (Avltree t,elemtype e)
{
if (T = = NULL)
{
return null;
}
else if (E < t->data)
{
t->lchild = Delete (t->lchild,e);
if (Depth (t->rchild)-Depth (t->lchild) = = 2)
{
if (Depth (T->rchild->rchild) < Depth (t-> Rchild->lchild))
{
t = doublerotatewithright (t);
}
else
{
T = singlerotatewithright (t);}}
}
else if (E > T->data)
{
t->rchild = Delete (t->rchild,e);
if (Depth (t->lchild)-Depth (t->rchild) = = 2)
{
if (Depth (T->lchild->lchild) < Depth (t >lchild->rchild))
{
t = doublerotatewithleft (t);
}
else
{
T = singlerotatewithleft (t);}}
}
else if (T->lchild = = NULL && T->rchild = = null)
{free
(T);
return NULL;
}
else
{
Avltree tmp = findmin (t->rchild);
T->data = tmp->data;
T->rchild = Delete (T->rchild,tmp->data);
}
return T;
}
Learning is very painful but after learning to fully understand the rotation is very easy, I wish you happy.