data structures and algorithms

31
DATA STRUCTURES AND ALGORITHMS Lecture Notes 6 Prepared by İnanç TAHRALI

Upload: prescott-adams

Post on 31-Dec-2015

61 views

Category:

Documents


1 download

DESCRIPTION

DATA STRUCTURES AND ALGORITHMS. Lecture Notes 6 Prepared by İnanç TAHRALI. ROAD MAP. TREES Implementation of Trees Tree Traversals Binary Trees The Search Tree ADT-Binary Search Trees AVL (Adelson-Velskii-Landis) Trees. AVL Trees. - PowerPoint PPT Presentation

TRANSCRIPT

DATA STRUCTURES

ANDALGORITHMS

Lecture Notes 6

Prepared by İnanç TAHRALI

2

ROAD MAP TREES

Implementation of Trees Tree Traversals Binary Trees The Search Tree ADT-Binary Search

Trees AVL (Adelson-Velskii-Landis)

Trees

3

AVL Trees

AVL tree is a binary search tree with a balance condition For every node in the tree, height of left and right

subtrees can differ at most 1

Depth of an AVL Tree is O(logN)

Height information is kept in each node structure

4

AVL Trees

An AVL tree with height 9 Left subtree is an AVL tree with height 7 Right subtree is an AVL tree with height 8

5

AVL Trees

Minimum number of nodes S(h) in an AVL tree of height h isS(h) = S(h-1)+S(h-2)+1

For h=0 S(h)=1For h=1 S(h)=2

Function S(h) is closed to Fibonacci numbers !

6

AVL Trees

All tree operations can be performed in O(logN) except possibly insertion

need to update all balancing information for nodes on the path back to root

Inserting a node can violate AVL tree property

Insert 6 will destroy the balance condition at node 8

In this case, a rotation must be done

7

AVL Trees If node that must be balanced is x, there are 4 cases:

1. insertion into the left subtree of the left child of x2. insertion into the right subtree of the left child of x3. insertion into the left subtree of the right child of x4. insertion into the right subtree of the right child of x

case 1-4 mirror image symetries with respect to x insertion occurs on the outside and fixed by a

single rotation case 2-3 mirror image symetries with respect to x

insertion occurs on the inside and fixed by adouble rotation

8

Single Rotation for Case 1

Node k2 violates AVL balance property its left subtree is to level deeper then right

subtree dashed lines in the middle of the diagram mark

the levels

9

Single Rotation for Case 4

Similar to single rotation for case 1

10

Example 1:

AVL property is destroyed by insertion of 6 Fixed by a single rotation

11

Example 2: Start with an initial empty AVL tree Insert items 3,2,1 and then 4 through 7 in sequential First problem occurs when inserting item 1

Perform a single rotation between root and its left child

12

Example 2:

Inserting 4 is not problem To insert 5 a single rotation is performed

13

Example 2:

Next we insert 6 Causes a balance problem at root

14

Example 2:

Next we insert 7 Causes a balance problem

15

Double Rotation for Case 2 Single rotation does not work for case 2 and 3 Subtree Y is too deep and a single rotation

does not make it less deep

16

Double Rotation for Case 2 Double rotation solves the problem

17

Double Rotation for Case 3

18

Example 2: Continue example 2 with inserting 10 through

16 in reverse order, followed by 8 and 9.

19

Example 2: Inserting 16 is easy Inserting 15causes a height imbalance at node 7 Right-left double rotation is performed

20

Example 2: Inserting 14 requires double rotation

21

Example 2: Inserting 13 occurs an imbalance at the root 13 is not between 4 and 7 so a single rotation will work

22

Example 2: Inserting 12 also requires single rotation

23

Example 2: To insert 11 and 10 , single rotations

need to be performed

24

Example 2: Inserting 9 causes the node containing

10 to become unbalanced.

25

Node declaration for AVL treestemplate <class Comparable>class AvlTree;

template <class Comparable>class AvlNode{

Comparable element;AvlNode *left;AvlNode *right;int height;

AvlNode( const Comparable & theElement, AvlNode *lt, AvlNode *rt, int h = 0 ) : element( theElement ), left( lt ), right( rt ), height( h ) { }

friend class AvlTree<Comparable>;

};

26

Interface of AVL treetemplate <class Comparable>class AvlTree{ public:

explicit AvlTree( const Comparable & notFound );AvlTree( const AvlTree & rhs );~AvlTree( );

const Comparable & findMin( ) const;const Comparable & findMax( ) const;const Comparable & find( const Comparable & x ) const;bool isEmpty( ) const;void printTree( ) const;

void makeEmpty( );void insert( const Comparable & x );void remove( const Comparable & x );

const AvlTree & operator=( const AvlTree & rhs );

27

private:AvlNode<Comparable> *root;const Comparable ITEM_NOT_FOUND;

const Comparable & elementAt( AvlNode<Comparable> *t ) const;

void insert( const Comparable & x, AvlNode<Comparable> * & t ) const;

AvlNode<Comparable> * findMin( AvlNode<Comparable> *t ) const; AvlNode<Comparable> * findMax( AvlNode<Comparable> *t ) const; AvlNode<Comparable> * find( const Comparable & x,

AvlNode<Comparable> *t ) const; void makeEmpty( AvlNode<Comparable> * & t ) const; void printTree( AvlNode<Comparable> *t ) const; AvlNode<Comparable> * clone( AvlNode<Comparable> *t ) const;

int height( AvlNode<Comparable> *t ) const; int max( int lhs, int rhs ) const; void rotateWithLeftChild( AvlNode<Comparable> * & k2 ) const; void rotateWithRightChild( AvlNode<Comparable> * & k1 ) const; void doubleWithLeftChild( AvlNode<Comparable> * & k3 ) const; void doubleWithRightChild( AvlNode<Comparable> * & k1 ) const;};

28

/* Return the height of node t or -1 if NULL.

template <class Comparable>int AvlTree<Comparable>::height( AvlNode<Comparable> *t ) const

{return t == NULL ? -1 : t->height;

}

29

/* Internal method to insert into a subtreetemplate <class Comparable>void AvlTree<Comparable>::insert( const Comparable & x,

AvlNode<Comparable> * & t ) const {if( t == NULL )

t = new AvlNode<Comparable>( x, NULL, NULL ); else if( x < t->element ) {

insert( x, t->left );if( height( t->left ) - height( t->right ) == 2 )

if( x < t->left->element ) rotateWithLeftChild( t ); else doubleWithLeftChild( t ); } else if( t->element < x ) { insert( x, t->right ); if( height( t->right ) - height( t->left ) == 2 ) if( t->right->element < x ) rotateWithRightChild( t ); else doubleWithRightChild( t );

}else ;t->height = max(height( t->left),height(t->right ) )+1;

}

30

/* routine rotateWithLeftChildtemplate <class Comparable>void

AvlTree<Comparable>::rotateWithLeftChild( AvlNode<Comparable> * & k2 ) const

{AvlNode<Comparable> *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;k2 = k1;

}

/* routine rotateWithRightChildtemplate <class Comparable>void

AvlTree<Comparable>::rotateWithRightChild( AvlNode<Comparable> * & k1 ) const

{AvlNode<Comparable> *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;k1 = k2;

}

31

/* routine doubleWithLeftChildtemplate <class Comparable>void

AvlTree<Comparable>::doubleWithLeftChild( AvlNode<Comparable> * & k3 ) const

{rotateWithRightChild( k3->left );rotateWithLeftChild( k3 );

}

/* routine doubleWithRightChildtemplate <class Comparable>void

AvlTree<Comparable>::doubleWithRightChild( AvlNode<Comparable> * & k1 ) const

{rotateWithLeftChild( k1->right );rotateWithRightChild( k1 );

}