//See Programming Exercise 9 for the definition of the class //to define AVL trees as ADT template class AVLNode { public: elemType info; int bfactor; // balance factor AVLNode *llink; AVLNode * rlink; }; template void rotateToLeft(AVLNode* &root) { AVLNode *p; //pointer to the root of the //right subtree of root if(root == NULL) cerr<<"Error in the tree."<rlink == NULL) cerr<<"Error in the tree:" <<" No right subtree to rotate."<rlink; root->rlink = p->llink; //the left subtree of p //becomes the right subtree of root p->llink = root; root = p; //make p the new root node } }//end rotateLeft template void rotateToRight(AVLNode* &root) { AVLNode *p; //pointer to the root of //the left subtree of root if(root == NULL) cerr<<"Error in the tree."<llink == NULL) cerr<<"Error in the tree:" <<" No left subtree to rotate."<llink; root->llink = p->rlink; //the right subtree of p //becomes the left subtree of root p->rlink = root; root = p; //make p the new root node } }//end rotateRight template void balanceFromLeft(AVLNode* &root) { AVLNode *p; AVLNode *w; p = root->llink; //p points to the left subtree of root switch(p->bfactor) { case -1: root->bfactor = 0; p->bfactor = 0; rotateToRight(root); break; case 0: cerr<<"Error: Cannot balance from the left."<rlink; switch(w->bfactor) //adjust the balance factors { case -1: root->bfactor = 1; p->bfactor = 0; break; case 0: root->bfactor = 0; p->bfactor = 0; break; case 1: root->bfactor = 0; p->bfactor = -1; }//end switch w->bfactor = 0; rotateToLeft(p); root->llink = p; rotateToRight(root); }//end switch; }//end balanceFromLeft template void balanceFromRight(AVLNode* &root) { AVLNode *p; AVLNode *w; p = root->rlink; //p points to the right subtree of root switch(p->bfactor) { case -1: w = p->llink; switch(w->bfactor) //adjust the balance factors { case -1: root->bfactor = 0; p->bfactor = 1; break; case 0: root->bfactor = 0; p->bfactor = 0; break; case 1: root->bfactor = -1; p->bfactor = 0; }//end switch w->bfactor = 0; rotateToRight(p); root->rlink = p; rotateToLeft(root); break; case 0: cerr<<"Error: Cannot balance from the right."<bfactor = 0; p->bfactor = 0; rotateToLeft(root); }//end switch; }//end balanceFromRight template void insertIntoAVL(AVLNode* &root, AVLNode *newNode, bool& isTaller) { if(root == NULL) { root = newNode; isTaller = true; } else if(root->info == newNode->info) cerr<<"No duplicates are allowed."<info > newNode->info) //newItem goes in //the left subtree { insertIntoAVL(root->llink, newNode, isTaller); if(isTaller) //after insertion, the //subtree grew in height switch(root->bfactor) { case -1: balanceFromLeft(root); isTaller = false; break; case 0: root->bfactor = -1; isTaller = true; break; case 1: root->bfactor = 0; isTaller = false; }//end switch }//end if else { insertIntoAVL(root->rlink, newNode, isTaller); if(isTaller) //after insertion, the //subtree grew in height switch(root->bfactor) { case -1: root->bfactor = 0; isTaller = false; break; case 0: root->bfactor = 1; isTaller = true; break; case 1: balanceFromRight(root); isTaller = false; }//end switch }//end else }//end insertIntoAVL template void insert(const elemType &newItem) { bool isTaller = false; AVLNode *newNode; newNode = new AVLNode; newNode->info = newItem; newNode->bfactor = 0; newNode->llink = NULL; newNode->rlink = NULL; insertIntoAVL(root, newNode, isTaller); }