Presentation is loading. Please wait.

Presentation is loading. Please wait.

4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.

Similar presentations


Presentation on theme: "4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ."— Presentation transcript:

1 4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ by at most one.  A tree is said to be height-balanced or AVL if for each node, the height of the left subtree and height of the right subtree differ by at most one.

2 4.5 AVL Trees  Height of AVL tree is 1.44log(N+2) -0.328  logN  searching complexity  O(log N)

3 4.5 AVL Trees  The height of the left subtree minus the height of the right subtree of a node is called the balance of the node. For an AVL tree, the balances of the nodes are always -1, 0 or 1.  Given an AVL tree, if insertions or deletions are performed, the AVL tree may not remain height balanced.

4 4.5 AVL Trees  Violation may occur when an insertion into 1. left subtree of left child (LL case) 2. right subtree of left child (RL case) 3. left subtree of right child (LR case) 4. right subtree of right child (RR case)

5 4.5 AVL Trees n+1 n n From: J Beidler, Data structures and algorithms, Springer1997

6 4.5 AVL Trees  To maintain the height balanced property of the AVL tree after insertion or deletion, it is necessary to perform a transformation on the tree so that (1) the inorder traversal of the transformed tree is the same as for the original tree (i.e., the new tree remains a binary search tree). (2) the tree after transformation is height balanced.

7 4.5 AVL Trees  Rotation - to restore the AVL tree after insertion - single rotation - double rotation

8 4.5 AVL Trees  single rotation

9 4.5 AVL Trees  Double rotation

10 4.5.1 AVL Trees: Single Rotation  Fix LL and RR cases

11 4.5.1 AVL Trees: Single Rotation  Example: 3 2 1 4 5 6 7  construct binary search tree without height balanced restriction  depth of tree = 4 i.e. LL case

12 4.5.1 AVL Trees: Single Rotation  Construct AVL tree (height balanced)

13 4.5.1 AVL Trees: Single Rotation Insert 4, 5 Insert 6

14 4.5.1 AVL Trees: Single Rotation Insert 7

15 4.5.2 AVL Trees: Double Rotation Single rotation fails to fix cases 2 and 3 (i.e. LR and RL cases) n n  n+1 n

16 4.5.2 AVL Trees: Double Rotation Double rotation is used case 2 (LR case) n+1 nn

17 4.5.2 AVL Trees: Double Rotation case 3 (RL case)

18 4.5.2 AVL Trees: Double Rotation Example: Double Rotation

19 4.5.2 AVL Trees: Double Rotation Insert 14 Double Rotation

20 4.5.2 AVL Trees: Double Rotation Insert 13 Single Rotation

21 4.5.2 AVL Trees: Double Rotation Insert 12, 11, 10 Single Rotation

22 4.5.2 AVL Trees: Double Rotation Insert 8 and 9

23 4.5.2 AVL Trees: Double Rotation Result

24 4.5.3 AVL Trees: Implementation After insertion, if the height of the node does not change => done Otherwise, if imbalance appears, then determine the appropriate action: single or double rotation

25 4.5.3 AVL Trees: Implementation struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; AvlTree MakeEmpty( AvlTree T ); Position Find( ElementType X, AvlTree T ); Position FindMin( AvlTree T ); Position FindMax( AvlTree T ); AvlTree Insert( ElementType X, AvlTree T ); AvlTree Delete( ElementType X, AvlTree T ); ElementType Retrieve( Position P );

26 4.5.3 AVL Trees: Implementation struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; };

27 4.5.3 AVL Trees: Implementation AvlTree MakeEmpty( AvlTree T ) { if( T != NULL ) { MakeEmpty( T->Left ); MakeEmpty( T->Right ); free( T ); } return NULL; }

28 4.5.3 AVL Trees: Implementation static int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; }

29 4.5.3 AVL Trees: Implementation static Position SingleRotateWithLeft( Position K2 ) { Position K1; K1 = K2  Left; K2  Left = K1  Right; K1  Right = K2; K2  Height = Max( Height( K2  Left ), Height(K2  Right ) ) + 1; K1  Height = Max( Height( K1  Left ), K2  Height ) + 1; return K1; /* New root */ }

30 4.5.3 AVL Trees: Implementation static Position SingleRotateWithRight( Position K1 ) { Position K2; K2 = K1  Right; K1  Right = K2  Left; K2  Left = K1; K1  Height = Max( Height( K1  Left ), Height( K1  Right ) ) + 1; K2  Height = Max( Height( K2  Right ), K1  Height ) + 1; return K2; /* New root */ }

31 4.5.3 AVL Trees: Implementation static Position DoubleRotateWithLeft( Position K3 ) { /* Rotate between K1 and K2 */ K3  Left = SingleRotateWithRight( K3  Left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); }

32 4.5.3 AVL Trees: Implementation static Position DoubleRotateWithRight( Position K1) { /* Rotate between K3 and K2 */ K1  Right = SingleRotateWithLeft( K1  Right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); }

33 4.5.3 AVL Trees: Implementation AvlTree Insert( ElementType X, AvlTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); if ( T == NULL ) FatalError( "Out of space!!!" ); else { T  Element = X; T  Height = 0; T  Left = T  Right = NULL; } else

34 4.5.3 AVL Trees: Implementation if ( X < T  Element ) { T  Left = Insert( X, T  Left ) if ( Height( T  Left ) - Height( T  Right ) == 2 ) if ( X < T  Left  Element ) T = SingleRotateWithLeft( T ) else T = DoubleRotateWithLeft( T ); } else

35 4.5.3 AVL Trees: Implementation if ( X > T  Element ) { T  Right = Insert( X, T  Right ); if( Height( T  Right ) - Height( T  Left ) == 2 ) if( X > T  Right  Element ) T = SingleRotateWithRight( T ); elseT = DoubleRotateWithRight( T ); } /* Else X is in the tree already; we'll do nothing */ T  Height = Max( Height( T  Left ), Height( T  Right ) ) + 1; return T; }

36 4.6 B-Trees A B-tree of order M is a multiway tree of order M with the following structural properties. -The root is either a leaf or has at least 2 subtrees. -All nonroot nodes have at least M/2 branches. -All leaves are at the same level.

37 4.6 B-Trees The first constraint ensures that the tree branches out at the root. The second constraint ensures that each node of the tree is at least half full. The third constraint keeps the tree nearly balanced.

38 4.6 B-Trees Example: B-tree of order 4

39 4.6 B-Trees A B-tree of order 3 (known as a 2-3 tree)

40 4.6 B-Trees Insert 1 node with key = 8

41 4.6 B-Trees Insert 1 node with key = 1

42 4.6 B-Trees Insert 1 node with key = 19 Problem

43 4.6 B-Trees Insert 1 node with key = 19

44 4.6 B-Trees Insert 1 node with key = 28 Create a leaf with 4 children split into 2 leaves of 2 children Create a leaf with 4 children split into 2 leaves of 2 children

45 4.6 B-Trees Insert 1 node with key = 28 (cont) 4 nodes under root

46 4.6 B-Trees Insert 1 node with key = 28 (cont) Split the root into 2 nodes

47 4.6 B-Trees use of B-tree in database systems number of disk access is O(log M N) memory access time vs disk access time


Download ppt "4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ."

Similar presentations


Ads by Google