You are on page 1of 19

AVL Trees Implementation

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 LH: The left subtree of the current node has more levels EH: The left and right subtrees have equal height RH: The right subtree has more levels

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

AVL Trees: Implementation


struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; };

AVL Trees: Implementation


int Height( Position P ) { if( P == NULL ) return -1; else return P->Height; }

AVL Trees: Implementation


AvlTree Insert( ElementType X, AvlTree T ) { if ( T == NULL ) { /* Create and return a one-node tree */ T = new AvlNode; if ( T == NULL ) FatalError( "Out of space!!!" ); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else

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

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 ); else T = 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; }

AVL Trees: Implementation


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

Case 1: Right is higher


Left rotation is performed  Add value 60

20 20 10 10 40 30 30 50 60 50 40

Case 1: Right is higher




Resulting Tree after left Rotation


40

20

50

10

30

60

AVL Trees: Implementation


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

Case 2:Left Subtree is higher


40

20

50

10

30

60

25

35

Case 2:Left Subtree is higher




Adding node 22
40 40 20 50 30 50

10

30

60

20

35

60

25

35

10

25

22

22

Case 2:Left Subtree is higher




AVL Balanced Tree


30

20

40

10

25

35

50

22 60

AVL Trees: Implementation


Position DoubleRotateWithLeft( Position K3 ) { /* Rotate between K1 and K2 */ K3->Left = SingleRotateWithRight( K3->Left ); /* Rotate between K3 and K2 */ return SingleRotateWithLeft( K3 ); Single Right Rotation at K1 } Single Left rotation at K3

AVL Trees: Implementation


Position DoubleRotateWithRight( Position K1) { /* Rotate between K3 and K2 */ K1->Right = SingleRotateWithLeft( K1->Right ); /* Rotate between K1 and K2 */ return SingleRotateWithRight( K1 ); }

Computer Network using BST


  

The BST should store the record on the basis of Computer ID Search can be performed on the basis of computer Id. When the program terminates it should store the whole tree in a text with name BSTree.txt
 

Records should be stored in the following format [Computer Id][ComputerName]

When the program loads, it should check the text if not empty then load the tree with the information in the text file.

Computer Network using BST




 

User can delete or add a Computer on the network at any time. Use dynamic storage for trees Design user friendly GUI fulfilling all the functionalities. Use SDI Application for this assignment.

You might also like