此算法中的树结构为“左儿子有兄弟链接结构”
在这样的一个二叉树中,一个节点的左分支是他的大儿子节点,右分支为他的大兄弟节点。
这里讲的树有递归前根,中根,后根遍历,插入节点,插入兄弟节点,查找结点,释放内存这些功能。
重点说一下查找节点这一算法:
pSTreeNode CTree::Search( pSTreeNode pNode, TreeDataType Value )
{
if ( pNode == NULL )
return NULL;
if ( pNode->data == Value )
return pNode;
if ( pNode->pFirstChild == NULL && pNode->pNextBrother == NULL )
return NULL;
else
{
if ( pNode->pFirstChild != NULL ) //首先判断根节点的左儿子节点是否为空,若不为空,通过引入指针来寻找节点
{
pSTreeNode pNodeTemp = Search( pNode->pFirstChild, Value );
if ( pNodeTemp != NULL )
return pNodeTemp;
else
{
return Search( pNode->pNextBrother, Value );
}
}
else //否则在根节点的右兄弟节点中寻找
return Search( pNode->pNextBrother, Value );
}
<