树(C++实现)

这里所讲述的树是普通树其每一个节点都含有三个指针,包括父指针,孩子指针,以及兄弟指针。
在这里插入图片描述
(请称呼我为灵魂画家)
其遍历规则如图所示,一个父节点只指向一个子节点,其他的子节点通过兄弟指针连接,这样需要先遍历兄弟节点,然后在遍历子节点。
其简易代码如下所示:

template<class T>
class CMyTree_List
{
	struct TreeNode//私有结构
	{
		T date;//数据域的数据(可以有多个)
		TreeNode *pParent; //父指针
		TreeNode *pBrother;//兄弟指针
		TreeNode *pChild;//子指针
	};
	TreeNode *pRoot;//根节点指针
public:
	CMyTree_List();
	~CMyTree_List();
public:
	void clear();//清除函数(可以单独调用,也可以给析构调用)
	bool find(T const& findDate);//查找
	//插入的操作,需要有插入的数据,插入的位置,以及插入的是位置上的兄弟节点还是子节点
	void insert(T const& insertDate, T const& findDate, bool isChild = true);
	//void aaa(T const& date)
	//{
	//	TreeNode *a = _find(pRoot, date);
	//	if (a)
	//		a->date = date;
	//}
private:
	//递归满足两个条件:1、自身函数调用自身;2、必须要有参数,只有有参数的情况下,才能让自身这个函数结束
	void _clear(TreeNode *root);//删除
	//作业:_clear函数能否删除树中的某一棵子树,如果可以,为什么?如果不可以,为什么?
	TreeNode * _find(TreeNode *root, T const& findDate)
	{
		if (root)
		{
			if (root->date == findDate)//当前节点所指的数据就是待查找的数据
				return root;
			TreeNode *tempNode = _find(root->pBrother);//接住兄弟节点的查找结果
			if (tempNode)
				return tempNode;
			return _find(root->pChild,findDate);
		}
		return nullptr;//没有找到返回null
	}
};
template<class T>
void CMyTree_List<T>::insert(T const& insertDate, T const& findDate, bool isChild /*= true*/)
{
	//准备一个待插入节点
	TreeNode * tempInsertNode = new TreeNode;
	tempInsertNode->date = insertDate;
	tempInsertNode->pParent = nullptr;
	tempInsertNode->pBrother = nullptr;
	tempInsertNode->pChild = nullptr;
	//在树中做插入
	if (pRoot)
	{
		//表示树为非空树
		TreeNode *findNode = _find(pRoot, findDate);
		if (findNode)
		{
			//表示树中找到了插入的位置
			if (isChild)
			{
				//表示在找到的插入位置处关联为这个位置的子节点
				if (findNode->pChild)
				{
					//如果这个插入位置已经有子节点了
					无序
					//tempInsertNode->pBrother = findNode->pChild;
					//findNode->pChild = tempInsertNode;
					//tempInsertNode->pParent = findNode;

					//有序
					TreeNode *tempNode = findNode->pChild;
					while (tempNode->pBrother)
						tempNode = tempNode->pBrother;
					tempNode->pBrother = tempInsertNode;
					tempInsertNode->pParent = tempNode->pParent;
				}
				else
				{
					//如果这个插入位置没有子节点
					findNode->pChild = tempInsertNode;
					tempInsertNode->pParent = findNode;
				}
			}
			else
			{
				//表示在找到的插入位置处关联为这个位置的兄弟节点
				if (findNode->pBrother)
				{
					//表示该插入位置有兄弟节点
					无序
					//tempInsertNode->pBrother = findNode->pBrother;
					//findNode->pBrother = tempInsertNode;
					//tempInsertNode->pParent = findNode->pParent;

					//有序
					TreeNode *tempNode = findNode->pBrother;
					while (tempNode->pBrother)
						tempNode = tempNode->pBrother;
					tempNode->pBrother = tempInsertNode;
					tempInsertNode->pParent = tempNode->pParent;

				}
				else
				{
					//表示该插入位置没有兄弟节点
					findNode->pBrother = tempInsertNode;
					tempInsertNode->pParent = findNode->pParent;
				}
			}
		}
		else
		{
			//这个插入位置没有找到
			//自定义一个规则,如果在树中找不到待插入的位置,在最后一个子节点处插入
			TreeNode *tempNode = pRoot;
			while (tempNode->pChild)
				tempNode = tempNode->pChild;//一直找到最后一个子节点
			tempNode->pChild = tempInsertNode;//把待插入节点放置在最后一个子节点的子节点
			tempInsertNode->pParent = tempNode;//待插入节点的父节点为之前的最后一个子节点
		}
	}
	else
	{
		pRoot = tempInsertNode;//这是一棵空树,待插入节点就是树的根节点
	}
}
template<class T>
bool CMyTree_List<T>::find(T const& findDate)
{
	return _find(pRoot, findDate) != nullptr;
}
template<class T>
void CMyTree_List<T>::_clear(TreeNode *root)
{
	if (root)//如果root为空,函数结果,不为空,证明这个节点有值
	{
		_clear(root->pBrother);//递归兄弟节点
		_clear(root->pChild);//递归子节点
		//只有当前节点的所有兄弟节点和子节点已经删除完成
		delete root;//删除自身
		root = nullptr;
	}
}
template<class T>
void CMyTree_List<T>::clear()
{
	_clear(pRoot);
}
template<class T>
CMyTree_List<T>::~CMyTree_List()
{
	clear();
}
template<class T>
CMyTree_List<T>::CMyTree_List()
{
	pRoot = nullptr;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值