我有一些無序帶節點名的數據
比如
1,22,3,=a
1,22,4,=b
1,33,5,=c
1,22,6,=d
要寫成這樣的
該怎麼寫算法啊,主要是判斷父節點有無和指針的問題
CString * CTreeappDlg::SplitString(CString str, char split, int& iSubStrs)
{
int iPos = 0; //分割符位置
int iNums = 0; //分割符的總數
CString strTemp = str;
CString strRight;
//先計算子字符串的數量
while (iPos != -1)
{
iPos = strTemp.Find(split);
if (iPos == -1)
{
break;
}
strRight = strTemp.Mid(iPos + 1, str.GetLength());
strTemp = strRight;
iNums++;
}
if (iNums == 0) //沒有找到分割符
{
//子字符串數就是字符串本身
iSubStrs = 1;
return NULL;
}
//子字符串數組
iSubStrs = iNums + 1; //子串的數量 = 分割符數量 + 1
CString* pStrSplit;
pStrSplit = new CString[iSubStrs];
strTemp = str;
CString strLeft;
for (int i = 0; i < iNums; i++)
{
iPos = strTemp.Find(split);
//左子串
strLeft = strTemp.Left(iPos);
//右子串
strRight = strTemp.Mid(iPos + 1, strTemp.GetLength());
strTemp = strRight;
pStrSplit[i] = strLeft;
}
pStrSplit[iNums] = strTemp;
return pStrSplit;
}
void CTreeappDlg::Append(CString str)
{
int n = 0;
CString * data = SplitString(str, ',', n);
HTREEITEM hRootItem;
hRootItem = m_tree.GetRootItem();
bool b = false;
while (hRootItem != NULL)
{
if (m_tree.GetItemText(hRootItem) == data[0])
{
b = true;
break;
}
hRootItem = m_tree.GetNextItem(hRootItem, TVGN_NEXT);
}
if (!b)
{
hRootItem = m_tree.InsertItem((LPCTSTR)data[0]);
}
for (int i = 1; i < 3; i++)
{
HTREEITEM pre = hRootItem;
hRootItem = m_tree.GetChildItem(hRootItem);
b = false;
while (hRootItem != NULL)
{
if (m_tree.GetItemText(hRootItem) == data[i])
{
b = true;
break;
}
hRootItem = m_tree.GetNextItem(hRootItem, TVGN_NEXT);
}
if (!b)
{
hRootItem = m_tree.InsertItem((LPCTSTR)data[i], pre);
m_tree.Expand(pre,TVE_EXPAND);
}
}
}
void CTreeappDlg::OnButton1()
{
Append("1,22,3,=a");
Append("1,22,4,=b");
Append("1,33,5,=c");
Append("1,22,6,=d");
}