【数据结构与算法】搜索二叉树实现

二叉树对于初学者来说有一点难度,但是对于提高编程能力还是非常有帮助的,这里给出搜索二叉树的实现:

#pragma once

template <typename T>
class BinaryTree {
public:
	BinaryTree();
private:
	typedef struct _Node {
		T data;
		struct _Node* lChild;
		struct _Node* rChild;
		struct _Node* pFather;
	}Node, *pNode;
	pNode m_TreeRoot;
	int m_nCount;
public:
	bool insert(T tValue);
	bool Delete(T tValue);
	bool Find(T tValue);
	bool Edit(T OldValue, T NewValue);
	bool Show();
private:
	bool DelLeaf(pNode pTempNode);
	bool DelSingle(pNode pTempNode);
	bool DelDouble(pNode pTempNode);
	bool BeforeTraverse(pNode pTempNode);
	bool MiddleTravserse(pNode pTempNode);
	bool AfterTravserse(pNode pTempNode);
};

template<typename T>
inline BinaryTree<T>::BinaryTree()
{
	m_TreeRoot = nullptr;
	m_nCount = 0;
}

template<typename T>
inline bool BinaryTree<T>::insert(T tValue)
{
	pNode pNewNode = new Node;
	if (pNewNode == nullptr) {
		std::cout << "内存空间申请失败!" << std::endl;
		return false;
	}
	pNewNode->data = tValue;
	pNewNode->lChild = nullptr;
	pNewNode->rChild = nullptr;
	pNewNode->pFather = nullptr;
	//如果是根节点:
	if (m_TreeRoot == nullptr) {
		m_TreeRoot = pNewNode;
		m_nCount++;
	}
	//如果不是根节点:
	else {
		pNode pTempNode = m_TreeRoot;
		while (true) {
			if (pTempNode->data > tValue) {
				if (pTempNode->lChild == nullptr) {
					pTempNode->lChild = pNewNode;
					pNewNode->pFather = pTempNode;
					m_nCount++;
					return true;
				}
				pTempNode = pTempNode->lChild;
			}
			else if (pTempNode->data < tValue) {
				if (pTempNode->rChild == nullptr) {
					pTempNode->rChild = pNewNode;
					pNewNode->pFather = pTempNode;
					m_nCount++;
					return true;
				}
				pTempNode = pTempNode->rChild;
			}
			else {
				std::cout << "已经有该值了" << std::endl;
				return false;
			}
		}
	}
}

template<typename T>
inline bool BinaryTree<T>::Delete(T OldValue)
{
	pNode pTempNode = m_TreeRoot;
	while (true) {
		if (pTempNode->data > OldValue) {
			pTempNode = pTempNode->lChild;
		}
		else if (pTempNode->data < OldValue) {
			pTempNode = pTempNode->rChild;
		}
		else {
			break;
		}
	}
	//叶子节点
	if (pTempNode->lChild == nullptr&&pTempNode->rChild == nullptr) {
		return DelLeaf(pTempNode);
	}
	//但分支节点
	else if (pTempNode->lChild == nullptr&&pTempNode->rChild->rChild == nullptr) {
		return DelSingle(pTempNode);
	}
	//双分支节点
	else {
		return DelDouble(pTempNode);
	}
}

template<typename T>
bool BinaryTree<T>::Find(T tValue)
{
	pNode pTempNode = m_TreeRoot;
	while (pTempNode != nullptr) {
		if (pTempNode->data > tValue) {
			pTempNode = pTempNode->lChild;
		}
		else if (pTempNode->data < tValue) {
			pTempNode = pTempNode->rChild;
		}
		else {
			std::cout << "二叉树表中有该数据" << std::endl;
			return true;
		}
	}
	return false;
}

