1. 程式人生 > >C++實現搜尋二叉樹

C++實現搜尋二叉樹

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
using namespace std;

template<class K,class V>
struct BSTNode
{
	K _key;
	V _value;
	BSTNode* _left;
	BSTNode* _right;

	BSTNode(const K& key, const V& value)
		: _key(key)
		, _value(value)
		, _left(NULL)
		, _right(NULL)
	{}
};
template<class K, class V>
class BSTree
{
	typedef BSTNode<K, V> Node;
public:
	BSTree()
		:_root(NULL)
	{}
	//非遞迴插入
	bool Insert(const K & key, const V & value)
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
			return true;
		}
		Node *parent = NULL;
		Node *cur = _root;
		while (cur)
		{
			if (key > cur->_key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (key < cur->_key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(key, value);
		if (parent->_key > key)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		return true;
	}
   //遞迴插入
	bool Insert_R(const K & key, const V & value)
	{
		return _Insert_R(_root, key, value);
	}
	//查詢
	void Find(const K& key)
	{
		if (_Find(key))
			cout << "搜尋二叉樹中存在" << key << endl;
		else
			cout << "搜尋二叉樹中不存在" << key << endl;
	}
//非遞迴刪除
	bool Remove(const K& key)
	{
		if (_root == NULL)
		{
			return false;
		}
		if (_root->_left == NULL && _root->_right == NULL)
		{
			delete _root;
			_root = NULL;
			return true;
		}

		Node *parent = _root;
		Node *del = _root;
		while (del)
		{
			if (key < del->_key)
			{
				parent = del;
				del = del->_left;
			}
			else if (key > del->_key)
			{
				parent = del;
				del = del->_right;
			}
			else
			{
				break;
			}
		}
		if (del == NULL)
		{
			return false;
		}

		if (del->_left == NULL)
		{
			if (del->_key == key)
			{
				_root = del->_right;
				delete del;
				del = NULL;
				return true;
			}
			else if (parent->_left == del)
			{
				parent->_left = del->_right;
			}
			else
			{
				parent->_right = del->_right;
			}
		}
		else if (del->_right == NULL)
		{
			if (del->_key == key)
			{
				_root = del->_left;
				delete del;
				del = NULL;
				return true;
			}
			else if (parent->_right == del)
			{
				parent->_right = del->_left;
			}
			else
			{
				parent->_left = del->_left;
			}
		}
		else
		{
			Node* sub = del;
			Node* First = del->_right;
			if (First->_left)
			{
				sub = First;
				First = First->_left;
			}
			swap(del->_key, First->_key);
			swap(del->_value, First->_value);
			del = First;
			if (del->_left == NULL)
			{
				if (sub->_left == del)
				{
					sub->_left = del->_right;
				}
				else
				{
					sub->_right = del->_right;
				}
			}
			else if (del->_right == NULL)
			{
				if (sub->_left == del)
				{
					sub->_left = del->_left;
				}
				else
				{
					sub->_left = del->_left;
				}
			}
		}
		delete del;
		del = NULL;
		return true;
	}
//遞迴刪除
	bool Remove_R(const K& key)
	{
		return _Remove_R(_root, key);
	}
	//查詢
	Node* _Find(const K& key)
	{
		if (_root == NULL)
		{
			return NULL;
		}
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_key)
			{
				cur = cur->_left;
			}
			else if (key > cur->_key)
			{
				cur = cur->_right;
			}
			else
			{
				break;
			}
		}
		return cur;
	}
//前序遍歷
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
protected:
	bool _Insert_R(Node*&root,const K & key, const V & value)
	{
		if (root == NULL)
		{
			root = new Node(key, value);
			return true;
		}
		if (key < root->_key)
		{
			return _Insert_R(root->_left, key, value);
		}
		else if (key > root->_key)
		{
			return _Insert_R(root->_right, key, value);
		}
		else
		{
			return false;
		}
	}
	bool _Remove_R(Node *&root, const K&key)
	{
		if (root == NULL)
		{
			return false;
		}
		if (key < root->_key)
		{
			return _Remove_R(root->_left, key);
		}
		else if (key > root->_key)
		{
			return _Remove_R(root->_right, key);
		}
		else
		{
			Node* del = root;
			if (root->_left == NULL)
			{
				root = root->_right;
			}
			else if (root->_right == NULL)
			{
				root = root->_left;
			}
			else
			{
				Node* First = root->_right;
				while (First->_left)
				{
					First = First->_left;
				}
				swap(del->_key, First->_key);
				swap(del->_value, First->_value);
				return _Remove_R(root->_right, key);
			}
			delete del;
			del = NULL;
			return true;
		}
	}
	void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
private:
	BSTNode<K, V>* _root;
};

void test()
{
	int a[10] = { 5, 2, 4, 8, 7, 1, 9, 0, 6, 3 };
	BSTree<int, int> bstree;
	for (int i = 0; i < 10; i++)
	{
		bstree.Insert_R(a[i], a[i]);
	}
	bstree.InOrder();
	bstree.Find(7);
	bstree.Find(11);
	bstree.Remove_R(5);
	bstree.Remove_R(2);
	bstree.Remove_R(4);
	bstree.Remove_R(8);
	bstree.Remove_R(7);
	bstree.Remove_R(1);
	bstree.Remove_R(9);
	bstree.Remove_R(0);
	bstree.Remove_R(6);
	bstree.Remove_R(3);
	bstree.InOrder();
}

int main()
{
	test();
	system("pause");
	return 0;
}