C 如何在btree中插入结构

C 如何在btree中插入结构,c,C,我有一个可以在中插入值的B树,如何修改它以插入结构(如图所示)而不是键;我必须使用卷号,并且必须从结构节点访问结构名称。我怎么做 struct name { char first_name[16]; char last_name[16]; int roll_number; }; 其中name是一个结构数组。到目前为止,我已经编写了插入结构元素的代码,但我无法继续下去;请帮帮我 #define M 3; struct node { int n; /* n <

我有一个可以在中插入值的B树,如何修改它以插入结构(如图所示)而不是键;我必须使用卷号,并且必须从结构节点访问结构名称。我怎么做

struct name
{
    char first_name[16];
    char last_name[16];
    int roll_number;
};
其中name是一个结构数组。到目前为止,我已经编写了插入结构元素的代码,但我无法继续下去;请帮帮我

#define M 3;
struct node
{
    int n; /* n < M No. of keys in node will always less than order of Btree */
    struct node *p[M]; /* (n+1 pointers will be in use) */
    int key[M-1]; /*array of names*/
} *root=NULL;

enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys };
void insert(int key);
void display(struct node *root,int);
enum KeyStatus ins(struct node *r, int x, int* y, struct node** u);
int searchPos(int x,int *key_arr, int n);

main()
{
    int choice,key;
    printf("Creation of B tree for node %d\n",M);
    while(1) {
            printf("1.Insert\n");
            printf("2.Quit\n");
            printf("Enter your choice : ");
            scanf("%d",&choice);
            switch(choice) {
                case 1:
                    printf("Enter the value : ");
                    scanf("%d",key);
                    insert(key);
                    break;
                case 2:
                    exit(1);
                default:
                    printf("Wrong choice\n");
                    break;
            }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

void insert(struct classifier *)
{
    struct node *newnode;
    int upKey;
    enum KeyStatus value;
    value = ins(root, key, &upKey, &newnode);
    if (value == Duplicate)
        printf("Key already available\n");
    if (value == InsertIt) {
        struct node *uproot = root;
        root=malloc(sizeof(struct node));
        root->n = 1;
        root->keys[0] = upKey;
        root->p[0] = uproot;
        root->p[1] = newnode;
    }/*End of if */
}/*End of insert()*/


enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node **newnode)
{
    struct node *newPtr, *lastPtr;
    int pos, i, n,splitPos;
    int newKey, lastKey;
    int check = 1;
    enum KeyStatus value;
    if (ptr == NULL) {
        *newnode = NULL;
        *upKey = key;
        return InsertIt;
    }
    n = ptr->n;
    pos = searchPos(key, ptr->keys, n);
    if (pos < n && key == ptr->keys[pos])
        return Duplicate;
        value = ins(ptr->p[pos], key, &newKey, &newPtr);
    if (value != InsertIt)
        return value;
    /*If keys in node is less than M-1 where M is order of B tree*/
        if (n < M - 1) {
            pos = searchPos(newKey, ptr->keys, n);
            /*Shifting the key and pointer right for inserting the new key*/
            for (i=n; i>pos; i--) {
                    ptr->keys[i] = ptr->keys[i-1];
                    ptr->p[i+1] = ptr->p[i];
            }
            /*Key is inserted at exact location*/
            ptr->keys[pos] = newKey;
            ptr->p[pos+1] = newPtr;
            ++ptr->n; /*incrementing the number of keys in node*/
            return Success;
        }/*End of if */
        /*If keys in nodes are maximum and position of node to be inserted is last*/
        if (pos == M - 1) {
            lastKey = newKey;
            lastPtr = newPtr;
        } else /*If keys in node are maximum and position of node to be inserted is not last*/
        {
            lastKey = ptr->keys[M-2];
            lastPtr = ptr->p[M-1];
            for (i=M-2; i>pos; i--) {
                ptr->keys[i] = ptr->keys[i-1];
                ptr->p[i+1] = ptr->p[i];
            }
            ptr->keys[pos] = newKey;
            ptr->p[pos+1] = newPtr;
    }
        splitPos = (M - 1)/2;
        (*upKey) = ptr->keys[splitPos];
        (*newnode)=malloc(sizeof(struct node));/*Right node after split*/
        ptr->n = splitPos; /*No. of keys for left splitted node*/
        (*newnode)->n = M-1-splitPos;/*No. of keys for right splitted node*/
        for (i=0; i < (*newnode)->n; i++) {
            (*newnode)->p[i] = ptr->p[i + splitPos + 1];
            if(i < (*newnode)->n - 1)
                (*newnode)->keys[i] = ptr->keys[i + splitPos + 1];
            else
                (*newnode)->keys[i] = lastKey;
        }
        (*newnode)->p[(*newnode)->n] = lastPtr;
        return InsertIt;
    }/*End of ins()*/

void search(int key)
{
    int pos, i, n;
    struct node *ptr = root;
    printf("Search path:\n");
    while (ptr) {
        n = ptr->n;
        for (i=0; i < ptr->n; i++)
            printf(" %d",ptr->keys[i]);
        printf("\n");
        pos = searchPos(key, ptr->keys, n);
        if (pos < n && key == ptr->keys[pos]) {
            printf("Key %d found in position %d of last dispalyed node\n",key,i);
            return;
        }
        ptr = ptr->p[pos];
    }
    printf("Key %d is not available\n",key);
}/*End of search()*/


int searchPos(int key, int *key_arr, int n)
{
    int pos=0;
    while (pos < n && key > key_arr[pos])
        pos++;
    return pos;
}/*End of searchPos()*/
#定义m3;
结构节点
{
int n;/*nn=1;
根->键[0]=向上键;
根->p[0]=连根拔起;
root->p[1]=newnode;
}/*if结束*/
}/*插入结束()*/
枚举键状态ins(结构节点*ptr,int键,int*upKey,结构节点**newnode)
{
结构节点*newPtr,*lastpr;
int pos,i,n,splitPos;
int newKey,lastKey;
整数检查=1;
枚举键状态值;
如果(ptr==NULL){
*newnode=NULL;
*向上键=键;
返回插入;
}
n=ptr->n;
pos=搜索pos(键,ptr->键,n);
如果(poskeys[pos])
返回副本;
值=ins(ptr->p[pos],键和新键和新ptr);
如果(值!=插入)
返回值;
/*如果节点中的键小于M-1,其中M是B树的顺序*/
if(nkey,n);
/*向右移动键和指针以插入新键*/
对于(i=n;i>pos;i--){
ptr->键[i]=ptr->键[i-1];
ptr->p[i+1]=ptr->p[i];
}
/*钥匙插入到准确的位置*/
ptr->键[pos]=新键;
ptr->p[pos+1]=新ptr;
++ptr->n;/*增加节点中的键数*/
回归成功;
}/*if结束*/
/*如果节点中的关键点是最大值,并且要插入的节点的位置是最后一个*/
如果(pos==M-1){
lastKey=newKey;
lastPtr=新PTR;
}else/*如果节点中的关键点是最大值,并且要插入的节点位置不是最后一个*/
{
lastKey=ptr->keys[M-2];
lastPtr=ptr->p[M-1];
对于(i=M-2;i>pos;i--){
ptr->键[i]=ptr->键[i-1];
ptr->p[i+1]=ptr->p[i];
}
ptr->键[pos]=新键;
ptr->p[pos+1]=新ptr;
}
splitPos=(M-1)/2;
(*向上键)=ptr->键[splitPos];
(*newnode)=malloc(sizeof(struct node));/*拆分后的右节点*/
ptr->n=splitPos;/*左分割节点的键数*/
(*newnode)->n=M-1-splitPos;/*右拆分节点的键数*/
对于(i=0;i<(*newnode)->n;i++){
(*newnode)->p[i]=ptr->p[i+splitPos+1];
如果(i<(*newnode)->n-1)
(*newnode)->keys[i]=ptr->keys[i+splitPos+1];
其他的
(*newnode)->keys[i]=lastKey;
}
(*newnode)->p[(*newnode)->n]=lastPtr;
返回插入;
}/*结束ins()*/
无效搜索(int键)
{
int pos,i,n;
结构节点*ptr=root;
printf(“搜索路径:\n”);
while(ptr){
n=ptr->n;
对于(i=0;in;i++)
printf(“%d”,ptr->key[i]);
printf(“\n”);
pos=搜索pos(键,ptr->键,n);
如果(poskeys[pos]){
printf(“在最后显示的节点的位置%d中找到了键%d,\n”,键,i);
返回;
}
ptr=ptr->p[pos];
}
printf(“密钥%d不可用\n”,密钥);
}/*搜索结束()*/
int searchPos(int key,int*key\u arr,int n)
{
int pos=0;
而(位置key\u arr[pos])
pos++;
返回pos;
}/*searchPos()结束*/

节点的定义中,您应该能够将
结构键
替换为
结构名称
,并使用类似
根->名称->滚动编号
的内容进行比较。可能还有其他清理工作要做,但这是基本思路。

在您对
节点的定义中,您应该能够将
结构键
替换为
结构名称
,并使用类似
根->名称->滚动编号
的内容进行比较。可能还有其他的清理工作要做,但这是最基本的想法