问题描述
学生结构
typedef struct student
{
int rollno;
char *name;
}student_t;
标记树结构
typedef struct markle
{
student_t *data;
int Hash;
int LeafCount;
struct markle *left;
struct markle *right;
}Markle;
创建根节点
Markle *createnode(void *data)
{
Markle *newnode=(Markle *)malloc(sizeof(Markle));
newnode->left=newnode->right=NULL;
newnode->Hash=hash(data);
return newnode;
}
创建叶节点
Markle *createnode_r(void *data)
{
Markle *newnode=(Markle *)malloc(sizeof(Markle));
newnode->left=newnode->right=NULL;
newnode->data=data;
newnode->LeafCount=1;
return newnode;
}
插入,在这里我只能插入两个级别的数据,但是我认为需要插入更多的旋转或一些递归函数。我已经尝试了几个小时,但无法解决。如果没有根节点,请在此处插入找到根节点并创建叶节点并分配哈希值,然后在下一次插入时如果在右侧未插入右节点,则在下一次插入时如果根的叶数为偶数则创建一个节点如果叶子数为奇数,则向右移动,然后创建另一个节点并将当前根节点的权限分配给新创建的节点的左侧,然后创建叶子节点并将其分配给先前创建的节点的右边,并将其分配给根节点的右侧node.if像这样,我应该必须查看每个插入。还有其他方法可以做到吗?
int markle_insert(Markle **root,void *data)
{
if(!(*root)){
(*root)=createnode(data);
(*root)->left=createnode_r(data);
(*root)->LeafCount=(*root)->left->LeafCount;
return 0;
}
if((*root)->right==NULL)
{
Markle *temp=createnode_r(data);
(*root)->right=temp;
(*root)->Hash=(*root)->Hash + hash(data);
(*root)->LeafCount=(*root)->LeafCount+temp->LeafCount;
}
else if((*root)->right!=NULL && (*root)->LeafCount%2==0)
{
Markle *temp=createnode(data);
temp->left=(*root);
temp->Hash=(*root)->Hash;
temp->LeafCount=(*root)->LeafCount;
(*root)=temp;
Markle *temp1=createnode_r(data);
(*root)->right=temp1;
(*root)->Hash=hashr((*root)->Hash + hash(data));
(*root)->LeafCount=(*root)->LeafCount+temp1->LeafCount;
}
else if((*root)->right!=NULL && (*root)->LeafCount%2!=0)
{
Markle *newnode=(Markle *)malloc(sizeof(Markle));
newnode->left=(*root)->right;
Markle *newr=createnode_r(data);
newnode->Hash=hash(data)+hash(newnode->left->data);
newnode->right=newr;
newnode->LeafCount=2;
(*root)->right=newnode;
(*root)->LeafCount=(*root)->LeafCount+1;
(*root)->Hash=hashr((*root)->Hash+hash(data));
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)