Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct N
  5. {
  6. int key;
  7. struct N *left;
  8. struct N *right;
  9. } Node;
  10.  
  11.  
  12. typedef struct stack
  13. {
  14. Node *treeNode;
  15. struct stack *next;
  16. } stackNode;
  17.  
  18. Node *CreateBalanced (int N);
  19. void inorder (Node *root);
  20. int isEmpty(stackNode *top);
  21. void push (stackNode **top, Node *v);
  22. void deleteStack(stackNode **top);
  23. void printinOrder ( Node *root);
  24. void liberateTreeRecursion (Node *root);
  25. Node *LCA(Node *root, int nr1, int nr2);
  26.  
  27.  
  28. int main()
  29. {
  30. Node *root = NULL, *lowCommAnc;
  31. int n,nr1,nr2;
  32.  
  33. printf("numarul de noduri=");
  34. scanf("%d",&n);
  35. root = CreateBalanced(n);
  36. // printinOrder(root);
  37. inorder(root);
  38. printf("\nNumerele pentru care vreti sa calculati LCA:\nNr.1:");
  39. scanf("%d",&nr1);
  40. printf("Nr.2:");
  41. scanf("%d",&nr2);
  42. lowCommAnc = LCA(root, nr1, nr2);
  43. printf("\nLCA (%d , %d) = %d",nr1,nr2,lowCommAnc->key);
  44. liberateTreeRecursion(root);
  45. return 0;
  46. }
  47.  
  48.  
  49. Node *CreateBalanced (int N)
  50. {
  51. int toAdd;
  52. if(N>0)
  53. {
  54. Node* root = (Node*)malloc(sizeof(Node));
  55. printf("valoare nod=");
  56. scanf("%d",&toAdd);
  57. root->key = toAdd;
  58.  
  59. root->left = CreateBalanced(N/2);
  60. root->right = CreateBalanced(N-1-N/2);
  61. return root;
  62. }
  63. else
  64. return NULL;
  65. }
  66.  
  67. void push (stackNode **top, Node *v)
  68. {
  69. stackNode* newNode = (stackNode*)malloc(sizeof(Node));
  70. newNode->treeNode = v;
  71. newNode->next = *top;
  72. *top = newNode;
  73. }
  74.  
  75. Node* pop(stackNode **top)
  76. {
  77. if(isEmpty(*top))
  78. return NULL;
  79. stackNode *temp = (*top);
  80. Node *aux = temp->treeNode;
  81. *top = (*top)->next;
  82. free(temp);
  83. return aux;
  84. }
  85.  
  86. int isEmpty(stackNode *top)
  87. {
  88. return (top == NULL)? 1 : 0;
  89. }
  90.  
  91. void inorder (Node *root)
  92. {
  93. Node *traveler = root;
  94. stackNode *S = NULL;
  95. while(1)
  96. {
  97. while(traveler)
  98. {
  99. push(&S,traveler);
  100. traveler = traveler->left;
  101. // printf("%d",root->key);
  102. }
  103.  
  104.  
  105. if(isEmpty(S))
  106. break;
  107. else
  108. {
  109. traveler = pop(&S);
  110. printf("%d ",traveler->key);// !!!
  111. traveler = traveler->right;
  112. }
  113.  
  114. }
  115. deleteStack(&S);
  116. }
  117.  
  118. void deleteStack(stackNode **top)
  119. {
  120. stackNode* topCopy = *top, *temp;
  121. while(topCopy != NULL)
  122. {
  123. temp = topCopy;
  124. topCopy = topCopy->next;
  125. //printf("%d",temp->key);
  126. free(temp);
  127. }
  128. *top = NULL;
  129. }
  130.  
  131. void printinOrder ( Node *root)
  132. {
  133. if(root == NULL)
  134. return;
  135. printinOrder(root->left);
  136. printf("%d ",root->key);
  137. printinOrder(root->right);
  138. }
  139.  
  140. void liberateTreeRecursion (Node *root)
  141. {
  142. if(!root)
  143. return;
  144. liberateTreeRecursion(root->left);
  145. liberateTreeRecursion(root->right);
  146. free(root);
  147. }
  148.  
  149. Node *LCA(Node *root, int nr1, int nr2)
  150. {
  151. Node* lowComAnc;
  152. if(!root)
  153. return NULL;
  154.  
  155. Node *LCA_L = NULL;
  156. Node *LCA_R = NULL;
  157.  
  158. LCA_L = LCA(root->left,nr1,nr2);
  159. LCA_R = LCA(root->right,nr1,nr2);
  160.  
  161. if(root->key == nr1)
  162. return root;
  163. if(root->key == nr2)
  164. return root;
  165.  
  166. if(LCA_L && LCA_R)
  167. lowComAnc = root;
  168. return lowComAnc;
  169.  
  170. if(LCA_L)
  171. return LCA_L;
  172. if(LCA_R)
  173. return LCA_R;
  174. return NULL;
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement