/******************************************************************************************************************************************************
Q17. Code challenge (requires 30-60 minutes)
A category tree is a representation of a set of categories and their parent-child relationships.
Each category has a unique name (no two categories have the same name). A category can have a parent category.
Categories without a parent category are called root categories.
Example: the category "A" has the categories "B" and "C", then "C" has "D", "E" and "F"...
To add a category to a category tree, the name and the parent of the category should be provided.
When adding a root category, a null value should be provided as the parent.
A call to getChildren should return the direct children of the specified category in any order.
InvalidArgumentException should be thrown when adding a category that has already been added anywhere in the CategoryTree or
if a parent is specifiedbut does not exist.
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. *
********************************************************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct tagNode{
char Data;
struct tagNode* LeftChild;
struct tagNode* RightBrother;
}Node;
Node* CreateNode(char NewData);
Node* FindSameNode(Node* Node, char NewData);
void AddChildNode(Node* ParentNode, Node* ChildNode);
void GetChildren(Node* Node);
Node* CreateNode(char NewData)
{
Node
* NewNode
= (Node
*)malloc(sizeof(Node
));
NewNode->Data = NewData;
NewNode->LeftChild = NULL;
NewNode->RightBrother = NULL;
return NewNode;
}
/*
void FindSameNode(Node* Node, char Data)
{
if(Node->LeftChild != NULL)
{
if(Node->Data == Data)
{
printf("Duplication %c\n", Data);
}
else
{
FindSameNode(Node->LeftChild, Data);
}
}
if(Node->RightBrother != NULL)
{
if(Node->Data == Data)
{
printf("Duplication %c\n", Data);
}
else
{
FindSameNode(Node->RightBrother, Data);
}
}
}
*/
Node* FindSameNode(Node* ParentNode, char NewData)
{
if (ParentNode == NULL)
return NULL;
if (ParentNode->Data == NewData)
return ParentNode;
Node* Right = FindSameNode(ParentNode->RightBrother, NewData);
Node* Left = FindSameNode(ParentNode->LeftChild, NewData);
if (Right != NULL)
return Right;
if (Left != NULL)
return Left;
return NULL;
}
void AddChildNode(Node* ParentNode, Node* ChildNode)
{
Node* ExistNode = FindSameNode(ParentNode, ChildNode->Data);
if(ExistNode != NULL)
{
printf("[InvalidArgumentException] %c already exists.\n", ExistNode
->Data
); return;
}
if(ParentNode->LeftChild == NULL)
{
ParentNode->LeftChild = ChildNode;
printf("ParentNode %c -> ", ParentNode
->Data
); printf("ChildNode %c\n", ChildNode
->Data
); }
else
{
Node* TempNode = ParentNode->LeftChild;
while(TempNode->RightBrother != NULL)
{
TempNode = TempNode->RightBrother;
}
TempNode->RightBrother = ChildNode;
printf("ParentNode %c -> ", ParentNode
->Data
); printf("ChildNode %c\n", ChildNode
->Data
); }
}
/*
void GetChildren(Node* ParentNode)
{
if(Node->LeftChild != NULL)
{
}
else
{
}
}
*/
int main()
{
Node* Root = CreateNode('A');
Node* B = CreateNode('B');
Node* C = CreateNode('C');
Node* D = CreateNode('D');
Node* E = CreateNode('E');
Node* F = CreateNode('F');
AddChildNode(Root, B);
AddChildNode(Root, C);
AddChildNode(C, D);
AddChildNode(C, E);
AddChildNode(C, F);
AddChildNode(C, E);
AddChildNode(B, C);
// GetChildren(C);
return 0;
}