实现树的构造,拷贝构造,析构以及插入、删除和查找等基本功能。
树-模板的实现
#pragma once
template <class T>
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 <class T>
CMyTree_List<T>::CMyTree_List() { //构造
pRoot = nullptr;
}
template <class T>
CMyTree_List<T>::CMyTree_List(CMyTree_List const& other) { //拷贝构造
pRoot = nullptr;
if (other.pRoot) {
insert(other.pRoot->data, 0);
_copy(other.pRoot->pChild);
}
}
template <class T>
CMyTree_List<T>::~CMyTree_List() { //析构
clear();
}
template <class T>
CMyTree_List<T>& CMyTree_List<T>::operator=(CMyTree_List& other) { //赋值运算符重载
_clear(pRoot);
CMyTree_List* temp = new CMyTree_List(other);
pRoot = temp->pRoot;
return *this;
}
template <class T>
void CMyTree_List<T>::clear() { //删除整棵树
_clear(pRoot);
}
template <class T>
void CMyTree_List<T>::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 <class T>
bool CMyTree_List<T>::find(T const& findData) { //查找
return _find(pRoot, findData) != nullptr;
}
template <class T>
void CMyTree_List<T>::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 <class T>
void CMyTree_List<T>::_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 <class T>
void CMyTree_List<T>::_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<int> mt;
mt.insert(1, 12);
mt.insert(2, 1);
mt.insert(3, 2, false);
mt.insert(4, 100);
CMyTree_List<int> mt1(mt);
mt1.insert(5, 4);
CMyTree_List<int> mt2 = mt1;
mt2.clear(4); //删除节点值为4的子树
return 0;
}