insertion: AVL TREE

advertisement
AVL TREE insertion:
It’s a BST, Height of the left sub tree and Height of the right sub tree differ by at most one. We have
a balance factor of a node which means:
Balance factor= Height of the right sub tree- Height of the left sub tree.
And it must be 1,-1, and 0 for a tree to become an AVL tree.
Rotations, When to Use Them and Why
How to decide when you need a tree rotation is usually easy, but determining which type of rotation
you need requires a little thought.
A tree rotation is necessary when you have inserted or deleted a node which leaves the tree in an
unbalanced state. An unbalanced state is defined as a state in which any subtree has a balance factor
of greater than 1, or less than -1. That is, any tree with a difference between the heights of its two
subtrees greater than 1, is considered unbalanced.
We have two types of rotation:
1. Single Rotation:
 Left- Left Rotation (LL), (negative-negative).
 Right-Right Rotation (RR) (Positive-Positive).
2. Double Rotation:
 Left- Right Rotation (LR) (negative- Positive).
 Right- Left Rotation (RL) (Positive- negative).
EXAMPLE 1:
10
1-Insert 10:
BF (10) = 0-0=0
then tree is balanced.
2- Insert 12:
10
12
BF (12) = 0-0= 0
BF (10) = 1-0= 1
Then tree is balanced.
3- Insert 14:
10
12
14
9
BF (14) = 0-0= 0,
BF (12) =1-0= 1,
BF (10) = 2-0= 2 unbalanced tree (non AVL tree), then we check the problem we have 2 then 1 then
the rotation must be (Positive-Positive which mean RR Rotation.
We fallow the steps:
1- Right child become the new root.
2- Old root become the left child to the new root.
3- And the left child of the new root becomes right child to the old root.
Then the tree become like this:
12
10
14
Balance tree.
4- Insert 8:
12
14
10
8
BF (8) = 0-0=0,
BF (10) = 1-0 =-1,
BF (14) = 0-0=0,
BF (12) = 1-2= -1 then the tree is balanced, AVL tree.
5-Insert 6:
12
14
10
8
6
BF (6) = 0-0=0,
BF (8) = 0-1=-1,
01
BF (10) = 0-2 = -2, unbalanced tree (non AVL tree), then we check the problem we have- 2 then -1
then the rotation must be negative-negative which mean LL Rotation.
We fallow the steps:
1- Left child become the new root.
2- Old root become the Right child to the new root.
3- And the Right child of the new root becomes left child to the old root.
Then the tree become like this:
12
14
8
10
6
Balance tree.
6-Insert 9:
12
14
8
10
6
9
BF (9) = 0-0=0,
BF (10) = 0-1=-1,
BF (8) = 2-1 = 1,
BF (12) = 1-3 = -2, unbalanced tree (non AVL tree), then we check the problem we have -2 then 1
then the rotation must be negative-Positive which mean LR Rotation. We handle with four links:
10
12
10
8
8
8
10
12
12
Both 8 and 12 keep its child….
00
6
14
Then the tree become like this:
10
12
8
Balance tree.
14
9
6
7-Insert 13:
10
12
8
14
9
6
BF (13) = 0-0=0,
BF (14) = 0-1=-1,
13
BF (12) = 2-0 = 2,
Unbalanced tree (non AVL tree), then we check the problem we have 2 then -1 then the rotation
must be Positive- negative which mean RL Rotation.
Then the tree become like this:
10
13
8
Balance tree.
6
9
12
14
Deletion from an AVL Tree
First we will do a normal binary search tree delete. Note that structurally speaking, all deletes from a
binary search tree delete nodes with zero or one child. For deleted leaf nodes, clearly the heights of
the children of the node do not change.
Also, the heights of the children of a deleted node with one child do not change either. Thus, if a
delete causes a violation of the AVL Tree height property, this would HAVE to occur on some node
on the path from the parent of the deleted node to the root node. Thus, once again, as above, to
01
restructure the tree after a delete we will call the restructure method on the parent of the deleted
node. One thing to node: whereas in an insert there is at most one node that needs to be unbalanced,
there may be multiple nodes in the delete that need to be rebalanced.
For this example, we will delete the node storing 8 from the AVL tree below:
Tracing through the code, we find that we must first call the rebalance method on the parent of the
deleted node, which stores 16. This node needs rebalancing and gets restructured as follows:
We march up to the parent of the node storing 24, the node storing 32. Once again, this node is
imbalanced. The reason for this is that the restructuring of the node with a 16 reduced the height of
that subtree. By doing so, there was INCREASE in the difference of height between the subtrees of
the old parent of the node storing 16. This increase could propagate an imbalance in the AVL tree.
When we restructure at the node storing the 32, we identify the node storing the 56 as the tallest
grandchild. We get the final tree as follows:
01
Download