XML Flat tree goes like this :
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Stack.xsl"?>
<Items>
<Item>
<Id>1</Id>
<ParentId>0</ParentId>
<Name>1</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>2</Id>
<ParentId>1</ParentId>
<Name>1.1</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>3</Id>
<ParentId>1</ParentId>
<Name>1.2</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>4</Id>
<ParentId>1</ParentId>
<Name>1.3</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>5</Id>
<ParentId>1</ParentId>
<Name>1.4</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>6</Id>
<ParentId>0</ParentId>
<Name>2</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>7</Id>
<ParentId>6</ParentId>
<Name>2.1</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>8</Id>
<ParentId>6</ParentId>
<Name>2.2</Name>
<SortOrder>0</SortOrder>
</Item>
<Item>
<Id>10</Id>
<ParentId>3</ParentId>
<Name>1.2.1</Name>
<SortOrder>0</SortOrder>
</Item>
</Items>
</XML>
and to display the output like below
1
1.1
1.2
1.2.1
1.3
1.4
2
2.1
2.2
Assuming the XML parsing is done and now is in some data structure in memory
and write a C or C++ program to display the output as above...
I pressume that interview was expecting which data structure I may use to store
XML elements values and how do I retrieve the same.
I tried using Binary search tree however it needs to have max of 2 children
for each node. again thought of using array, which i thought is a bit
complicated compare to tree..
guess array would be one of the solutions like a[i][j][k]
for(i=0;i<m;i++
{
print (a[i] n );
for (j=0;j<n;j++);
print(a[i][j] n);
for(k=0;k<p;k++)
print(a[i][j][k] n);
}
I feel tree would be more efficient way for programming this kind of
problems however I could find other than Binary search tree for programming
this again that is not a solution for this