您的位置:首页 > 编程语言 > C语言/C++

VC++ 树的孩子兄弟表示法

2013-04-11 14:42 316 查看
树的孩子兄弟表示法,又叫二叉树表示法、二叉链表表示法,它是以二叉链表作为存储结构。
这个主要是因为在工作中要将一个三级菜单处理成需要的格式。我会在汽车电子中将这个程序列出来。
Tree.h

struct TreeNode
{
public:
struct TreeNode *firstChild;
struct TreeNode *nextSibling;
int tagParent;
int tagSelf;
CString data;
};

class CTree
{
public:
CTree();
virtual ~CTree();
TreeNode *root;
TreeNode *curr;
int tag;
void InsertChild(CString strValue);
BOOL FirstChild();
BOOL NextSibling();
BOOL CreateOptionTree(CString strValue);
void PreOrderTree(TreeNode *parent);
int  GetTreeDepth(TreeNode *parent);
int  GetChildNums(TreeNode *parent);
void CreateTag(TreeNode *parent);
void DeleteTree(TreeNode *parent);
void SetCurrent(TreeNode *t);
};

Tree.cpp

CTree::CTree()
{
tag = 1;
root = new TreeNode;
root->firstChild = NULL;
root->tagParent = 0;
root->tagSelf = tag;
curr = root;
}

CTree::~CTree()
{

}

/************************************************************************
函数名:  FirstChild
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-7
作  用:  使当前节点的第一个孩子节点为当前节点
形参数:
返回值:  TRUE:成功 FALSE:失败
修改记录:
************************************************************************/
BOOL CTree::FirstChild()
{
if (curr != NULL && curr->firstChild != NULL)
{
curr = curr->firstChild;
return TRUE;
}
else
{
return FALSE;
}
}

/************************************************************************
函数名:  NextSibling
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-7
作  用:  使当前节点的兄弟节点为当前节点
形参数:
返回值:  TRUE:成功 FALSE:失败
修改记录:
************************************************************************/
BOOL CTree::NextSibling()
{
if (curr != NULL && curr->nextSibling != NULL)
{
curr = curr->nextSibling;
return TRUE;
}
else
{
return FALSE;
}
}

/************************************************************************
函数名:  InsertChild
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-7
作  用:  将strValue插入到当前节点的最后一个孩子节点
形参数:
返回值:
修改记录:2013-3-8,增加tagParent,tagSelf
************************************************************************/
void CTree::InsertChild(CString strValue)
{
TreeNode *newNode = new TreeNode;

strValue.TrimLeft();
strValue.TrimRight();

newNode->data = strValue;
newNode->firstChild = NULL;
newNode->nextSibling = NULL;

if (curr->firstChild == NULL)
{
newNode->tagParent = curr->tagSelf;
newNode->tagSelf = ++tag;
curr->firstChild = newNode;
curr = curr->firstChild;
}
else
{
TreeNode *p = curr->firstChild;
while(p->data != strValue)
{
if (p->nextSibling != NULL)
{
p = p->nextSibling;
}
else
{
break;
}
}

if (p->data == strValue)
{
curr = p;
}
else
{
newNode->tagParent = p->tagParent;
newNode->tagSelf = ++tag;
p->nextSibling = newNode;
curr = p->nextSibling;
}
}
}

/************************************************************************
函数名:  CreateOptionTree
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-7
作  用:
形参数:
返回值:  TRUE:成功 FALSE:失败
修改记录:
************************************************************************/
BOOL CTree::CreateOptionTree(CString strValue)
{
if (root == NULL)
{
root = new TreeNode;
root->firstChild = NULL;
root->tagParent = 0;
root->tagSelf = tag;
curr = root;
}
if (strValue == "")
{
return TRUE;
}

InsertChild(strValue);
return TRUE;
}

/************************************************************************
函数名:  GetTreeDepth
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-8
作  用:
形参数:
返回值:
修改记录:未完成
************************************************************************/
int CTree::GetTreeDepth(TreeNode *parent)
{
int num = 1;

if (parent->firstChild == NULL)
{
return num;
}
else
{
TreeNode *p = parent;

}
}

/************************************************************************
函数名:  PreOrderTree
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-8
作  用:
形参数:
返回值:
修改记录:
************************************************************************/
void CTree::PreOrderTree(TreeNode *parent)
{
if (parent->firstChild != NULL)
{
PreOrderTree(parent->firstChild);
}
if (parent->nextSibling != NULL)
{
PreOrderTree(parent->nextSibling);
}
}

/************************************************************************
函数名:  GetChildNums
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-8
作  用:  得到节点的孩子数
形参数:
返回值:
修改记录:
************************************************************************/
int CTree::GetChildNums(TreeNode *parent)
{
if (parent->firstChild == NULL)
{
return 0;
}
else
{
int num = 1;
TreeNode *p = parent->firstChild;
while(p->nextSibling != NULL)
{
num++;
p = p->nextSibling;
}

return num;
}
}

/************************************************************************
函数名:  DeleteTree
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-11
作  用:  删除以parent为根结点的树
形参数:
返回值:
修改记录:
************************************************************************/
void CTree::DeleteTree(TreeNode *parent)
{
if (parent == NULL)
{
return;
}

TreeNode *p = parent->firstChild, *q;
while(p != NULL)
{
q = p->nextSibling;
DeleteTree(p);
p = q;
}

delete parent;
}

/************************************************************************
函数名:  SetCurrent
作  者:	 谭友亮(Charles Tan)
日  期:	 2013-3-11
作  用:  设置当前结点
形参数:
返回值:
修改记录:
************************************************************************/
void CTree::SetCurrent(TreeNode *t)
{
curr = t;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: