栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

树模板的实现(C++)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树模板的实现(C++)

实现树的构造,拷贝构造,析构以及插入、删除和查找等基本功能。

树-模板的实现

#pragma once

template 
class CMyTree_List {
	struct TreeNode {
		T data;
		TreeNode* pParent;
		TreeNode* pBrother;
		TreeNode* pChild;
	};
	TreeNode* pRoot;
public:
	CMyTree_List();
	~CMyTree_List();
	CMyTree_List(CMyTree_List const& other);//拷贝构造
	CMyTree_List& operator=(CMyTree_List& other);//赋值运算符重载
public:	//公有接口给使用者使用
	void clear();	//删除整棵树
	void clear(T const& subTreeNode);	//删除子树
	bool find(T const& findData);
	void insert(T const& insertData,T const& findData, bool isChild=true);
private:	//私有接口给类内函数自己使用
	void _clear(TreeNode* root);//递归删除
	TreeNode* _find(TreeNode* root, T const& findData){		//查找
		if (root) {
			if (root->data == findData) {
				return root;
			}
			TreeNode* tempNode = _find(root->pBrother, findData);
			if (tempNode)
				return tempNode;
			return _find(root->pChild, findData);
		}
		return nullptr;
	}

	void _copy(TreeNode* root);	//拷贝
};

template 
CMyTree_List::CMyTree_List() {	//构造
	pRoot = nullptr;
}

template 
CMyTree_List::CMyTree_List(CMyTree_List const& other) {	//拷贝构造
	pRoot = nullptr;
	if (other.pRoot) {
		insert(other.pRoot->data, 0);
		_copy(other.pRoot->pChild);
	}
}

template 
CMyTree_List::~CMyTree_List() {	//析构
	clear();
}

template 
CMyTree_List& CMyTree_List::operator=(CMyTree_List& other) {	//赋值运算符重载
	_clear(pRoot);
	CMyTree_List* temp = new CMyTree_List(other);
	pRoot = temp->pRoot;
	return *this;
}

template 
void CMyTree_List::clear() {		//删除整棵树
	_clear(pRoot);
}

template 
void CMyTree_List::clear(T const& subTreeNode) {	//删除子树
	TreeNode* findNode = _find(pRoot, subTreeNode);
	if (findNode == pRoot) {
		_clear(findNode);
		return;
	}
	if (findNode) {
		if (findNode->pBrother) {	//后面有兄弟结点
			TreeNode* cur = findNode->pParent->pChild;
			if (cur != findNode) {	//不是第一个兄弟结点,将前一个兄弟结点的pBrother指向下一个兄弟结点
				while (cur->pBrother != findNode) {
					cur = cur->pBrother;
				}
				cur->pBrother = findNode->pBrother;
				findNode->pBrother = nullptr;
			}
			else {	//是第一个兄弟结点,将父节点的pChild指向下一个兄弟结点
				findNode->pParent->pChild = findNode->pBrother;
			}
		}
		else {	//后面无兄弟结点
			TreeNode* cur = findNode->pParent->pChild;
			if (cur != findNode) {	//不是第一个兄弟结点,将前一个兄弟结点的pBrother赋空
				while (cur->pBrother != findNode) {
					cur = cur->pBrother;
				}
				cur->pBrother = nullptr;
			}
			else {	//是第一个兄弟结点,将父节点的pChild赋空
				findNode->pParent->pChild = nullptr;
			}

		}
		_clear(findNode);	//删除该子树
	}
}

template 
bool CMyTree_List::find(T const& findData) {		//查找
	return _find(pRoot, findData) != nullptr;
}

template 
void CMyTree_List::insert(T const& insertData, T const& findData, bool isChild = true) {		//插入
	//新结点准备
	TreeNode* tempInsertNode = new TreeNode;
	tempInsertNode->data = insertData;
	tempInsertNode->pParent = nullptr;
	tempInsertNode->pBrother = nullptr;
	tempInsertNode->pChild = nullptr;

	if (pRoot) {
		//非空树
		TreeNode* findNode = _find(pRoot, findData);
		if (findNode) {
			//找到插入的位置
			if (isChild) {
				//在插入位置的子节点处插入 有序
				if (findNode->pChild) {	//该子节点有孩子
					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) {	//该节点有兄弟
					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 {
			//没找到插入的位置

			findNode = pRoot;
			while (findNode->pChild) {
				findNode = findNode->pChild;
			}
			findNode->pChild = tempInsertNode;
			tempInsertNode->pParent = findNode;
		}
	}
	else {	//根节点为空
		pRoot = tempInsertNode;
	}

}

template 
void CMyTree_List::_clear(TreeNode* root) {	//删除
	if (root) {
		_clear(root->pBrother);
		if (root->pBrother) {
			root->pBrother = nullptr;
		}
		_clear(root->pChild);
		if (root->pChild) {
			root->pChild = nullptr;
		}
		delete root;
		root = nullptr;
	}
}


template 
void CMyTree_List::_copy(TreeNode* root) {	//	拷贝
	if (root) {
		insert(root->data, root->pParent->data);
		_copy(root->pBrother);
		_copy(root->pChild);
	}
}

主函数调用

#include "MyTree_list.h"

int _tmain(int argc, _TCHAR* argv[])
{
	CMyTree_List mt;
	mt.insert(1, 12);
	mt.insert(2, 1);
	mt.insert(3, 2, false);
	mt.insert(4, 100);

	CMyTree_List mt1(mt);
	mt1.insert(5, 4);

	CMyTree_List mt2 = mt1;
	mt2.clear(4);	//删除节点值为4的子树

	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号