//delete函数运行错误,希望有同学帮忙修改一下
#include<iostream>
#include<ctime>
using namespace std;
struct SplayNode
{
int value;
SplayNode *parent;
SplayNode *left;
SplayNode *right;
};
SplayNode *root;
template<typename T>
T rendom(T begin, T end)
{
return begin + (end-begin)*rand()/(RAND_MAX+1.0);
}
//左旋
void LeftRotate(SplayNode *st)
{
SplayNode *p = st->right;
st->right = p->left;
if(p->left != NULL)
{
p->left->parent = st;
}
p->parent = st->parent;
if(st->parent == NULL)
{
root = p;
}
else
{
if(st == p->parent->left)
st->parent->left = p;
else
st->parent->right = p;
}
p->left = st;
st->parent = p;
}
//右旋
void RightRotate(SplayNode *st)
{
SplayNode *p = st->left;
st->left = p->right;
if(p->right != NULL)
{
p->right->parent = st;
}
p->parent = st->parent;
if(st->parent == NULL)
{
root = p;
}
else
{
if(st == p->parent->left)
st->parent->left = p;
else
st->parent->right = p;
}
p->right = st;
st->parent = p;
}
void splay(SplayNode *st)
{
while(st->parent != NULL)
{
//st是左孩子
if(st->parent->left == st)
{
//如果祖父g为空,右旋
if(st->parent->parent == NULL)
RightRotate(st->parent);
else
{
//如果父亲p是祖父g的左孩子,右旋g和p
if(st->parent == st->parent->parent->left)
{
RightRotate(st->parent->parent);
RightRotate(st->parent);
}
如果p是g的右孩子,先右旋p,在左旋p
else
{
RightRotate(st->parent);
LeftRotate(st->parent);
}
}
}
//否则,是右孩子
else
{
//如果祖父为空,左旋
if(st->parent->parent == NULL)
LeftRotate(st->parent);
else
{
//如果p是g的右孩子,左旋g和右旋p
if(st->parent == st->parent->parent->right)
{
LeftRotate(st->parent->parent);
LeftRotate(st->parent);
}
//如果p是g的右孩子,左旋p,右旋p
else
{
LeftRotate(st->parent);
RightRotate(st->parent);
}
}
}
}
}
void Insert(int key)
{
SplayNode *y = NULL;
SplayNode *x = root;
//找到结点位置x,y记录父节点
while(x != NULL)
{
y = x;
if(key < x->value)
x = x->left;
else
x = x->right;
}
SplayNode *st = new SplayNode;
st->value = key;
st->right = NULL;
st->left = NULL;
st->parent = y;
if(y == NULL)
root = st;
else
{
if(key < y->value )
y->left = st;
else
y->right = st;
}
splay(st);
}
void PrePrint(SplayNode *st)
{
if(st != NULL)
cout<<st->value<<" ";
if(st->left != NULL)
PrePrint(st->left);
if(st->right != NULL)
PrePrint(st->right);
}
void InPrint(SplayNode *st)
{
if(st->left != NULL)
InPrint(st->left);
if(st != NULL)
cout<<st->value<<" ";
if(st->right != NULL)
InPrint(st->right);
}
SplayNode *Minimum(SplayNode *st)
{
while(st->left != NULL)
st = st->left;
return st;
}
SplayNode *Maximum(SplayNode *st)
{
while(st->right != NULL)
st = st->right;
return st;
}
int Search(SplayNode *st, int key)
{
while(st != NULL && key != st->value)
{
if(key < st->value)
st = st->left;
else
st = st->right;
}
if( st )
return st->value;
else
return -1;
}
//Delete运行错误,希望有同学帮忙修改一下
void Delete(int key)
{
SplayNode *st = root;
while(key != st->value && st != NULL)
{
if(key < st->value)
st = st->left;
else
st = st->right;
}
if(st != NULL)
{
SplayNode *pdel;
if(st->right == NULL)
{
pdel = st;
if(st->parent->left == st)
st->parent->left = st->left;
else
st->parent->right = st->right;
st->left->parent = st->parent;
delete pdel;
}
if(st->left == NULL)
{
pdel = st;
if(st->parent->left == st)
st->parent->left = st->left;
else
st->parent->right = st->right;
st->right->parent = st->parent;
delete pdel;
}
if( st->right != NULL && st->left != NULL)
{
SplayNode *rig;
pdel = st;
if(st->right->left == NULL)
{
st = st->right;
}
else
{
rig = st->right;
while(rig->left != NULL)
rig = rig->left;
rig->parent = pdel->parent;
rig->right = pdel->right;
}
delete pdel;
}
}
}
int main()
{
Insert(40);
Insert(90);
Insert(20);
Insert(70);
Insert(50);
Insert(60);
Insert(91);
cout<<"Presplay:"<<endl;
PrePrint(root);
cout<<endl;
cout<<"Insplay:"<<endl;
InPrint(root);
cout<<endl;
for(int i=0; i<10; ++i)
{
Insert(rendom(1,100));
}
cout<<endl;
cout<<"splay:"<<endl;
PrePrint(root);
cout<<endl;
cout<<"Max : "<<Maximum(root)->value<<endl;
cout<<"Min : "<<Minimum(root)->value<<endl;
/* Delete(20);
cout<<"splay:"<<endl;
PrePrint(root);
cout<<endl;
*/
int key;
while(cin>>key)
{
if(key == -1)
break;
else
{
if(key == Search(root, key))
cout<<"find it !"<<endl;
else
cout<<"not find it !"<<endl;
}
}
return 0;
}