Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #define M 5
  4. //#define MaxKlucze 2*M
  5. struct node {
  6. int n; /* n < M No. of keys in node will always less than order of B tree */
  7. int keys[M-1]; /*array of keys*/
  8. struct node *p[M]; /* (n+1 pointers will be in use) */
  9. }*root = NULL;
  10.  
  11. enum KeyStatus { Duplicate, SearchFailure, Success, InsertIt, LessKeys };
  12.  
  13. void insert(int key);
  14. void display(struct node *root, int);
  15. void DelNode(int x);
  16. void search(int x);
  17. enum KeyStatus ins(struct node *r, int x, int* y, struct node** u);
  18. int searchPos(int x, int *key_arr, int n);
  19. enum KeyStatus del(struct node *r, int x);
  20.  
  21. void insert(int key)
  22. {
  23. struct node *newnode;
  24. int upKey;
  25. enum KeyStatus value;
  26. value = ins(root, key, &upKey, &newnode);
  27. if (value == Duplicate)
  28. printf("Key already available\n");
  29. if (value == InsertIt)
  30. {
  31. struct node *uproot = root;
  32. root = (node*)malloc(sizeof(struct node));
  33. root->n = 1;
  34. root->keys[0] = upKey;
  35. root->p[0] = uproot;
  36. root->p[1] = newnode;
  37. }/*End of if */
  38. }/*End of insert()*/
  39.  
  40. enum KeyStatus ins(struct node *ptr, int key, int *upKey, struct node **newnode)
  41. {
  42. struct node *newPtr, *lastPtr;
  43. int pos, i, n, splitPos;
  44. int newKey, lastKey;
  45. enum KeyStatus value;
  46. if (ptr == NULL)
  47. {
  48. *newnode = NULL;
  49. *upKey = key;
  50. return InsertIt;
  51. }
  52. n = ptr->n;
  53. pos = searchPos(key, ptr->keys, n);//wyszukiwanie miejsca do wstawienia
  54. if (pos < n && key == ptr->keys[pos])//sprawdzenie czy jest duplikat
  55. return Duplicate;
  56. value = ins(ptr->p[pos], key, &newKey, &newPtr);
  57. if (value != InsertIt)
  58. return value;
  59. /*If keys in node is less than M-1 where M is order of B tree*/
  60. if (n < M - 1)
  61. {
  62. pos = searchPos(newKey, ptr->keys, n);
  63. /*Shifting the key and pointer right for inserting the new key*/
  64. for (i = n; i>pos; i--)
  65. {
  66. ptr->keys[i] = ptr->keys[i - 1];
  67. ptr->p[i + 1] = ptr->p[i];
  68. }
  69. /*Key is inserted at exact location*/
  70. ptr->keys[pos] = newKey;
  71. ptr->p[pos + 1] = newPtr;
  72. ++ptr->n; /*incrementing the number of keys in node*/
  73. return Success;
  74. }/*End of if */
  75. /*If keys in nodes are maximum and position of node to be inserted is last*/
  76. if (pos == M - 1)
  77. {
  78. lastKey = newKey;
  79. lastPtr = newPtr;
  80. }
  81. else /*If keys in node are maximum and position of node to be inserted is not last*/
  82. {
  83. lastKey = ptr->keys[M - 2];
  84. lastPtr = ptr->p[M - 1];
  85. for (i = M - 2; i>pos; i--)
  86. {
  87. ptr->keys[i] = ptr->keys[i - 1];
  88. ptr->p[i + 1] = ptr->p[i];
  89. }
  90. ptr->keys[pos] = newKey;
  91. ptr->p[pos + 1] = newPtr;
  92. }
  93. splitPos = (M - 1) / 2;
  94. (*upKey) = ptr->keys[splitPos];
  95.  
  96. (*newnode) = (node*)malloc(sizeof(struct node));/*Right node after split*/
  97. ptr->n = splitPos; /*No. of keys for left splitted node*/
  98. (*newnode)->n = M - 1 - splitPos;/*No. of keys for right splitted node*/
  99. for (i = 0; i < (*newnode)->n; i++)
  100. {
  101. (*newnode)->p[i] = ptr->p[i + splitPos + 1];
  102. if (i < (*newnode)->n - 1)
  103. (*newnode)->keys[i] = ptr->keys[i + splitPos + 1];
  104. else
  105. (*newnode)->keys[i] = lastKey;
  106. }
  107. (*newnode)->p[(*newnode)->n] = lastPtr;
  108. return InsertIt;
  109. }/*End of ins()*/
  110.  
  111. void display(struct node *ptr, int blanks)
  112. {
  113. if (ptr)
  114. {
  115. int i;
  116. for (i = 1; i <= blanks; i++)
  117. printf(" ");
  118. for (i = 0; i < ptr->n; i++)
  119. printf("%d ", ptr->keys[i]);
  120. printf("\n");
  121. for (i = 0; i <= ptr->n; i++)
  122. display(ptr->p[i], blanks + 10);
  123. }/*End of if*/
  124. }/*End of display()*/
  125.  
  126. void search(int key)
  127. {
  128. int pos, i, n;
  129. struct node *ptr = root;
  130. printf("Search path:\n");
  131. while (ptr)
  132. {
  133. n = ptr->n;
  134. for (i = 0; i < ptr->n; i++)
  135. printf(" %d", ptr->keys[i]);
  136. printf("\n");
  137. pos = searchPos(key, ptr->keys, n);
  138. if (pos < n && key == ptr->keys[pos])
  139. {
  140. printf("Key %d found in position %d of last dispalyed node\n", key, i);
  141. return;
  142. }
  143. ptr = ptr->p[pos];
  144. }
  145. printf("Key %d is not available\n", key);
  146. }/*End of search()*/
  147.  
  148. int searchPos(int key, int *key_arr, int n)
  149. {
  150. int pos = 0;
  151. while (pos < n && key > key_arr[pos])
  152. pos++;
  153. return pos;
  154. }/*End of searchPos()*/
  155.  
  156. void DelNode(int key)
  157. {
  158. struct node *uproot;
  159. enum KeyStatus value;
  160. value = del(root, key);
  161. switch (value)
  162. {
  163. case SearchFailure:
  164. printf("Key %d is not available\n", key);
  165. break;
  166. case LessKeys:
  167. uproot = root;
  168. root = root->p[0];
  169. free(uproot);
  170. break;
  171. }/*End of switch*/
  172. }/*End of delnode()*/
  173.  
  174. enum KeyStatus del(struct node *ptr, int key)
  175. {
  176. int pos, i, pivot, n, min;
  177. int *key_arr;
  178. enum KeyStatus value;
  179. struct node **p, *lptr, *rptr;
  180.  
  181. if (ptr == NULL)
  182. return SearchFailure;
  183. /*Assigns values of node*/
  184. n = ptr->n;
  185. key_arr = ptr->keys;
  186. p = ptr->p;
  187. min = (M - 1) / 2;/*Minimum number of keys*/
  188.  
  189. pos = searchPos(key, key_arr, n);
  190. if (p[0] == NULL)
  191. {
  192. if (pos == n || key < key_arr[pos])
  193. return SearchFailure;
  194. /*Shift keys and pointers left*/
  195. for (i = pos + 1; i < n; i++)
  196. {
  197. key_arr[i - 1] = key_arr[i];
  198. p[i] = p[i + 1];
  199. }
  200. return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys;
  201. }/*End of if */
  202.  
  203. if (pos < n && key == key_arr[pos])
  204. {
  205. struct node *qp = p[pos], *qp1;
  206. int nkey;
  207. while (1)
  208. {
  209. nkey = qp->n;
  210. qp1 = qp->p[nkey];
  211. if (qp1 == NULL)
  212. break;
  213. qp = qp1;
  214. }/*End of while*/
  215. key_arr[pos] = qp->keys[nkey - 1];
  216. qp->keys[nkey - 1] = key;
  217. }/*End of if */
  218. value = del(p[pos], key);
  219. if (value != LessKeys)
  220. return value;
  221.  
  222. if (pos > 0 && p[pos - 1]->n > min)
  223. {
  224. pivot = pos - 1; /*pivot for left and right node*/
  225. lptr = p[pivot];
  226. rptr = p[pos];
  227. /*Assigns values for right node*/
  228. rptr->p[rptr->n + 1] = rptr->p[rptr->n];
  229. for (i = rptr->n; i>0; i--)
  230. {
  231. rptr->keys[i] = rptr->keys[i - 1];
  232. rptr->p[i] = rptr->p[i - 1];
  233. }
  234. rptr->n++;
  235. rptr->keys[0] = key_arr[pivot];
  236. rptr->p[0] = lptr->p[lptr->n];
  237. key_arr[pivot] = lptr->keys[--lptr->n];
  238. return Success;
  239. }/*End of if */
  240. if (pos<n && p[pos + 1]->n > min)
  241. {
  242. pivot = pos; /*pivot for left and right node*/
  243. lptr = p[pivot];
  244. rptr = p[pivot + 1];
  245. /*Assigns values for left node*/
  246. lptr->keys[lptr->n] = key_arr[pivot];
  247. lptr->p[lptr->n + 1] = rptr->p[0];
  248. key_arr[pivot] = rptr->keys[0];
  249. lptr->n++;
  250. rptr->n--;
  251. for (i = 0; i < rptr->n; i++)
  252. {
  253. rptr->keys[i] = rptr->keys[i + 1];
  254. rptr->p[i] = rptr->p[i + 1];
  255. }/*End of for*/
  256. rptr->p[rptr->n] = rptr->p[rptr->n + 1];
  257. return Success;
  258. }/*End of if */
  259.  
  260. if (pos == n)
  261. pivot = pos - 1;
  262. else
  263. pivot = pos;
  264.  
  265. lptr = p[pivot];
  266. rptr = p[pivot + 1];
  267. /*merge right node with left node*/
  268. lptr->keys[lptr->n] = key_arr[pivot];
  269. lptr->p[lptr->n + 1] = rptr->p[0];
  270. for (i = 0; i < rptr->n; i++)
  271. {
  272. lptr->keys[lptr->n + 1 + i] = rptr->keys[i];
  273. lptr->p[lptr->n + 2 + i] = rptr->p[i + 1];
  274. }
  275. lptr->n = lptr->n + rptr->n + 1;
  276. free(rptr); /*Remove right node*/
  277. for (i = pos + 1; i < n; i++)
  278. {
  279. key_arr[i - 1] = key_arr[i];
  280. p[i] = p[i + 1];
  281. }
  282. return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys;
  283. }/*End of del()*/
  284.  
  285. int main()
  286. {
  287. FILE *baza = NULL;
  288. int key;
  289. int choice;
  290. baza = fopen("baza.bin", "wb");
  291. printf("Creation of B tree for node %d\n", M);
  292. while (1)
  293. {
  294. printf("1.Insert\n");
  295. printf("2.Delete\n");
  296. printf("3.Search\n");
  297. printf("4.Display\n");
  298. printf("5.Quit\n");
  299. printf("Enter your choice : ");
  300. scanf("%d", &choice);
  301.  
  302. switch (choice)
  303. {
  304. case 1:
  305. printf("Enter the key : ");
  306. scanf("%d", &key);
  307. insert(key);
  308. break;
  309. case 2:
  310. printf("Enter the key : ");
  311. scanf("%d", &key);
  312. DelNode(key);
  313. break;
  314. case 3:
  315. printf("Enter the key : ");
  316. scanf("%d", &key);
  317. search(key);
  318. break;
  319. case 4:
  320. printf("Btree is :\n");
  321. display(root, 0);
  322. break;
  323. case 5:
  324. fclose(baza);
  325. exit(1);
  326. default:
  327. printf("Wrong choice\n");
  328. break;
  329. }/*End of switch*/
  330. }/*End of while*/
  331. return 0;
  332. }/*End of main()*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement