Binary Search Tree的实现 C++版

// 定义二叉检索树类
template <typename Key >
class BST
{
	private:
		BSTNode<Key >* root;
		int nodecount;

		void clearhelp(BSTNode<Key >*);
		BSTNode<Key>* inserthelp(BSTNode<Key >*, const Key&);
		BSTNode<Key>* deletemin(BSTNode<Key >*);
		BSTNode<Key>* getmin(BSTNode<Key >*);
		BSTNode<Key>* removehelp(BSTNode<Key >*, const Key&);
		Key findhelp(BSTNode<Key >*, const Key&) const;
		void printhelp(BSTNode<Key >*, int) const;
		void preodhelper(BSTNode<Key >*) const;
		void inodhelper(BSTNode<Key >*) const;
		void postodhelper(BSTNode<Key >*) const;
		Key sumvalue(BSTNode<Key>*);

	public:
		BST() {root = NULL; nodecount = 0;}
		~BST() { clearhelp(root); }

		void clear()
		{
			clearhelp(root);
			root = NULL;
			nodecount = 0;
		}

		void insert(const Key& k)
		{
			root = inserthelp(root, k);
			nodecount ++;
		}

		Key remove(const Key& k)
		{
			Key temp = findhelp(root, k);
			if(temp != NULL)
			{
				root = removehelp(root, k);
				nodecount --;
			}
			return temp;
		}

		Key removeAny()
		{
			if(root != NULL)
			{
				Key temp = root->key();
				root = removehelp(root, root->key());
				nodecount --;
				return temp;
			}
			else
				return NULL;
		}

		Key find(const Key& k) const
		{
			return findhelp(root, k);
		}

		int size()
		{
			return nodecount;
		}

		void print() const
		{
			if(root == NULL)
				cout << "The BST is empty." << endl;
			else
				printhelp(root, 0);
		}

		void preorder()
		{
			preodhelper(root);
		}

		void inorder()
		{
			inodhelper(root);
		}

		void postorder()
		{
			postodhelper(root);
		}

		Key sum()
		{
			return sumvalue(root);
		}
};

template <typename Key >
Key BST<Key>::findhelp(BSTNode<Key >* root, const Key& k) const
{
	if(root == NULL)
		return NULL;
	if(k < root->key())
		return findhelp(root->left(), k);
	else if(k > root->key())
		return findhelp(root->right(), k);
	else
		return root->key();
}

template <typename Key >
BSTNode<Key>* BST<Key>::inserthelp(BSTNode<Key >* root, const Key& k)
{
	if(root == NULL)
		return new BSTNode<Key >(k, NULL, NULL);
	if(k < root->key())
		root->setLeft(inserthelp(root->left(), k));
	else
		root->setRight(inserthelp(root->right(), k));
	return 
		root;
}

template <typename Key >
BSTNode<Key >* BST<Key >::deletemin(BSTNode<Key >* root)
{
	if(root->left() == NULL)
		return root->right();
	else
	{
		root->setLeft(deletemin(root->left()));
		return root;
	}
}

template <typename Key >
BSTNode<Key >* BST<Key >::getmin(BSTNode<Key >* root)
{
	if(root->left() == NULL)
		return root;
	else
		return getmin(root->left());
}

template <typename Key >
BSTNode<Key >* BST<Key >::removehelp(BSTNode<Key >* root, const Key& k)
{
	if(root == NULL)
		return NULL;
	else if(k < root->key())
		root -> setLeft(removehelp(root->left(), k));
	else if(k > root->key())
		root -> setRight(removehelp(root->right(), k));
	else
	{
		BSTNode<Key >* temp =root;
		if( root->left() == NULL)
		{
			root = root->right();
			delete temp;
		}
		else if(root->right() == NULL)
		{
			root = root->left();
			delete temp;
		}
		else
		{
			BSTNode<Key >* temp = getmin(root->right());
			root->setkey(temp->key());
			root->setKey(temp->key());
			root->setRight(deletemin(rt->right()));
			delete temp;
		}
	}
	return root;
}

template <typename Key >
void BST<Key >::clearhelp(BSTNode<Key >* root)
{
	if(root == NULL)
		return ;
	clearhelp(root->left());
	clearhelp(root->right());
	delete root;
}

template <typename Key >
void BST<Key >::printhelp(BSTNode<Key >* root, int level) const
{
	if(root == NULL)
		return ;
	printhelp(root->left(), level+1);
	for(int i = 0; i<level; i++)
		cout << " ";
	cout << root->key() << "\n";
	printhelp(root->right(), level+1);
}

// 递归,前序遍历
template <typename Key >
void BST<Key>::preodhelper(BSTNode<Key >* rt) const
{
	if(rt == NULL)
		return ;
	cout << rt->key() << " ";
	preodhelper(rt->left());
	preodhelper(rt->right());
}

// 递归,中序遍历
template <typename Key >
void BST<Key>::inodhelper(BSTNode<Key >* rt) const
{
	if(rt == NULL)
		return ;
	inodhelper(rt->left());
	cout << rt->key() << " ";
	inodhelper(rt->right());
}

// 递归,后序遍历
template <typename Key >
void BST<Key>::postodhelper(BSTNode<Key >* rt) const
{
	if(rt == NULL)
		return ;
	postodhelper(rt->left());
	postodhelper(rt->right());
	cout << rt->key() << " ";
}

// 一棵在结点上存放整型数值的数,递归计算所有结点的数值之和
template <typename Key>
Key BST<Key>::sumvalue(BSTNode<Key>* rt)
{
	if (rt == NULL)
		return 0;
	if ((rt->left() == NULL)&&(rt->right() == NULL))
		return rt->key();		
	else
	{
		return rt->key() + sumvalue(rt->left()) + sumvalue(rt->right());		
	}
}

#include <iostream>
using namespace std;
// 测试用主程序
int main()
{
	int a[] = {15,20,25,18,16,5,7};
	int len = sizeof(a) / sizeof(a[0]);
	
	// 创建二叉检索树
	BST<int> myBST;
	for(int count = 0; count < len; count++)
	{
		myBST.insert(a[count]);
	}
	// 打印二叉检索树
	myBST.print();

	// 前序遍历
	myBST.preorder();
	cout << endl;
	// 中序遍历
	myBST.inorder();
	cout << endl;
	// 后序遍历
	myBST.postorder();
	cout << endl;

	// 打印树的所有结点的数值之和
	cout << "The sum of each node's value is: " << myBST.sum() << endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值