fork(8) download
  1. /******************************************************************************************************************************************************
  2. Q17. Code challenge (requires 30-60 minutes)
  3. A category tree is a representation of a set of categories and their parent-child relationships.
  4. Each category has a unique name (no two categories have the same name). A category can have a parent category.
  5. Categories without a parent category are called root categories.
  6. Example: the category "A" has the categories "B" and "C", then "C" has "D", "E" and "F"...
  7. To add a category to a category tree, the name and the parent of the category should be provided.
  8. When adding a root category, a null value should be provided as the parent.
  9. A call to getChildren should return the direct children of the specified category in any order.
  10. InvalidArgumentException should be thrown when adding a category that has already been added anywhere in the CategoryTree or
  11. if a parent is specifiedbut does not exist.
  12. Please write your solution (program to represent an abstract category tree) in pure Golang, JavaScript, PHP, C or C++ and provide examples of using it. *
  13. ********************************************************************************************************************************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. typedef struct tagNode{
  20. char Data;
  21. struct tagNode* LeftChild;
  22. struct tagNode* RightBrother;
  23. }Node;
  24.  
  25. Node* CreateNode(char NewData);
  26. Node* FindSameNode(Node* Node, char NewData);
  27. void AddChildNode(Node* ParentNode, Node* ChildNode);
  28. void GetChildren(Node* Node);
  29.  
  30. Node* CreateNode(char NewData)
  31. {
  32. Node* NewNode = (Node*)malloc(sizeof(Node));
  33.  
  34. NewNode->Data = NewData;
  35. NewNode->LeftChild = NULL;
  36. NewNode->RightBrother = NULL;
  37.  
  38. return NewNode;
  39. }
  40.  
  41. /*
  42. void FindSameNode(Node* Node, char Data)
  43. {
  44. if(Node->LeftChild != NULL)
  45. {
  46. if(Node->Data == Data)
  47. {
  48. printf("Duplication %c\n", Data);
  49. }
  50. else
  51. {
  52. FindSameNode(Node->LeftChild, Data);
  53. }
  54.   }
  55.   if(Node->RightBrother != NULL)
  56. {
  57. if(Node->Data == Data)
  58. {
  59. printf("Duplication %c\n", Data);
  60. }
  61. else
  62. {
  63. FindSameNode(Node->RightBrother, Data);
  64. }
  65.   }
  66. }
  67. */
  68.  
  69. Node* FindSameNode(Node* ParentNode, char NewData)
  70. {
  71. if (ParentNode == NULL)
  72. return NULL;
  73.  
  74. if (ParentNode->Data == NewData)
  75. return ParentNode;
  76.  
  77. Node* Right = FindSameNode(ParentNode->RightBrother, NewData);
  78. Node* Left = FindSameNode(ParentNode->LeftChild, NewData);
  79. if (Right != NULL)
  80. return Right;
  81. if (Left != NULL)
  82. return Left;
  83.  
  84. return NULL;
  85. }
  86.  
  87. void AddChildNode(Node* ParentNode, Node* ChildNode)
  88. {
  89. Node* ExistNode = FindSameNode(ParentNode, ChildNode->Data);
  90. if(ExistNode != NULL)
  91. {
  92. printf("[InvalidArgumentException] %c already exists.\n", ExistNode->Data);
  93. return;
  94. }
  95.  
  96. if(ParentNode->LeftChild == NULL)
  97. {
  98. ParentNode->LeftChild = ChildNode;
  99. printf("ParentNode %c -> ", ParentNode->Data);
  100. printf("ChildNode %c\n", ChildNode->Data);
  101. }
  102. else
  103. {
  104. Node* TempNode = ParentNode->LeftChild;
  105. while(TempNode->RightBrother != NULL)
  106. {
  107. TempNode = TempNode->RightBrother;
  108. }
  109. TempNode->RightBrother = ChildNode;
  110. printf("ParentNode %c -> ", ParentNode->Data);
  111. printf("ChildNode %c\n", ChildNode->Data);
  112. }
  113. }
  114.  
  115. /*
  116. void GetChildren(Node* ParentNode)
  117. {
  118.  
  119.   if(Node->LeftChild != NULL)
  120.   {
  121.   }
  122.   else
  123.   {
  124.   }
  125. }
  126. */
  127.  
  128. int main()
  129. {
  130. Node* Root = CreateNode('A');
  131.  
  132. Node* B = CreateNode('B');
  133. Node* C = CreateNode('C');
  134. Node* D = CreateNode('D');
  135. Node* E = CreateNode('E');
  136. Node* F = CreateNode('F');
  137.  
  138. AddChildNode(Root, B);
  139. AddChildNode(Root, C);
  140. AddChildNode(C, D);
  141. AddChildNode(C, E);
  142. AddChildNode(C, F);
  143.  
  144. AddChildNode(C, E);
  145. AddChildNode(B, C);
  146.  
  147.  
  148. // GetChildren(C);
  149.  
  150. return 0;
  151. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
ParentNode A -> ChildNode B
ParentNode A -> ChildNode C
ParentNode C -> ChildNode D
ParentNode C -> ChildNode E
ParentNode C -> ChildNode F
[InvalidArgumentException] E already exists.
[InvalidArgumentException] C already exists.