如题,下面是我写的一些代码,实在改不好,初学C++,希望各位高手帮忙,谢谢
#include
struct tree;
struct node{
friend struct tree;
int leftkey,rightkey;//左右权值
node*left,*center,*right;//结点的左中右指针
node*parent;
int sign;
node()
{
left=right=center=NULL;
parent=NULL;
leftkey=rightkey=0;
sign=0;//标记根结点
}
};
struct tree{
node* root;
node* insert(node* root,int value,int newvalue,node* newnode);//2-3树的插入,在root结点处完成插入,新增权值为value,newnode为分裂后产生的新结点
void output(node* root);//2-3树的显式输出
};
node* tree::insert(node* root,int value,int newvalue,node* newnode)
{
node* newroot=new node();
int sonvalue;
node* sonnode=NULL;
if(root==NULL)//树为空时
{
root=new node();
root->leftkey=value;
root->sign=1;
}
else if(root->left==NULL&&root->right==NULL&&root->center==NULL)//即root为叶结点
{
if(root->rightkey==0)//只有一个权值时
{
if(value>=root->leftkey)root->rightkey=value;//value作为root的右权值
else //value作为root的左权值
{
root->rightkey=root->leftkey;
root->leftkey=value;
}
}/////////////////////////////////////////////////////////////////////////////////////////////////////////
else//root具有两个权值时
{
newnode=new node();
if(value>root->rightkey)//将root的右权值提升
{
newnode->leftkey=value;
newvalue=root->rightkey;
root->rightkey=0;
}
else
{
newnode->leftkey=root->rightkey;
if(valueleftkey)
{
newvalue=root->leftkey;//将root的左权值提升
root->leftkey=value;
root->rightkey=0;
}
else
{
newvalue=value;//提升value
root->rightkey=0;
}
}
if(root->sign)
{
newroot->leftkey=newvalue;
newroot->left=root;
root->sign=0;
newroot->center=newnode;
root=newroot;
root->sign=1;
}
}
}//////////////////////////////////////////////////////////////////////////////////////////////////////////
else if(valueleftkey)//root非叶结点而value小于root左权值,向root左子树插入
insert(root->left,value,sonvalue,sonnode);//递归插入左子树
else if(valuerightkey||root->rightkey==0)
insert(root->center,value,sonvalue,sonnode);//递归插入中子树
else insert(root->right,value,sonvalue,sonnode);//递归插入右子树
if(sonnode!=NULL&&sonvalue!=0)//子结点有提升,为下层调用的递归
if(root->leftkey!=0&&root->rightkey!=0&&root!=NULL)//root已满
{
newnode=new node();
if(sonvalueleftkey)//提升左权值
{
newvalue=root->leftkey;
root->leftkey=sonvalue;
newnode->leftkey=root->rightkey;
root->rightkey=0;
newnode->left=root->center;
newnode->center=root->right;
root->right=NULL;///////////////
root->center=sonnode;
}
else if(sonvaluerightkey)
{
newvalue=sonvalue;
newnode->leftkey=root->rightkey;
root->rightkey=0;
newnode->left=sonnode;
newnode->center=root->right;
root->right=NULL;///////////////
}
else
{
newvalue=root->rightkey;
newnode->leftkey=sonvalue;
root->rightkey=0;
newnode->left=root->right;
root->right=NULL;//////////////
newnode->center=sonnode;
}
if(root->sign)
{
newroot->leftkey=newvalue;
newroot->left=root;
newroot->center=newnode;
root=newroot;
root->sign=1;
}
}
else//root未满
{
if(sonvalueleftkey)
{
root->rightkey=root->leftkey;
root->leftkey=sonvalue;
root->right=root->center;
root->center=sonnode;
}
else
{
root->rightkey=sonvalue;
root->right=sonnode;
}
if(root->sign)
{
newroot->leftkey=newvalue;
newroot->left=root;
newroot->center=newnode;
root=newroot;
root->sign=1;
}
}
return root;
}
--------------------next---------------------
阅读(1114) | 评论(0) | 转发(0) |