

Please use C++ as a Programming language and do the tasks specified per the Guideline above and include comments of your work.
Please make sure that the following test cases are working:
Example 1
For input D13 D60 D76 D12 A17 D98 A94 D70 D3 A23 A42 D45 A100 D50
A99 A22 A87 A4 A90 D88 A71 A20 D39 D83 A97 A56 D28 A9 D43 A19 D5
A11 A54 A73 D54 A9 A24 A58 D6 D80 A72 A47 A82 A12 A75 D77 D84 D86
A60 D64 D70 D70 A73 A71 A40 D94 D27 A63 D47 A42 A44 A27 A100 A6 D84
A19 D65 A75 A55 A63 A39 D99 A50 D98 A98 D100 D93 A91 A81 D59 D56
D29 D11 D45 D47 D55 D85 D7 D70 A13 A55 A25 D35 D65 A48 D55 A45 D29
A35 A15 IN
Output received 1 3 4 9
Output shouldbe 4 6 9 12 13 15 17 19 20 22 23 24 25 27
35 39 40 42 44 45 48 50 58 60 63 71 72 73 75 81 82 87 90 91 97
98
incorrect
Example 2
For input A88 D77 D95 A78 A71 A2 D9 A2 A60 D80 A85 A65 D11 A30 D8
A68 D87 A28 A88 A96 D29 D26 D88 D47 D68 A65 A86 A100 A61 D7 D76 D21
D24 A40 D94 A84 A16 D28 A45 A60 D34 D14 A68 A64 A74 A62 D99 D2 D34
D32 D60 D52 D19 A95 A28 A91 D24 A34 D22 D77 D7 D78 A3 A100 D95 D53
D82 D64 A55 A46 A17 A70 D4 A25 A75 D71 A30 D50 D44 A11 D39 A47 D77
A71 D1 A98 D51 A63 D15 A15 D75 A4 D14 D77 A9 D84 D70 A5 D67 A22
POST
Output shouldbe 3 5 4 15 11 9 22 17 28 25 16 34 40 46 55
47 63 62 61 45 30 71 68 85 74 91 98 100 96 86 65
Example 3
For input A13 D10 D48 D14 D4 D75 A51 D36 A72 D55 D14 D30 D100 A90
D53 D86 D96 A89 D11 A72 A55 A43 A28 D19 D20 A22 D13 A94 A40 A34 D10
A69 D54 D27 D31 A1 D22 D47 D73 D53 A13 D39 D23 A77 D53 A78 D75 D38
A84 A97 D49 D24 A8 A49 A44 D74 D85 A89 A83 A38 A63 A59 A64 D61 D95
D68 D42 D58 A18 D24 A13 A18 D6 A63 A16 D97 A68 A75 D61 D81 D30 A6
D81 D44 A41 A30 A42 A67 A86 D92 D50 D39 D95 D78 A66 D34 A74 D64 A46
A94 IN
Output shouldbe 1 6 8 13 16 18 28 30 38 40 41 42 43 46
49 51 55 59 63 66 67 68 69 72 74 75 77 83 84 86 89 90
94
Example 4
For input A23 D51 A75 A41 A17 A81 D88 A72 D75 A94 A34 D11 A11 A94
D17 D81 D71 A24 D91 A44 D30 D99 A69 A92 D54 D75 A90 A76 D7 D79 A98
D7 A38 D51 D81 D57 A56 D3 D37 D17 A30 A31 D93 D83 A44 A59 A87 A64
A82 D36 A66 D17 A79 D35 A77 A37 A96 D96 A63 A43 A84 D78 A19 A64 A15
A75 D71 D66 D74 A93 D92 D3 D84 D23 D44 D72 A76 D28 D93 A47 A49 A30
A11 D92 A93 A50 A92 A11 A25 A27 D55 A85 A26 A50 D24 D10 A98 A55 D6
A36 IN
Output shouldbe 11 15 19 25 26 27 30 31 34 36 37 38 41
43 47 49 50 55 56 59 63 64 69 75 76 77 79 82 85 87 90 92 93 94
98
EXample 5
For input D23 D87 D47 D36 A55 D39 A4 D26 A11 A93 D38 A44 A98 D1 A98
A95 D39 A24 A64 D22 A32 D51 A9 A63 A35 A74 D95 D51 A50 A56 D12 D4
D31 A74 D80 A8 D12 A81 A29 A91 A69 D80 A68 D100 D92 D25 D69 A75 D73
D29 D5 D25 A9 A30 A59 A66 D75 D95 D60 A26 D56 D69 D94 A21 D17 D25
A17 A77 D58 D13 A81 A1 A47 D94 A24 A50 D76 D1 A51 D83 A86 A5 D48
A57 A28 D15 D46 A14 D21 A53 D85 A15 D6 A29 D99 D43 D29 D47 D55 A86
IN
Output shouldbe 5 8 9 11 14 15 17 24 26 28 30 32 35 44
50 51 53 57 59 63 64 66 68 74 77 81 86 91 93 98
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int key;
Node *left;
Node *right;
int height;
};
int max(int a, int b);
int height(Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b)
{
return (a > b)? a : b;
}
Node* newNode(int key)
{
Node* node = new Node();
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
Node *rightRotate(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
return x;
}
Node *leftRotate(Node *x)
{
Node *y = x->right;
Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
return y;
}
int getBalance(Node *N)
{
if (N == NULL)
return 0;
return height(N->left) -
height(N->right);
}
Node* insert(Node* node, int key)
{
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left =
insert(node->left, key);
else if (key > node->key)
node->right =
insert(node->right, key);
else
return node;
node->height = 1 +
max(height(node->left),
height(node->right));
int balance = getBalance(node);
if (balance > 1 && key <
node->left->key)
return
rightRotate(node);
if (balance < -1 && key >
node->right->key)
return
leftRotate(node);
if (balance > 1 && key >
node->left->key)
{
node->left =
leftRotate(node->left);
return
rightRotate(node);
}
if (balance < -1 && key <
node->right->key)
{
node->right =
rightRotate(node->right);
return
leftRotate(node);
}
return node;
}
Node * minValueNode(Node* node)
{
Node* current = node;
while (current->left != NULL)
current =
current->left;
return current;
}
Node* remove(Node* root, int key)
{
if (root == NULL)
return root;
if ( key < root->key )
root->left =
remove(root->left, key);
else if( key > root->key )
root->right =
remove(root->right, key);
else
{
if( (root->left ==
NULL) ||
(root->right == NULL) )
{
Node *temp = root->left ?
root->left :
root->right;
if (temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else
{
Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = remove(root->right,
temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 +
max(height(root->left),
height(root->right));
int balance = getBalance(root);
if (balance > 1 &&
getBalance(root->left) >= 0)
return
rightRotate(root);
if (balance > 1 &&
getBalance(root->left) < 0)
{
root->left =
leftRotate(root->left);
return
rightRotate(root);
}
if (balance < -1 &&
getBalance(root->right) <= 0)
return
leftRotate(root);
if (balance < -1 &&
getBalance(root->right) > 0)
{
root->right =
rightRotate(root->right);
return
leftRotate(root);
}
return root;
}
void pre_order(Node *root)
{
if(root != NULL)
{
cout <<
root->key << " ";
pre_order(root->left);
pre_order(root->right);
}
}
void in_order(Node *root)
{
if(root != NULL)
{
in_order(root->left);
cout <<
root->key << " ";
in_order(root->right);
}
}
void post_order(Node *root)
{
if(root != NULL)
{
post_order(root->left);
post_order(root->right);
cout <<
root->key << " ";
}
}
int main()
{
string str,word;
getline(cin, str);//get the line from user
Node *root=NULL;//intitializing tree
vector<string> out;
istringstream ss(str);//string stream for
tokenizing
//we split the input into each opearation
do {
ss >> word;
out.push_back(word);
} while (ss);
//we traverse upto operations only either insertion or
deletion
int len=out.size();
for(int i=0;i<len-1;i++){
if(out[i][0]=='A'){ // if the
string first char is 'A' then it is insertion
int
val=0;//here we are calculating value after 'A'
for(int j=1;j<(int)out[i].size();j++){
val=val*10+(out[i][j]-'0');
}
root=insert(root,val);//here inserting into tree
}else if(out[i][0]=='D'){// if
the string first char is 'A' then it is insertion
int
val=0;//here we are calculating value after 'A'
for(int j=1;j<(int)out[i].size();j++){
val=val*10+(out[i][j]-'0');
}
//cout<<"D "<<val<<endl;
root=remove(root,val);//here we are removing from tree
}
}
string ord=out[out.size()-1];//last operation tells
about Traversal pre,in,post order
if(ord=="IN"){
if(root==NULL){
cout<<"EMPTY";
}else{
in_order(root);
}
cout<<endl;
}else if(ord=="PRE"){
if(root==NULL){
cout<<"EMPTY";
}else{
pre_order(root);
}
cout<<endl;
}else if(ord=="POST"){
if(root==NULL){
cout<<"EMPTY";
}else{
post_order(root);
}
cout<<endl;
}
}
Please use C++ as a Programming language and do the tasks specified per the Guideline above...