Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: - - PowerPoint PPT Presentation

data structures and
SMART_READER_LITE
LIVE PREVIEW

Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: - - PowerPoint PPT Presentation

CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, 4.4 Izmir University of Economics 1 AVL Trees An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition . It must be


slide-1
SLIDE 1

CE 221 Data Structures and Algorithms

Chapter 4: Trees (AVL Trees)

Text: Read Weiss, §4.4

1 Izmir University of Economics

slide-2
SLIDE 2

AVL Trees

  • An AVL (Adelson-Velskii and Landis) tree is

a binary search tree with a balance

  • condition. It must be easy to maintain, and

it ensures that the depth of the tree is O (log N).

  • Idea 1: left and

right subtrees are

  • f the same height

(not shallow).

Izmir University of Economics 2

slide-3
SLIDE 3

Balance Condition for AVL Trees

  • Idea 2: Every node must have left and right

subtrees of the same height. The height of an empty tree is -1 (Only perfectly balanced trees with 2k-1 nodes would satisfy). AVL Tree = BST with every node satisfying the property that the heights of left and right subtrees can differ

  • nly by one.

The tree on the left is an AVL tree. The height info is kept for each node.

Izmir University of Economics 3

slide-4
SLIDE 4

The Height of an AVL Tree - I

  • For an AVL Tree with N nodes, the height is at

most 1.44log(N+2)-0.328.

  • In practice it is slightly more than log N.
  • Example: An AVL tree of height 9 with fewest

nodes (143).

Izmir University of Economics 4

slide-5
SLIDE 5
  • The minimum number of nodes, S(h), in an AVL tree of

height h is given by S(0)=1, S(1)=2, S(h) = S(h-1) + S(h-2) +1

  • Recall Fibonacci Numbers? (F(0) = F(1) = 1, F(n)=F(n-1) + F(n-2))
  • Claim: S(h) = F(h+2) -1 for all h ≥ 0.
  • Proof: By induction. Base cases; S(0)=1=F(2)-1=2-1,

S(1)=2=F(3)-1=3-1. Assume by inductive hypothesis that claim holds for all heights k ≤ h. Then,

  • S(h+1) = S(h) + S(h-1) + 1

= F(h+2) -1 +F(h+1) -1 + 1 (by inductive hypothesis) = F(h+3) – 1

  • We also know that

Izmir University of Economics 5

The Height of an AVL Tree - II

                 

    h h h F 2 5 1 2 5 1 5 1 ) (

slide-6
SLIDE 6
  • Thus, all the AVL tree operations can be performed

in O (log N) time. We will assume lazy deletions. Except possibly insertion (update all the balancing information and keep it balanced)

Izmir University of Economics 6

The Height of an AVL Tree - III

2 2 2 5 1 5 1 ) ( 1 1 2 2 5 1 5 1 ) ( 1 ) 2 ( ) ( 1 2 5 1 5 1 ) ( 2 5 1 2 5 1 5 1 ) (                   

                                                                     

h h S h h S h F h S h h F h h h F ) (log 327 . ) 2 log( 44 . 1 2 5 1 log * 2 5 log 2 2 5 1 log ) 2 log( 2 5 1 log ) 2 ( 5 log 2 1 ) 2 log( 2 2 5 1 5 1 2 2 2 2 5 1 5 1 ) ( n O h h N h N h N h N h h S N                         

                                       

slide-7
SLIDE 7

AVL Tree Insertion

  • Example: Let’s try to insert 6 into the AVL tree below. This

would destroy the AVL property of the tree. Then this property has to be restored before the insertion step is considered over.

  • It turns out that this can always be done with a simple

modification to the tree known as rotation. After an insertion,

  • nly the nodes that are on the path from the insertion point to

the root might have their balance altered. As we follow the path up to the root and update the balancing information there may exist nodes whose new balance violates the AVL condition. We will prove that our rebalancing scheme performed once at the deepest such node works.

Izmir University of Economics 7 6

slide-8
SLIDE 8
  • Let’s call the node to be balanced α. Since any node has at

most 2 children, and a height imbalance requires that α‘s 2 subtrees’ height differ by 2. There are four cases to be considered for a violation:

1) An insertion into left subtree of the left child of α. (LL) 2) An insertion into right subtree of the left child of α. (LR) 3) An insertion into left subtree of the right child of α. (RL) 4) An insertion into right subtree of the right child of α. (RR)

  • Cases 1 and 4 are mirror image symmetries with respect to α,

as are Cases 2 and 3. Consequently; there are 2 basic cases.

  • Case I (LL, RR) (insertion occurs on the outside) is fixed by a

single rotation.

  • Case II (RL, LR) (insertion occurs on the inside) is fixed by

double rotation.

Izmir University of Economics 8

AVL Tree Rotations

slide-9
SLIDE 9

Single Rotation (LL)

  • Let k2 be the first node on the path up violating AVL

balance property. Figure below is the only possible scenario that allows k2 to satisfy the AVL property before the insertion but violate it afterwards. Subtree X has grown an extra level (2 levels deeper than Z now). Y cannot be at the same level as X (k2 then out

  • f balance before insertion) and Y cannot be at the

same level as Z (then k1 would be the first to violate).

Izmir University of Economics 9

slide-10
SLIDE 10

Izmir University of Economics 10

  • Note that in single rotation inorder traversal orders
  • f the nodes are preserved.
  • The new height of the subtree is exactly the same

as before. Thus no further updating of the nodes

  • n the path to the root is needed.

Single Rotation (RR)

slide-11
SLIDE 11
  • AVL property destroyed by insertion of

6, then fixed by a single rotation.

  • BST node structure needs an additional

field for height.

11 Izmir University of Economics

Single Rotation-Example I

slide-12
SLIDE 12

Izmir University of Economics 12

Single Rotation-Example II

  • Start with an initially empty tree and insert items 1

through 7 sequentially. Dashed line joins the two nodes that are the subject of the rotation.

slide-13
SLIDE 13

Izmir University of Economics 13

Insert 6. Balance problem at the root. So a single rotation is performed. Finally, Insert 7 causing another rotation.

Single Rotation-Example III

slide-14
SLIDE 14

Izmir University of Economics 14

Double Rotation (LR, RL) - I

  • The algorithm that works for cases 1 and 4 (LL, RR)

does not work for cases 2 and 3 (LR, RL). The problem is that subtree Y is too deep, and a single rotation does not make it any less deep.

  • The fact that subtree Y has had an item inserted into

it guarantees that it is nonempty. Assume it has a root and two subtrees.

slide-15
SLIDE 15

Izmir University of Economics 15

Below are 4 subtrees connected by 3 nodes. Note that exactly one of tree B or C is 2 levels deeper than D (unless all empty). To rebalance, k3 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k2’s left child and k3 to be its right child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored; so, all the balancing and and height updating is complete.

Double Rotation (LR) - II

slide-16
SLIDE 16

Izmir University of Economics 16

Double Rotation (RL) - III

In both cases (LR and RL), the effect is the same as rotating between α’s child and grandchild and then between α and its new child. Every double rotation can be modelled in terms of 2 single rotations. Inorder traversal

  • rders are always preserved between k1, k2, and k3.

Double RL = Single LL (α->right)+ Single RR (α) Double LR = Single RR (α->left)+ Single LL (α )

slide-17
SLIDE 17

Izmir University of Economics 17

  • Continuing our example, suppose keys 8

through 15 are inserted in reverse order. Inserting 15 is easy but inserting 14 causes a height imbalance at node 7. The double rotation is an RL type and involves 7, 15, and 14.

Double Rotation Example - I

slide-18
SLIDE 18

Izmir University of Economics 18

  • insert 13: double rotation is RL that will

involve 6, 14, and 7 and will restore the tree.

Double Rotation Example - II

slide-19
SLIDE 19

19 Izmir University of Economics

Double Rotation Example - III

  • If 12 is now inserted, there is an imbalance at

the root. Since 12 is not between 4 and 7, we know that the single rotation RR will work.

slide-20
SLIDE 20

Izmir University of Economics 20

Double Rotation Example - IV

  • Insert 11: single rotation LL; insert 10: single

rotation LL; insert 9: single rotation LL; insert 8: without a rotation.

slide-21
SLIDE 21

Izmir University of Economics 21

Double Rotation Example - V

  • Insert 8½: double rotation LR. Nodes 8, 8½, 9

are involved.

slide-22
SLIDE 22

Implementation Issues - I

  • To insert a new node with key X into an AVL

tree T, we recursively insert X into the appropriate subtree of T (let us call this TLR). If the height of TLR does not change, then we are done. Otherwise, if a height imbalance appears in T, we do the appropriate single or double rotation depending on X and the keys in T and TLR, update the heights (making the connection from the rest of the tree above), and are done.

Izmir University of Economics 22

slide-23
SLIDE 23

Izmir University of Economics 23

Implementation Issues - II

  • Another efficiency issue concerns storage of

the height information. Since all that is really required is the difference in height, which is guaranteed to be small, we could get by with two bits (to represent +1, 0, -1) if we really

  • try. Doing so will avoid repetitive calculation
  • f balance factors but results in some loss of
  • clarity. The resulting code is somewhat more

complicated than if the height were stored at each node.

slide-24
SLIDE 24

Izmir University of Economics 24

Implementation Issues - III

  • First, the declarations. Also, a quick function

to return the height of a node dealing with the annoying case of a NULL pointer.

slide-25
SLIDE 25

Izmir University of Economics 25

Implementation - LL

/* This function can be called only if k2 has a left */ /* child. Perform a rotate between k2 and its left */ /* child k1. Update heights, then return new root */

slide-26
SLIDE 26

Izmir University of Economics 26

Implementation - RR

/* This function can be called only if k1 has a right */ /* child. Perform a rotate between k1 and its right */ /* child k2. Update heights, then return new root */ private AvlNode<AnyType> rotateWithRightChild(AvlNode<AnyType> k1) { AvlNode<AnyType> k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = Math.max(height(k1.left), height(k1.right))+1; k2.height = Math.max(height(k2.right), k1.height)+1; return k2; }

slide-27
SLIDE 27

Izmir University of Economics 27

Implementation - LR

/* Double LR = Single RR (α->left)+ Single LL (α ) */ /* This function can be called only if k3 has a left */ /* child and k3's left child k1 has a right child k2 */ /* single rotation between k1 and k2 followed by */ /* single rotation between k3 and k2 */

slide-28
SLIDE 28

Izmir University of Economics 28

Implementation - RL

/* Double RL = Single LL (α->right)+ Single RR (α) */ /* This function can be called only if k1 has a right */ /* child k3 and k1's right child has a left child k2 */ private AvlNode<AnyType> doubleWithRightChild(AvlNode<AnyType> k1) { k1.right = rotateWithLeftChild( k1.right ); return rotateWithRightChild( k1 ); }

slide-29
SLIDE 29

Izmir University of Economics 29

AVL Tree Insertion

slide-30
SLIDE 30

Izmir University of Economics 30

Balancing

slide-31
SLIDE 31

AVL Tree Deletion

Izmir University of Economics 31

slide-32
SLIDE 32

Homework Assignments

  • 4.18, 4.19, 4.25
  • You are requested to study and solve the
  • exercises. Note that these are for you to

practice only. You are not to deliver the results to me.

Izmir University of Economics 32