Sei sulla pagina 1di 28

1

AVL Trees

Outline and Required Reading:

• AVL Trees (§ 9.2)

COSC 2011, Fall 2003, Section A


Instructor: N. Vlajic
2

AVL Trees

Binary Search Trees – better than “linear” dictionaries; however, the


worst–case performance of find, insert, remove
operations still linear (O(n))
Height-Balance Property – for every internal node v of T, the heights
of the children of v can differ at most by 1;
if v has only one (internal node) child, its
height must be 1

AVL Tree – any binary search tree T that satisfies the height-balance
property is said to be an AVL Tree

44 4

17 2 56 3
If an AVL tree has n nodes,
what is the cost of
1 5 32 1 48 2 77 1 find, insert, remove ??

51 1
3

AVL Trees (cont.)

Balance Factor – the difference in heights of its two subtrees (hR-hL)


of a Node

hR
hL

≤1; if |BF|>1, the node is unbalanced


Balanced Node – node with |BF|≤

Balance Factor – corresponds to the balance factor of its root node


of Binary Trees • tree is “left-heavy” if BF ≤ -1
• tree is “equal-height” if BF = 0
• tree is “right-heavy” if BF ≥ +1

Balance Factor – BF of each node in an AVL tree can only ∈ {-1, 0, 1}


of AVL Trees
4

AVL Trees (cont.)

Proposition 1.A: The height of an AVL tree T storing n items is O(log2n).

Proposition 1.B: The number of internal nodes in an AVL tree of height


h is Ω(2h).

Proof: fix h, then find the corresponding min number of nodes in the tree

• h=1 ⇒ n=1

• h=2 ⇒ n=2 or n=3 ⇒ nmin(2)=2

≥3
• h≥ ⇒ the tree contains the root and
- two AVL subtrees of heights (h-1), or
- one AVL subtree of height (h-1) and the other of height (h-2)

nmin(h) = 1 + nmin(h-1) + nmin(h-2) ∗)


