二叉搜索树_c++

二叉搜索树实现
#include<iostream>
using namespace std;

template<class T> struct BinaryNode{
	BinaryNode(const T& t):key(t), p(0),left(0),right(0){}
	T  key;
	BinaryNode *p;
	BinaryNode *left;
	BinaryNode *right;
};

template<class T> class BinarySearchTree{
public:
	BinarySearchTree():root(0){}
	~BinarySearchTree();

	void InorderTreeWalk(BinaryNode<T> *x) const; //中序遍历
	void PreorderTreeWalk(BinaryNode<T> *x) const; //前序遍历
	void PostorderTreeWalk(BinaryNode<T> *x) const; //后序遍历
	BinaryNode<T>* Search(const T &k) const; //查找
	BinaryNode<T>* TreeMinimum(BinaryNode<T> *x) const; //最小元素
	BinaryNode<T>* TreeMaximum(BinaryNode<T> *x) const; //最大元素
	BinaryNode<T>* Successor(BinaryNode<T> *x) const; //查找后继
	void Insert(const T &val); //插入节点
	void Transplant(BinaryNode<T> *u, BinaryNode<T> *v); //用v代替u,v.left和v.right由调用者更新
	void Delete(BinaryNode<T> *x); //删除x所指节点
	void MakeEmpty(BinaryNode<T> *x); //清空树
	BinaryNode<T>* get_root()
	{
		return root;
	}
private:
	BinaryNode<T> *root;
};

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

template<class T> void BinarySearchTree<T>::InorderTreeWalk(BinaryNode<T> *x) const
{
	if (x != 0)
	{
		InorderTreeWalk(x->left);
		cout<<x->key<<" ";
		InorderTreeWalk(x->right);
	}
}

template<class T> void BinarySearchTree<T>::PreorderTreeWalk(BinaryNode<T> *x) const
{
	if (x != 0)
	{
		cout<<x->key<<' ';
		PreorderTreeWalk(x->left);
		PreorderTreeWalk(x->right);
	}
}

template<class T> void BinarySearchTree<T>::PostorderTreeWalk(BinaryNode<T> *x) const
{
	if (x !=0)
	{
		PostorderTreeWalk(x->left);
		PostorderTreeWalk(x->right);
		cout<<x->key<<' ';
	}
}

template <class T> BinaryNode<T>* BinarySearchTree<T>::Search(const T&k) const
{
	BinaryNode<T> *x = root;
	while (x != 0 && k != x->key)
	{
		if (k < x->key)
		{
			x = x->left;
		}
		else
		{
			x = x->right;
		}
	}

	return x;
}

template<class T> BinaryNode<T>* BinarySearchTree<T>::TreeMinimum(BinaryNode<T> *x) const
{
	while (x->left != 0)
	{
		x = x->left;
	}

	return x;
}

template<class T> BinaryNode<T>* BinarySearchTree<T>::TreeMaximum(BinaryNode<T> *x) const
{
	while(x->right != 0)
	{
		x = x->right;
	}

	return x;
}

template<class T> BinaryNode<T>* BinarySearchTree<T>::Successor(BinaryNode<T> *x) const
{
	if(x->right != 0)
	{
		return TreeMinimum(x->right);
	}

	BinaryNode<T> *y = x->p;
	while(y != 0 && x == y->right)
	{
		x = y;
		y = y->p;
	}

	return y;
}

template<class T> void BinarySearchTree<T>::Insert(const T &val)
{
	BinaryNode<T> * z= new BinaryNode<T>(val);
	BinaryNode<T> *y = 0;
	BinaryNode<T> *x = root;

	while(x != 0)
	{
		y = x;
		if (z->key < x->key)
		{
			x = x->left;
		}
		else
		{
			x = x->right;
		}
	}

	z->p = y;

	if (y == 0)
	{
		root = z;
	}
	else if(z->key < y->key)
	{
		y->left = z;
	}
	else
	{
		y->right = z;
	}
}

template<class T> void BinarySearchTree<T>::Transplant(BinaryNode<T> *u, BinaryNode<T> *v)
{
	if (u->p == 0)
	{
		//如果u是根节点
		root = v;
	}
	else if (u  = u->p->left)
	{
		u->p->left = v;
	}
	else
	{
		u->p->right = v;
	}

	if (v != 0)
	{
		v->p = u->p;
	}
}

template<class T> void BinarySearchTree<T>::Delete(BinaryNode<T> *x)
{
	if (x->Left = 0)
	{
		Transplant(x,x->right);
	}
	else if (x->right = 0)
	{
		Transplant(x,x->left);
	}
	else
	{
		BinaryNode<T> *y = TreeMinimum(x->right);

		if (y->p != x)
		{
			Transplant(y,y->right);
			y->right = z->right;
			y->right->p = y;
		}

		Transplant(x,y);
		y->left = x->left;
		y->left->p = y;
	}
}

template<class T> void BinarySearchTree<T>::MakeEmpty(BinaryNode<T> *x)
{
	if (x != 0)
	{
		if (x->right != 0)
		{
			MakeEmpty(x->right);
		}

		if (x->left != 0)
		{
			MakeEmpty(x->left);
		}

		delete(x);

		x = 0;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值