47
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.

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

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

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.

4.5 AVL Trees

Height of AVL tree is

1.44log(N+2) -0.328 logN

searching complexity O(log N)

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.5 AVL Trees

Violation may occur when an insertion into1. 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)

4.5 AVL Trees

n+1n+1

n

n+1n+1

n

n+1n+1

n+1 n+1

n+1n+1

From: J Beidler, Data structures and algorithms, Springer1997

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.

4.5 AVL Trees

Rotation

- to restore the AVL tree after insertion

- single rotation

- double rotation

4.5 AVL Trees

single rotation

4.5 AVL Trees

Double rotation

4.5.1 AVL Trees: Single Rotation

Fix LL and RR cases

4.5.1 AVL Trees: Single Rotation

Example: 3 2 1 4 5 6 7construct binary search tree without height

balanced restriction depth of tree = 4

i.e. LL casei.e. LL case

4.5.1 AVL Trees: Single Rotation

Construct AVL tree (height balanced)

4.5.1 AVL Trees: Single Rotation

Insert 4, 5Insert 4, 5

Insert 6Insert 6

4.5.1 AVL Trees: Single Rotation

Insert 7Insert 7

4.5.2 AVL Trees: Double Rotation

• Single rotation fails to fix cases 2 and 3 (i.e. LR and RL cases)

nn n+1

n

4.5.2 AVL Trees: Double Rotation

• Double rotation is used

• case 2 (LR case)

n+1n+1

n n

n+1

4.5.2 AVL Trees: Double Rotation

• case 3 (RL case)

4.5.2 AVL Trees: Double Rotation

• Example:

Double Rotation

Double Rotation

4.5.2 AVL Trees: Double Rotation

• Insert 14

Double Rotation

Double Rotation

4.5.2 AVL Trees: Double Rotation

• Insert 13

Single Rotation

Single Rotation

4.5.2 AVL Trees: Double Rotation

• Insert 12, 11, 10

Single Rotation

Single Rotation

4.5.2 AVL Trees: Double Rotation

• Insert 8 and 9

4.5.2 AVL Trees: Double Rotation

• Result

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

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 );

4.5.3 AVL Trees: Implementation

struct AvlNode

{

ElementType Element;

AvlTree Left;

AvlTree Right;

int Height;

};

4.5.3 AVL Trees: Implementation

AvlTree MakeEmpty( AvlTree T )

{ if( T != NULL )

{ MakeEmpty( T->Left );

MakeEmpty( T->Right );

free( T );

}

return NULL;

}

4.5.3 AVL Trees: Implementation

static int Height( Position P )

{

if( P == NULL )

return -1;

else

return P->Height;

}

4.5.3 AVL Trees: Implementation

static Position SingleRotateWithLeft( Position K2 )

{ Position K1;

K1 = K2Left;

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 */ }

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 */ }

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 );

}

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 );

}

4.5.3 AVL Trees: ImplementationAvlTree 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

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

4.5.3 AVL Trees: Implementationif ( 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;

}

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.

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.

4.6 B-Trees

• Example: B-tree of order 4

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

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

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

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

ProblemProblem

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

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

4.6 B-Trees• Insert 1 node with key = 28 (cont)

4 nodes under root4 nodes under root

4.6 B-Trees• Insert 1 node with key = 28 (cont)

Split the root into 2 nodesSplit the root into 2 nodes

4.6 B-Trees• use of B-tree in database systems

• number of disk access is O(logMN)

• memory access time vs disk access time