Guest User

Untitled

a guest
Oct 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #define true 1
  5. #define false 0
  6. #define log2(a) log((a))/log(2)
  7.  
  8. typedef struct node* nodeptr;
  9.  
  10. typedef struct node
  11. {
  12. int val;
  13. nodeptr leftchild;
  14. nodeptr rightchild;
  15. int bf;
  16. }node;
  17.  
  18. void insert(nodeptr *parent,int *unbalanced,int key);
  19. void leftRotation(nodeptr *pparent,int *unbalanced);//..means if left subtree grows larger then rotate this left subtree
  20. void rightrotation(nodeptr *,int *);//................vice versa
  21. int height(node* node);
  22. void inorder(node *proot);
  23.  
  24. int main(void)
  25. {
  26. node *root,*cur;
  27. root=NULL;
  28. int balancedState=true;
  29. //int num[]={1,2,3,4,5,6,8,9,10,7};//..input series..{8,7,2,9},{8,7,2,9,10},{8,7,2,9,10,11}
  30. int num[]={10,9,8,7,6,5,3,2,1,4,15,14,16,12};
  31. int size=sizeof(num)/sizeof(int);
  32. int i=0;
  33. for(i=0;i<size;i++)
  34. {
  35. printf("\nheight before insertion of %d is %d\n",num[i],height(root)-1);
  36. insert(&root,&balancedState,num[i]);
  37. printf("\n%d is inserted.\nHeight after insertion is: %d",num[i],height(root)-1);
  38.  
  39. printf("\n\n****************************************************");
  40. }
  41.  
  42. printf("\n\nfinal height is: %d",height(root)-1);
  43. printf("\nno of nodes are %d.\nAnd log2(%d) is %f.",size,size,log2(size));
  44. printf("\nSo Height is almost equal to log2(%d))\n",size);
  45. printf("\noutput<inorder traversal of tree: ");
  46. if(root==NULL)
  47. printf("\nnull");
  48. inorder(root);
  49. getch();
  50. //return 0;
  51. }
  52.  
  53. void insert(nodeptr *parent,int *unbalanced,int key)
  54. {
  55. if(!(*parent))
  56. {
  57. //printf("mhere 1");
  58. node *newnode;
  59. newnode=(node *)malloc(sizeof(node));
  60. newnode->leftchild=newnode->rightchild=NULL;
  61. newnode->val=key;
  62. newnode->bf=0;
  63. *unbalanced=true;
  64. *parent=newnode;
  65. //printf("mhere 1");
  66. //return newnode;
  67. }
  68. else if(key<(*parent)->val)
  69. {
  70. //printf("\nm here2");
  71. insert(&(*parent)->leftchild,unbalanced,key);
  72. if(*unbalanced)
  73. {
  74. switch((*parent)->bf)
  75. {
  76. case -1: (*parent)->bf=0;
  77. //printf("\nreset bf");
  78. *unbalanced=false;
  79. break;
  80. case 0: (*parent)->bf=1;
  81. //printf("\nset bf");
  82. break;
  83. case 1: leftRotation(parent,unbalanced);
  84. //printf("\n**LEFT ROTATION DONE**\n");
  85. break;////////////////////////////////////
  86. }
  87. }
  88.  
  89. }
  90. else if(key>(*parent)->val)
  91. {
  92. //printf("m here3");
  93. insert(&(*parent)->rightchild,unbalanced,key);
  94. //printf("m here4");
  95. if(*unbalanced)
  96. {
  97. switch((*parent)->bf)
  98. {
  99. case 1: (*parent)->bf=0;
  100. *unbalanced=false;
  101. break;
  102. case 0: (*parent)->bf=-1;
  103. break;
  104. case -1: //printf("going to rightrOTATE");
  105. rightrotation(parent,unbalanced);
  106. //printf("\n**RIGHT ROTATION DONE**\n");
  107. break;
  108. }
  109. }
  110. else
  111. {
  112. *unbalanced=false;
  113. //printf("the key is already in the tree");
  114. }
  115. }
  116. }
  117.  
  118. void leftRotation(nodeptr *pparent,int *unbalanced)
  119. {
  120. nodeptr child,grandchild;
  121. child=(*pparent)->leftchild;
  122. if(child->bf==1)
  123. {
  124. printf("\nsimple left rotation.");
  125. (*pparent)->leftchild=child->rightchild;
  126. child->rightchild=(*pparent);
  127. (*pparent)->bf=0;
  128. (*pparent)=child;
  129. }
  130. else
  131. {
  132. printf("\nDOUBLE leftROTATION");
  133. grandchild=child->rightchild;
  134. child->rightchild=grandchild->leftchild;
  135. grandchild->leftchild=child;
  136. (*pparent)->leftchild=grandchild->rightchild;
  137. grandchild->rightchild=*pparent;
  138. switch(grandchild->bf)
  139. {
  140. case 1:(*pparent )->bf=-1;child->bf=0;break;
  141. case 0:(*pparent)->bf=child->bf=0;break;
  142. case -1:(*pparent)->bf=0;
  143. child->bf=1;
  144. break;
  145. }
  146. *pparent=grandchild;
  147. }
  148. (*pparent)->bf=0;
  149. *unbalanced=false;
  150. return;
  151. }
  152.  
  153. void rightrotation(nodeptr *pparent,int *unbalanced)
  154. {
  155. nodeptr child,grandchild;
  156. child=(*pparent)->rightchild;
  157. if(child->bf==-1)
  158. {
  159. printf("\nSIMPLE RIGHTROTATION");
  160. (*pparent)->rightchild=child->leftchild;
  161. child->leftchild=*pparent;
  162. (*pparent)->bf=0;
  163. (*pparent)=child;
  164. }
  165. else
  166. {
  167. printf("\nDOUBLE rightROTATION.");
  168. grandchild=child->leftchild;
  169. child->leftchild=grandchild->rightchild;
  170. grandchild->rightchild=child;
  171. (*pparent)->rightchild=grandchild->leftchild;
  172. grandchild->leftchild=*pparent;
  173. switch(grandchild->bf){
  174. case 1:(*pparent)->bf=-1;
  175. child->bf=0;
  176. break;
  177. case 0: (*pparent)->bf=child->bf=0;
  178. break;
  179. case -1:(*pparent)->bf=0;
  180. child->bf=1;break;
  181. }
  182. *pparent=grandchild;
  183. }
  184. (*pparent)->bf=0;
  185. *unbalanced=false;
  186. return;
  187. }
  188.  
  189. int height(node* node)
  190. {
  191. if (node==NULL)
  192. return 0;
  193. else
  194. {
  195. ///* compute the depth of each subtree
  196. int lDepth = height(node->leftchild);
  197. int rDepth = height(node->rightchild);
  198.  
  199. ///* use the larger one
  200. if (lDepth > rDepth)
  201. return(lDepth+1);
  202. else return(rDepth+1);
  203. }
  204. }
  205.  
  206. void inorder(node *proot)
  207. {
  208. if(proot==NULL)return;
  209. inorder(proot->leftchild);
  210. printf("\nkey:%3d\tbalancefactor:%3d",proot->val,proot->bf);
  211. inorder(proot->rightchild);
  212.  
  213. }
Add Comment
Please, Sign In to add comment