Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. /*
  2. For R2 reprezentation I used a structure called multiNode with a key, number of children and children array of pointers.
  3. For R3 reprezentation I used a structure called binaryNode with a key, and 2 pointers(i.e. first child and next brother).
  4.  
  5. For T1 (i.e. convertParentToMultiway) we do it in O(n) efficiency. Despite the fact when we create a new node, we first create its parent,
  6. the implementation manages to do it in O(n) because when we call to create a node we stop if it is already created
  7. For T2(i.e. convertMultiwayToBinary) we do it in O(n) efficiency. We travel each node once.
  8.  
  9. I have an array of pointers to keep track of the nodes I create for R2.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #define MAX_NR_CHILDREN 10
  16. #define PRINT_SPACE 3
  17.  
  18. typedef struct multiNode_ {
  19. int key;
  20. int countChildren;
  21. struct multiNode_ *children[MAX_NR_CHILDREN];
  22. } multiNode;
  23.  
  24. typedef struct binaryNode_ {
  25. int key;
  26. struct binaryNode_ *firstChild, *nextBrother;
  27. } binaryNode;
  28.  
  29. multiNode *createMultiwayNode(int key)
  30. {
  31. multiNode *newNode = NULL;
  32. newNode = (multiNode*)malloc(sizeof(multiNode));
  33. if (newNode == NULL)
  34. {
  35. printf("Not enough memory");
  36. exit(-2);
  37. }
  38. newNode->countChildren = 0;
  39. newNode->key = key;
  40. return newNode;
  41. }
  42.  
  43. binaryNode *createBinaryNode(int key)
  44. {
  45. binaryNode *newNode = NULL;
  46. newNode = (binaryNode*)malloc(sizeof(binaryNode));
  47. if (newNode == NULL)
  48. {
  49. printf("Not enough memory");
  50. exit(-2);
  51. }
  52. newNode->key = key;
  53. newNode->firstChild = newNode->nextBrother = NULL;
  54. return newNode;
  55. }
  56.  
  57. void createNode(int i, int parent[], multiNode **nCreated, multiNode** root)
  58. {
  59. //if node is already created
  60. if (nCreated[i] != NULL)
  61. return;
  62.  
  63. nCreated[i] = createMultiwayNode(i);
  64.  
  65. //if root found
  66. if (parent[i] == -1)
  67. {
  68. (*root) = nCreated[i];
  69. return;
  70. }
  71.  
  72. //if parent isn't created, create it first
  73. if (nCreated[parent[i]] == NULL)
  74. createNode(parent[i], parent, nCreated, root);
  75.  
  76. //by now we have parent created. Add node to its children list
  77. multiNode *parentNode = nCreated[parent[i]];
  78. parentNode->children[parentNode->countChildren] = nCreated[i];
  79. parentNode->countChildren++;
  80.  
  81. }
  82.  
  83. void convertParentToMultiway(int parent[], int size, multiNode **root)
  84. {
  85. //need a vector to keep track if parent is created
  86. multiNode **nCreated = (multiNode**)malloc(size * sizeof(multiNode*));
  87. for (int i = 1; i < size; i++)
  88. nCreated[i] = NULL;
  89.  
  90. for (int i = 1; i < size; i++)
  91. createNode(i, parent, nCreated, root);
  92.  
  93. }
  94.  
  95. void convertMultiwayToBinaryRec(multiNode *multiRoot, binaryNode *binaryRoot)
  96. {
  97. binaryNode *brother = NULL;
  98. for (int i = 0; i < multiRoot->countChildren; i++)
  99. {
  100.  
  101. if (i == 0)
  102. {
  103. binaryRoot->firstChild = createBinaryNode(multiRoot->children[0]->key);
  104. brother = binaryRoot->firstChild;
  105. convertMultiwayToBinaryRec(multiRoot->children[i], brother);
  106. }
  107. else
  108. {
  109. brother->nextBrother = createBinaryNode(multiRoot->children[i]->key);
  110. brother = brother->nextBrother;
  111. convertMultiwayToBinaryRec(multiRoot->children[i], brother);
  112.  
  113. }
  114. }
  115. }
  116.  
  117. void convertMultiwayToBinary(multiNode *multiRoot, binaryNode **binaryRoot)
  118. {
  119. (*binaryRoot) = createBinaryNode(multiRoot->key);
  120. convertMultiwayToBinaryRec(multiRoot, *binaryRoot);
  121. }
  122.  
  123. void prettyPrint(multiNode *binaryRoot, int level)
  124. {
  125. for (int i = 1; i <= level * PRINT_SPACE; i++)
  126. printf(" ");
  127. printf("%d\n", binaryRoot->key);
  128. for (int i = 0; i < binaryRoot->countChildren; i++)
  129. prettyPrint(binaryRoot->children[i], level + 1);
  130. }
  131.  
  132. void prettyPrint(binaryNode *binaryRoot, int level)
  133. {
  134. for (int i = 1; i <= level * PRINT_SPACE; i++)
  135. printf(" ");
  136. printf("%d\n", binaryRoot->key);
  137. if (binaryRoot->firstChild != NULL)
  138. prettyPrint(binaryRoot->firstChild, level + 1);
  139. if (binaryRoot->nextBrother != NULL)
  140. prettyPrint(binaryRoot->nextBrother, level);
  141. }
  142.  
  143. void printArray(int array[], int size)
  144. {
  145. for (int i = 1; i < size; i++)
  146. printf("%2d ", i);
  147. printf("\n");
  148. for (int i = 1; i < size; i++)
  149. printf("%2d ", array[i]);
  150. printf("\n\n");
  151. }
  152.  
  153. int main()
  154. {
  155. int input[] = { 0, 2, 7, 5, 2, 7, 7, -1, 5, 2 };
  156. int inputSize = sizeof(input) / sizeof(int);
  157.  
  158. multiNode *multiRoot = NULL;
  159. binaryNode *binaryRoot = NULL;
  160.  
  161. convertParentToMultiway(input, inputSize, &multiRoot);
  162. convertMultiwayToBinary(multiRoot, &binaryRoot);
  163.  
  164. printArray(input, inputSize);
  165. prettyPrint(binaryRoot, 0);
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement