(三)数据结构-二叉树的查找

二叉查找树的一些实现,一些心得都嵌入其中的注释

#include "iostream"
using namespace std;

//因为查找树左小右大
template <class T>
class BinarySearchTree
{
public:
	BinarySearchTree();    //构造
	BinarySearchTree(const BinarySearchTree &rhs);   //复制构造
	~BinarySearchTree();

	const BinarySearchTree &operator=(const BinarySearchTree &rhs);

	const T &findMin()const;   //找打最小element那个结点及其值
	const T &findMax()const;   //找打最大element那个结点及其值
	bool contains(const T &x);  //其中有无此element的结点,有返回true,否则返回false;
	bool isempty() const;      //判断是否为空
	void printTree() const;    //打印树,遍历显示

	void makeEmpty();          //置空
	void insert(const T &x);    //插入结点
	void remove(const T &x);   //删除结点

private:
	struct BinaryNode
	{
		T element;
		BinaryNode *leftChild;
		BinaryNode *rightChild;
		
		BinaryNode(const T &elem, BinaryNode *lt, BinaryNode *rt)        
			:element(elem), leftChild(lt), rightChild(rt){}              //结点复制构造时使用
	};

	BinaryNode *root;

	void insert(const T &x, BinaryNode * &root) const;  //指向引用的指针
	void remove(const T &x, BinaryNode * &root) const;   //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"

	BinaryNode *findMin(BinaryNode *root) const;
	BinaryNode *findMax(BinaryNode *root) const;

	bool contains(const T &x, BinaryNode *root) const;
	void makeEmpty(BinaryNode * &root);
	void printTree(BinaryNode * root) const;
	BinaryNode *clone(BinaryNode *root) const;    //复制返回的根节点
};

template <class T>
BinarySearchTree<T>::BinarySearchTree()
{
	root = NULL;
}

template <class T>
BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree &rhs)
{
	operator=(rhs);
}

template <class T>
BinarySearchTree<T>::~BinarySearchTree()
{
	//delete root;
}

template <class T>
const BinarySearchTree<T> &BinarySearchTree<T>::operator=(const BinarySearchTree &rhs)
{
	if(this = &this)
		return *this;
	delete root;
	root = new BinaryNode;
	root = rhs.root;
	
	return *this;
}

template <class T>
bool BinarySearchTree<T>::isempty() const
{
	if(!root)
		return true;
	else
		return false;
}

template <class T>
const T &BinarySearchTree<T>::findMin()const
{
	T _min;
	BinaryNode *minNode = new BinaryNode;
	minNode = findMin(root);
	_min = minNode.element;
	
	return _min;
}

template <class T>
const T &BinarySearchTree<T>::findMax()const
{
	T _max;
	BinaryNode *maxNode = new BinaryNode;
	maxNode = findMax(maxNode);
	_max = maxNode.element;
	
	return _max;
}


//声明这个是类型typename
template <class T>
typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMin(BinaryNode *root) const
{
	if(root)
	{
		if(root->leftChild)
		{
			return findMin(root->leftChild);
		}
		else 
			return root;
	}
	else
		return root;
}

template <class T>
typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax(BinaryNode *root) const
{
	if(root)
	{
		if(root->rightChild)
		{
			return findMax(root->rightChild);   //返回这个小的结点
		}
		else 
			return root;
	}
	else
		return root;
}

template <class T>
bool BinarySearchTree<T>::contains(const T &x)
{
	return contains(x, root);
	
}

template <class T>
bool BinarySearchTree<T>::contains(const T &x, BinaryNode *root) const
{
	if(!root)
		return false;
	else if(x < root->element)
		return contains(x, root->leftChild);
	else if(x > root->element)
		return contains(x, root->rightChild);
	else
		return true;
}

template <class T>
void BinarySearchTree<T>::insert(const T &x)
{
	insert(x, root);
}

template <class T>
void BinarySearchTree<T>::insert(const T &x, BinaryNode * &root) const  //指向引用的指针
{
	if(!root)
	{
		root = new BinaryNode(x, NULL, NULL);   //那个在结点中构造函数的作用,哈哈
	}
	if(root && x !=root->element)
	{
		if(x < root->element)
			insert(x, root->leftChild);
		else
			insert(x, root->rightChild);
	}
	else
	return;
}

template <class T>
void BinarySearchTree<T>::remove(const T &x)
{
	remove(x, root);

}

//删除,要的那个结点,若左存在,从他的作结点中找出中序遍历的最后一个结点,替换这个;若左不存在,则用右替换
template <class T>
void BinarySearchTree<T>::remove(const T &x, BinaryNode * &root) const   //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"
{
	if(!root)
		return;
	else
	{
		if(x > root->element)
		{
			remove(x, root->rightChild);
		}
		else if(x < root->element)
			remove(x, root->leftChild);
		else if(NULL != root->leftChild && NULL != root->rightChild)
		{
			root->element = findMin(root->rightChild)->element;
			remove(root->element, root->rightChild);
		}
		else
		{
			BinaryNode *oldNode = root;
			root = (root->leftChild != NULL) ? root->leftChild : root->rightChild;
			delete oldNode;
		}	
	}
}

template <class T>
void BinarySearchTree<T>::makeEmpty()
{
	makeEmpty(root);
}

template <class T>
void BinarySearchTree<T>::makeEmpty(BinaryNode * &root)
{
	if(!root)
		return;
	else
	{
		if(root->leftChild)
			makeEmpty(root->leftChild);
		if(root->rightChild)
			makeEmpty(root->rightChild);
		delete root;
	}
}

int main()
{
	BinarySearchTree<int> binaryTree;
	binaryTree.insert(10);
	binaryTree.insert(5);
	binaryTree.insert(6);
	binaryTree.insert(12);
	binaryTree.insert(15);
	binaryTree.remove(6);
	cout << binaryTree.contains(12) << endl;
	cout << binaryTree.isempty() << endl;

	binaryTree.makeEmpty();    //注意在这里已经delete root一次, 那么在析构中就不应该delete 一次了

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值