设为首页 加入收藏

TOP

VC++ 树的孩子兄弟表示法 (一)
2014-11-23 19:09:43 】 浏览:553
Tags:孩子 兄弟 表示

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

树的孩子兄弟表示法,又叫二叉树表示法、二叉链表表示法,它是以二叉链表作为存储结构。
这个主要是因为在工作中要将一个三级菜单处理成需要的格式。我会在汽车电子中将这个程序列出来。[cpp] view plaincopyprint 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->

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇图像编程学习笔记2――bmp位图平.. 下一篇VC笔记

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目