Advertisement
Guest User

Untitled

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