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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值