如何在C语言中实现链表中的标记树插入
学生结构如何在C语言中实现链表中的标记树插入,c,C,学生结构 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树结构 typedef struct student {
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树结构
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;
}
插入,在这里我只能插入两级数据,但我认为需要插入更多的旋转或递归函数。我已经尝试了几个小时,但无法解决。在插入中,如果未找到根节点,则创建根节点,创建叶节点,并分配哈希值,在下一次插入时,如果右侧没有插入右节点,则在下一次插入时,如果根的叶数为偶数,则在右侧创建一个节点;如果叶数为奇数,则在右侧创建另一个节点,并将当前根节点的右侧指定给新创建节点的左侧,然后创建叶节点,并将其分配到先前创建的节点的右侧,然后将其分配到根节点的右侧。如果是这样,我应该查看每个插入。是否有其他方法
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));
}
}
你读过维基百科的s页了吗?还要阅读C编译器(例如…)和调试器(例如…)的文档。然后使用一个好的源代码编辑器(例如…)。不要忘记阅读(例如…)您正在调用的每个函数(如
malloc
)的文档。要害怕并意识到malloc
可能会失败(您需要检查)。如果使用compile时包含所有警告和调试信息:gcc-Wall-Wextra-g
。(在所有情况下,要求编译器提供更多警告和调试工具)。另见报告。考虑使用或