template<typename T>
inline bool BinaryTree<T>::Edit(T OldValue, T NewValue)
{
	pNode pTempNode = m_TreeRoot;
	while (pTempNode != nullptr) {
		if (pTempNode->data > OldValue) {
			pTempNode = pTempNode->lChild;
		}
		else if (pTempNode->data < OldValue) {
			pTempNode = pTempNode->rChild;
		}
		else {
			break;
		}
	}
	this->Delete(pTempNode->data);
	this->insert(NewValue);
	return true;
}

template<typename T>
inline bool BinaryTree<T>::Show()
{
	pNode pTempNode = m_TreeRoot;
	if (pTempNode == nullptr) {
		return true;
	}
	std::cout << "前序遍历:";
	BeforeTraverse(pTempNode);
	std::cout << std::endl;
	std::cout << "中序遍历:";
	MiddleTravserse(pTempNode);
	std::cout << std::endl;
	std::cout << "后序遍历:";
	AfterTravserse(pTempNode);
	std::cout << std::endl;


}

template<typename T>
inline bool BinaryTree<T>::DelLeaf(pNode pTempNode)
{
	//判断是否是全局根节点
	if (pTempNode == m_TreeRoot) {
		delete pTempNode;
		m_TreeRoot = nullptr;
		m_nCount--;
		return true;
	}
	else {
		pNode pFatherNode = pTempNode->pFather;
		if (pFatherNode->lChild == pTempNode) {
			pFatherNode->lChild = nullptr;
		}
		else {
			pFatherNode->rChild = nullptr;
		}
		delete pTempNode;
		m_nCount--;
		pFatherNode = nullptr;
		return true;
	}
}

template<typename T>
inline bool BinaryTree<T>::DelSingle(pNode pTempNode)
{
	pNode pChildNode = pTempNode->rChild != nullptr ? pTempNode->rChild : pTempNode->lChild;
	//判断是否是全局根节点
	if (pTempNode == m_TreeRoot) {
		m_TreeRoot = pChildNode;
		delete pTempNode;
		m_nCount--;
		return true;
	}
	else {
		pNode pFatherNode = pTempNode->pFather;
		if (pFatherNode->lChild == pTempNode) {
			pFatherNode->lChild = pChildNode;
		}
		else {
			pFatherNode->rChild = pChildNode;
		}
		pChildNode->pFather = pFatherNode; 
		m_nCount--;
		delete pTempNode;
		return true;
	}
}

template<typename T>
inline bool BinaryTree<T>::DelDouble(pNode pTempNode)
{
	pNode pMaxNode = pTempNode->lChild;
	while (pMaxNode->rChild != nullptr) {
		pMaxNode = pMaxNode->rChild;
	}
	pTempNode->data = pMaxNode->data;
	if (pMaxNode->lChild == nullptr&&pMaxNode->rChild == nullptr) {
		return DelLeaf(pMaxNode);
	}
	if(pMaxNode->lChild == nullptr || pMaxNode->rChild == nullptr){
		return DelSingle(pMaxNode);
	}
}

template<typename T>
inline bool BinaryTree<T>::BeforeTraverse(pNode pTempNode)
{
	if (pTempNode == nullptr) {
		return true;
	}
	std::cout << pTempNode->data << " ";
	BeforeTraverse(pTempNode->lChild);
	BeforeTraverse(pTempNode->rChild);
	return true;
}

template<typename T>
inline bool BinaryTree<T>::MiddleTravserse(pNode pTempNode)
{
	if (pTempNode == nullptr) {
		return true;
	}
	BeforeTraverse(pTempNode->lChild);
	std::cout << pTempNode->data << " ";
	BeforeTraverse(pTempNode->rChild);
	return true;
}

template<typename T>
inline bool BinaryTree<T>::AfterTravserse(pNode pTempNode)
{
	if (pTempNode == nullptr) {
		return true;
	}
	BeforeTraverse(pTempNode->lChild);
	BeforeTraverse(pTempNode->rChild);
	std::cout << pTempNode->data << " ";
	return true;
}

如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!

你可能感兴趣的:(数据结构与算法,c++,算法,数据结构,二叉树)