Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Identify and fix the avl tree deletion part in this code C++ CODE: #include using namespace std; /*----------------------------------------------------------------- --------------Course: Data Structure and Algorithm----------------- ------------------------------------------------------------------- */

Identify and fix the avl tree deletion part in this code C++

CODE:

#include using namespace std; /*----------------------------------------------------------------- --------------Course: Data Structure and Algorithm----------------- ------------------------------------------------------------------- */

//Structure for AVL Tree struct AVL { int element; struct AVL *left,*right; int ht;

};

//function for calculating height int height(AVL *T) { int lh,rh; if(T == NULL) return -1; else { lh = height (T->left); rh = height (T->right); if (lh>rh) { return (lh+1); } else { return (rh+1); } } }

//Function for balancing factor int balanceFactor(AVL *T) { int lh,rh; if (T==NULL) { return -1; } else { return height(T->left) - height(T->right); } }

//function for right rotation AVL * rotateright(AVL *x) {

AVL *y; y = x -> left; x -> left = y -> right; y -> right = x; x -> ht = height(x); y -> ht = height(y); return(y); }

//function for left rotation AVL * rotateleft(AVL *x) { AVL *y; y = x -> right; x -> right = y -> left; y -> left = x; x -> ht = height(x); y -> ht = height(y); return(y); }

AVL * getSetRotateLeft(AVL *T) {

T = rotateleft(T); return(T); }

AVL * getSetRotateRight(AVL *T) { T = rotateright(T); return(T); }

AVL * getSetRotateLeftRight(AVL *T) {

T -> left = rotateleft(T->left); T = rotateright(T); return(T); }

AVL * getSetRotateRightLeft(AVL *T) { T -> right = rotateright(T->right); T = rotateleft(T); return(T); }

//function for inserting values in the AVL Tree AVL * insert(AVL *T,int x) {

if(T == NULL){

T = new AVL; T -> element = x; T -> left = NULL; T -> right = NULL; } else if(x > T->element) {

T -> right = insert(T -> right,x); if(balanceFactor(T) == -2) { if( x > T -> right -> element) T = getSetRotateLeft(T); else T = getSetRotateRightLeft(T); } } else if(xelement) { T -> left = insert(T -> left,x); if(balanceFactor(T)==2) if(x < T-> left -> element) T=getSetRotateRight(T); else T=getSetRotateLeftRight(T);

} else { cout<<"Duplicate data discarded"<

T -> ht = height(T);

return(T); }

//function for traversing preorder void preorder(AVL *T) { if( T != NULL) { cout << "Balance factor " << T -> element << " " << balanceFactor(T) << endl; preorder(T->left); preorder(T->right); } }

// finding inorder successor AVL*inorderSucc(AVL*root) { AVL* curr = root; while (curr && curr->left != NULL) { curr = curr->left; } return curr; }

//Function for deletion in AVL tree AVL* deletioninAVL(AVL* root, int key) { if (root == NULL) { return NULL; } else if(key < root->element) { root->left=deletioninAVL(root->left,key); } else if(key > root->element) { root->right = deletioninAVL(root->right,key); } else { if(root->left==NULL) { AVL* temp = root->right; free(root); return temp; } else if(root->right==NULL) { AVL* temp = root->left; free(root); return temp; } AVL* temp = inorderSucc(root->right); root->element = temp->element; root->right = deletioninAVL(root->right, temp->element); } int newbf = balanceFactor(root); if (newbf == 2 && balanceFactor(root -> left) >= 0) return getSetRotateRight(root); else if (newbf == 2 && balanceFactor(root -> left) == -1) { return getSetRotateLeftRight(root); } else if (newbf == -2 && balanceFactor(root -> right) <= -0) return getSetRotateLeft(root); else if (newbf == -2 && balanceFactor(root -> right) == 1) { return getSetRotateRightLeft(root); } return root; }

//main function int main() { AVL *root= new AVL; int x,n,i,choice; while (choice !=0) { cout << "What operation do you want to perform? " <>choice; if (choice ==1) { cout << "Enter no. of elements you want to enter : "; cin >> n; root = NULL; for( i = 0; i < n;i++) { cout << "Enter element of tree: "; cin >> x; root = insert(root,x); } cout <>key; deletioninAVL(root,key); cout<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions