#embrace <cstdio>
#embrace <cstdlib>
#embrace <iostream>
utilizing namespace std;
struct splay {
int key;
splay* lchild;
splay* rchild;
};
class SplayTree {
public:
SplayTree() {}
splay* RR_Rotate(splay* k2)
{
splay* k1 = k2->lchild;
k2->lchild = k1->rchild;
k1->rchild = k2;
return k1;
}
splay* LL_Rotate(splay* k2)
{
splay* k1 = k2->rchild;
k2->rchild = k1->lchild;
k1->lchild = k2;
return k1;
}
splay* Splay(int key, splay* root)
{
if (!root)
return NULL;
splay header;
header.lchild = header.rchild = NULL;
splay* LeftTreeMax = &header;
splay* RightTreeMin = &header;
whereas (1) {
if (key < root->key) {
if (!root->lchild)
break;
if (key < root->lchild->key) {
root = RR_Rotate(root);
if (!root->lchild)
break;
}
RightTreeMin->lchild = root;
RightTreeMin = RightTreeMin->lchild;
root = root->lchild;
RightTreeMin->lchild = NULL;
}
else if (key > root->key) {
if (!root->rchild)
break;
if (key > root->rchild->key) {
root = LL_Rotate(root);
if (!root->rchild)
break;
}
LeftTreeMax->rchild = root;
LeftTreeMax = LeftTreeMax->rchild;
root = root->rchild;
LeftTreeMax->rchild = NULL;
}
else
break;
}
LeftTreeMax->rchild = root->lchild;
RightTreeMin->lchild = root->rchild;
root->lchild = header.rchild;
root->rchild = header.lchild;
return root;
}
splay* New_Node(int key)
{
splay* p_node = new splay;
if (!p_node) {
fprintf(stderr, "Out of reminiscence certain!n");
exit(1);
}
p_node->key = key;
p_node->lchild = p_node->rchild = NULL;
return p_node;
}
splay* Insert(int key, splay* root)
{
static splay* p_node = NULL;
if (!p_node)
p_node = New_Node(key);
else
p_node->key = key;
if (!root) {
root = p_node;
p_node = NULL;
return root;
}
root = Splay(key, root);
if (key < root->key) {
p_node->lchild = root->lchild;
p_node->rchild = root;
root->lchild = NULL;
root = p_node;
}
else if (key > root->key) {
p_node->rchild = root->rchild;
p_node->lchild = root;
root->rchild = NULL;
root = p_node;
}
else
return root;
p_node = NULL;
return root;
}
splay* Delete(int key, splay* root)
{
splay* temp;
if (!root)
return NULL;
root = Splay(key, root);
if (key != root->key)
return root;
else {
if (!root->lchild) {
temp = root;
root = root->rchild;
}
else {
temp = root;
root = Splay(key, root->lchild);
root->rchild = temp->rchild;
}
free(temp);
return root;
}
}
splay* Search(int key, splay* root)
{
return Splay(key, root);
}
void InOrder(splay* root)
{
if (root) proper youngster: "
<< root->rchild->key;
cout << "n";
InOrder(root->rchild);
}
};
int essential()
{
SplayTree st;
int vector[10]
= { 30, 28, 26, 24, 22, 20, 18, 16, 14, 12 };
splay* root;
root = NULL;
const int size = 10;
int i;
for (i = 0; i < size; i++)
root = st.Insert(vector[i], root);
cout << "nInOrder: n";
st.InOrder(root);
int enter, alternative;
whereas (1) {
cout << "nSplay Tree Operations GFG n";
cout << "1. Insert " << endl;
cout << "2. Delete" << endl;
cout << "3. Search" << endl;
cout << "4. Exit" << endl;
cout << "Enter your alternative: ";
cin >> alternative;
swap (alternative) {
case 1:
cout << "Enter worth to be insert: ";
cin >> enter;
root = st.Insert(enter, root);
cout << "nAfter Insert: " << enter << endl;
st.InOrder(root);
break;
case 2:
cout << "Enter worth to be delete: ";
cin >> enter;
root = st.Delete(enter, root);
cout << "nAfter Delete: " << enter << endl;
st.InOrder(root);
break;
case 3:
cout << "Enter worth to be search: ";
cin >> enter;
root = st.Search(enter, root);
cout << "nAfter Search " << enter << endl;
st.InOrder(root);
break;
case 4:
exit(1);
default:
cout << "nInvalid Enter sort! n";
}
}
cout << "n";
return 0;
}