200 likes | 309 Views
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
E N D
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 */ }
50 50 20 40 40 30 30 10 10 60 20 Case 1: Right is higher • Left rotation is performed • Add value 60
40 20 30 50 10 60 Case 1: Right is higher • Resulting Tree after left Rotation
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 */ }
50 20 40 30 10 35 60 25 Case 2:Left Subtree is higher
40 20 50 50 30 10 10 22 22 35 35 60 60 25 25 40 30 20 Case 2:Left Subtree is higher • Adding node 22
30 40 20 50 10 22 35 60 25 Case 2:Left Subtree is higher • AVL Balanced Tree
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.