我有一个可以插入值的B树,如何修改它以插入一个结构(如图所示)而不是一个键; 我必须使用卷号,并且必须从结构节点访问结构名称。 我该怎么做?
struct name { char first_name[16]; char last_name[16]; int roll_number; };
其中name是结构数组。 到目前为止,我已经编写了插入结构元素的代码,但我无法继续进行; 请帮帮我。
#define M 3; struct node { int n; /* n 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 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 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 n; i++) { (*newnode)->p[i] = ptr->p[i + splitPos + 1]; if(i 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 n; i++) printf(" %d",ptr->keys[i]); printf("n"); pos = searchPos(key, ptr->keys, n); if (pos keys[pos]) { printf("Key %d found in position %d of last dispalyed noden",key,i); return; } ptr = ptr->p[pos]; } printf("Key %d is not availablen",key); }/*End of search()*/ int searchPos(int key, int *key_arr, int n) { int pos=0; while (pos key_arr[pos]) pos++; return pos; }/*End of searchPos()*/
在你的node
定义中,你应该能够只用struct name
替换struct key
,并使用root->name->roll_number
进行比较。 可能还有其他清理要做,但这是基本想法。
以上就是c/c++开发分享如何在btree中插入结构相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/549730.html