(∗
5

AVL Trees (cont.)

h h
h-1 h-1 h-1 h-2

Why the following possibility should not be considered??

h
h-1
6

AVL Trees (cont.)

∗) we obtain
Since nmin(h-1) > nmin(h-2), from (∗
1
nmin(h) > 2nmin(h-2)
1
nmin(h) > 4nmin(h-4)
h-2
nmin(h) > 8nmin(h-6)

∗ ∗)
(∗ nmin(h) > 2i ⋅nmin(h-2i), ∀ i such that h-2i ≥1

nmin(1) and nmin(2) are known, hence we will pick i such that h-2i = 1 or 2.

h odd ⇒ pick i such that h-2i=1 ⇒ i=h/2-1/2


h 
i =   −1
h even ⇒ pick i such that h-2i=2 ⇒ i=h/2 - 1 2
7

AVL Trees (cont.)

 h   h 
   −1  −1
∗) and (∗
From (∗ ∗ ∗) ⇒ n(h) ≥ nmin (h) > 2  2 
nmin (1 or 2 ) ≥ 2 2 

 h2 
n(h) = Ω 2 
 

Based on the above, an AVL tree storing n items


has height at most 2log(n)+2.

h < 2 ⋅ log(n) + 2

h(n) = O(log(n))
8

AVL Trees: Update Operations

AVL Trees – binary search trees that guarantee height-balance property,


i.e. search-algorithm time complexity of O(log(n))
• if an insert / delete destroys the balance, the balance
must be restored through one or more “rotations”
• NOTE: insertion in an AVL tree is performed using the
BST’s insertItem method (with expandExternal);
removal from an AVL tree is performed using the BST’s
removeElement method (with removeAboveExternal)

Proposition 2: An AVL tree with BF=0, for all nodes, cannot be become
unbalanced after a single insert / delete operation.

Proof: left for exercise …


9

AVL Trees: Update Operations (cont.)

Rotation – restructuring of the tree that maintains the binary search


tree property
• inorder traversal must produce the same results on
both original and restructured tree

Types of Rotation

(1) Single-Left Rotation


(2) Single-Right Rotation
(3) Double Left-Right Rotation
(4) Double Right-Left Rotation
10

AVL Trees: Update Operations (cont.)

(1) & (2) - Single-Left and Single-Right Rotations

Original Tree Single-Left Rotation Single-Right Rotation

Y Z X

X Z Y Y
ZR XL
X Z
XL XR ZL ZR ZL XR

XL, X, XR, Y, ZL, Z, ZR XL XR ZL ZR

XL, X, XR, Y, ZL, Z, ZR XL, X, XR, Y, ZL, Z, ZR

Single Left and Right Rotation preserve the inorder traversal ordering,
i.e. they preserve binary search tree property.
11

AVL Trees: Update Operations (cont.)

(3) Double Left-Right Rotation

Original Tree After Single Left-Rotation Double Left-Right Rotation


Y Y Z

X Z X Y
YR YR
Z X

XL ZR XL ZL ZR YR

ZL ZR XL ZL

XL, X, ZL, Z, ZR, Y, YR XL, X, ZL, Z, ZR, Y, YR XL, X, ZL, Z, ZR, Y, YR

Double Left-Right Rotation preserves the inorder traversal ordering.


12

AVL Trees: Update Operations (cont.)

(4) Double Right-Left Rotation

Original Tree After Single Right-Rotation Double Right-Left Rotation


Y Y Z

X Z y X
YL YL
Z X
XR ZL YL ZL ZR XR

ZL ZR ZR XR

YL, Y, ZL, Z, ZR, X, XR YL, Y, ZL, Z, ZR, X, XR YL, Y, ZL, Z, ZR, X, XR

Double Right-Left Rotation preserves the inorder traversal ordering.


13

AVL Trees: Insertion

Insertion Scenarios that Require Reconstruction

Y BF=1 (a) BF(Y)=1 and new node is inserted in ZR


after insertion: BF(Z)+=1, BF(y)=2
X Z
(b) BF(Y)=1 and new node is inserted in ZL
after insertion: BF(Z)-=1 , BF(Y)=2
XL XR
ZL ZR

Y BF=-1 (c) BF(Y)=-1 and new node is inserted in XL


after insertion: BF(X)-=1, BF(Y)=(-2)
X Z
(d) BF(y)=1 and new node is inserted in XR
after insertion: BF(X)+=1, BF(Y)=(-2)
ZL ZR
XL XR
14

AVL Trees: Update Operations on Insertion

(a) Insertion in Right Subtree of Right Child – BF of the right child


changes form 0 to +1

Y BF=+1 Y BF=+2 X BF=0

X BF=0 X BF=+1 Y BF=0


h h
YL YL h+1
h h h h+1 h h XR
1 XL XR XL YL XL
2
XR
N

Original AVL Tree Unbalanced AVL Tree Restored AVL Tree


(after insertion) (after single-left rotation)
Tree height is decreased ! !!!

Similarly, in case (c), imbalance caused by insertion in the left subtree


of the left child, can be restored with a single-right rotation.
15

AVL Trees: Update Operations on Insertion (cont.)

(b) Insertion in Left Subtree of Right Child – BF of the right child


changes form 0 to -1

Y BF=+1 Y BF=+2 Z BF=0

X BF=0 X BF=-1 Y BF=0 X BF=1


h h
Z BF=0 Z BF=-1
YL h YL h h h-1 h
ZR
XR XR YL ZL XR
1 h-1 Z Z h ZR
L R 2 ZL
N

Original AVL Tree Unbalanced AVL Tree Restored AVL Tree


(after insertion) (after right-left rotation)
Tree height is decreased ! !!!

Similarly, case (d), imbalance caused by insertion in the right subtree


of the left child can be restored with a left-right rotation.
16

AVL Trees: Update Operations on Insertion (cont.)

Could case (b) be fixed with a single-left rotation???

Y BF=+2 X BF=-2

X BF=-1 Y BF=+1
h h
Z BF=-1
YL h
BF=-1 Z XR
h h
XR
h ZR YL
2 ZL ZR
ZL
N
N

Unbalanced AVL Tree AVL Tree


(after insertion) after single-left rotation
17

AVL Trees: Update Operations on Insertion (cont.)

Types of Imbalance due to Insertion and Corresponding “Fixes”

cause of imbalance BF(Y) BF(RC) required rotation


new node inserted in
the right-subtree of +2 +1 single-left
Y’s right child (RC)
new node inserted in
the left-subtree of +2 -1 double right-left
Y’s right child (RC)
new node inserted in
the left-subtree of -2 -1 single-right
Y’s left child (LC)
new node inserted in
the right-subtree of -2 +1 double left-right
Y’s left child (LC)
18

AVL Trees: Update Operations on Insertion (cont.)

Rotation-Based Algorithm for AVL-Tree Reconstruction

if root R of a subtree becomes unbalanced to the right, after insertion


of a new node
if BF(rightChild(R))=+1 then { single-left rotation around R }
if BF(rightChild(R))=-1 then { single-right rotation around rightChild(R);
single-left rotation rotation around R }

if root R of a subtree becomes unbalanced to the left, after insertion


of a new node
if BF(leftChild(R))=-1 then { single-right rotation around R }
if BF(leftChild(R))=+1 then { single-left rotation around leftChild(R);
single-right rotation rotation around R }
19

AVL Trees: Update Operations on Insertion (cont.)

Restructure Algorithm AVL-Tree Reconstruction (from textbook)

Part 1: Insertion

1.1 perform a BST insertion of element into Tree (T),


and let N be the newly-created node containing GP |BF|=2
element
1.2 let C be either N or an ancestor of C such that P |BF|=1
grandparent(C) is the nearest height-unbalanced
ancestor of C |BF|=1 C

1.3 let rotated-st be the subtree obtained by rotating T1


C, parent(C) and grandparent(C)
T2
1.4 let rotated-st replace the subtree of T rooted at T3 T4
grandparent(C)
N

unbalanced AVL tree


20

AVL Trees: Update Operations on Insertion (cont.)

Part 2: Rotation

2.1 Let (X, Y, Z) be a left-to-right (inorder) enumeration of C, parent(C),


grandparent(C)
2.2 Let (T1, T2, T3, T4) be a left-to-right (inorder) enumeration of the four
subtrees of X, Y, Z (these subtrees are not rooted at X, Y, Z)
2.3 Let rotated-st be constructed as follows:
(a) its top node is Y
(b) Y’s left and right children are X and Z Y
(c) X’s left and right subtrees are T1 and T2
(d) Z’s left and right subtrees are T3 and T4 X Z
2.4 Return rotated-st.

T3 T4
T1 T2
N

rotated-st
21

AVL Trees: Update Operations on Insertion (cont.)

Example 1 [ reconstruction after insertion using restructure algorithm ]

Z |BF|=2 Y

Y |BF|=1 X Z

X |BF|=1

T4
T1 T2
T3 T3 T4
T1 T2 N

unbalanced AVL tree reconstructed AVL tree


22

AVL Trees: Update Operations on Insertion (cont.)

Example 2 [ reconstruction after insertion using restructure algorithm ]

X |BF|=2 Y

Z |BF|=1 X Z

y |BF|=1
T1
T2 T3
T4 T1 T4
N
T2 T3

unbalanced AVL tree reconstructed AVL tree


23

AVL Trees: Update Operations on Insertion (cont.)

Remarks on restructure Algorithm


• Preserves the inorder traversal ordering of all the nodes in T.
• Unifies all 4 types of rotation into a single restructure operation.
• Restores height-balance property locally, at the three involved
nodes (A, B, C).
• Restores height-balance property globally. (This property applies
to each of the 4 rotations, as well.)
" new subtree is shorter by 1, hence all ancestors that became
unbalanced after insertion, are now balanced

A |BF|=2 B |BF|<=1

h+1 h
T
T
24

AVL Trees: Removal

AVL-Tree Reconstruction after Deletion using restructure Algorithm

Let us assume that an AVL Tree has become unbalanced after performing
removeAboveExternal(W).

1. Let Z be the first height-unbalanced node encountered while moving up


the tree from W.
Let Y be the child of Z with the larger height.
Let X be the child of Y with the larger height. (May not be unique.)
2. Do restructure on (X, Y, Z) to restore balance at the subtree rooted at Z.
3. Continue checking for unbalanced nodes higher in the tree.
(NOTE: restructure operation on lower nodes can destroys the balance
of higher nodes.)
25

AVL Trees: Removal (cont.)

Example 2 [ reconstruction after deletion using restructure algorithm ]

44 Z
62 Y
17 62 Y
44 Z 78 X
32 50 78 X 17 50 88

48 54 88 48 54

unbalanced AVL tree reconstructed AVL tree


26

AVL Trees: Implementation

Link-Structure vs. Array – link-structure better - restructure operation


can be performed in O(1) time
" restructure in array implementation very
costly if “index-updates” are required

Computation of BFs – find & store each node’s height in the node itself;
BF(v) = BF(rightChild(v).h) – BF(leftChild(v).h)
" cost of finding all BFs – O(n)

AVLItem class – extends Item class (as used in BSTs)


" defines additional instance variable height

AVL Tree class – extends BinarySearchTree class


" inherits: size, isEmpty, findElement, findAllElements,
removeAllElements
" overrides: insertItem, removeElement
27

AVL Trees: Implementation (cont.)

Link Structure for AVL Trees - Performance

Method Time
size, isEmpty O(1)
findElement, insertItem, removeElement O(log(n))
findAllElements, removeAllElements O(log(n)+s)
s - size of returned iterator

findElement - O(log(n))
" has down-phase (search ) only

insertElement - O(log(n))
" initial findElement runs in O(log(n)) up
" restructuring up the tree runs in O(log(n)) down phase
" maintaining heights runs in O(log(n)) phase
28

AVL Trees: Questions

Q.1 Assume that the following tree is a standard binary search tree
(sorted lexicographically). Draw this tree after the insertion of a new
node with element=R.

G S

P Y

Now, assume that the tree in the picture is an AVL tree. Draw the AVL
tree that will result when you insert a new node with element=R.

Finally, assume that the pictured above is an AVL tree. Draw the AVL
tree that will result when you insert a new node with element=T.

Potrebbero piacerti anche