Sorry that code in c#, but this code provides easy to understand to c++ programmers (I've no tested this code, but code well commented, hope this code will help you):
[c#]
private NodeBase AddWord(FlyGrid flyGrid, Word word)
{
//search for node associated with headword
NodeBase node = SearchHeadWord(flyGrid.Rows.Items, word);
//if node not found - create and add node with headword
if (node == null)
node = AddHeadWord(flyGrid.Rows.Items, word);
//add word to the headword node
return AddWord(node, word);
}
private NodeBase SearchHeadWord(NodeCollection nodes, Word word)
{
for(int i=0; i < nodes.Count; i++)
{
NodeBase node = nodes[i];//get node
//try to extract HeadWord instance from node.Value
HeadWord hword = node.Value as HeadWord;
//if associated value is Headword - compare with word.Headword
if (hword == word.Headword)
return node;
//you can scan node's children. if it is necessary
if (node.HasChildren and node.Items != null)
{
node = SearchHeadWord(node.Items, word);
if (node != null) return node;
}
}
return null;
}
private NodeBase AddHeadWord(NodeCollection nodes, Word word)
{
//create node
Node node = new Node(word.HeadWord);
//add node to the nodes collection
nodes.Add(node);
//return created node
return node;
}
private NodeBase AddWord(NodeBase node, Word word)
{
//create node
Node node = new Node(word);
//add node to the nodes collection
node.Items.Add(node);
return node;
}