二叉搜索樹

二叉搜索樹

二叉搜索樹是基於二叉樹的一種結構,對於一個二叉樹它的左節點小於它的根節點,它的右節點大於它的根節點,它的每個子樹的結構相同

其結構以及接口實現過程如下

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

	BSTreeNode(const K&key, const V&value)
		:_key(key)
		, _value(value)
		, _left(NULL)
		, _right(NULL)
	{}
};
template<class K,class V>
class BSTree
{
	typedef BSTreeNode<K, V> Node;
public:
	BSTree()
		:_root(NULL)
	{}
	~BSTree()
	{
		_Destroy(_root);
	}
public:
	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 (cur->_value<value)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_value>value)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
				return false;
			
		}
		if (value > parent->_value)
			parent->_right = new Node(key, value);
		else
			parent->_left = new Node(key, value);
		return true;
	}
	
	bool Insert_R(const K&key, const V&value)//遞歸控制
	{
		return _Insert_R(_root,key,value);
	}
	
	Node*Find_R(const V&value)//遞歸
	{
		return _Find_R(_root, value);
	}
	Node*Find(const V&value)//尋找結點
	{
		Node*cur = _root;
		while (cur)
		{
			if (cur->_value > value)
				cur = cur->_right;
			else if (cur->_value < value)
				cur = cur->_right;
			else//cur->_value = value
				return cur;
		}
		return NULL;//未找到
	}
	bool Remove(const V&value)//刪除結點,刪除成功返回true,若不存在value則返回false
	{
		//1.刪除結點左或右爲空(或左右均爲空),左爲空則父節點直接指向右,刪除這個結點,若右爲空則父節點直接指向左
		//2.刪除的左右結點均不爲空,則找到這個結點左樹的最右節點或右樹的最左節點替換
		Node*cur = _root;
		Node*parent = NULL;
		while (cur)
		{
			if (cur->_value > value)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_value < value)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
				break;
		}
		if (cur == NULL)
			return false;
		Node*del;
		//情況1
		if (cur->_left == NULL)
		{
			del = cur;
			if (parent == NULL)//根結點
			{
				_root = cur->_right;
			}
			else
			{
				if (parent->_left == cur)
					parent->_left = cur->_right;
				else
					parent->_right = cur->_right;

			}			
		}
		else if (cur->_right == NULL)
		{
			del = cur;
			if (parent == NULL)
			{
				_root = cur->_left;
			}
			else
			{
				if (parent->_left = cur)
					parent->_left = cur->_left;
				else
					parent->_right = cur->_left;
			}
		}
		else//情況2
		{
			parent = cur;
			Node*firstLeft = cur->_right;//找這個結點右節點的最左結點
			while (firstLeft->_left)
			{
				parent = firstLeft;
				firstLeft = firstLeft->_left;
			}
			del = firstLeft;
			cur->_key = firstLeft->_key;
			cur->_value = firstLeft->_value;
			if (parent->_left == firstLeft)
			{
				parent->_left = firstLeft->_right;
			}
			else
				parent->_right = firstLeft->_right;
		}
		delete del;
		return true;
	}
	bool Remove_R(const V&value)
	{
		return _Remove_R(_root, value);
	}
	void InOrder()//中序遍歷,即二叉樹敗絮
	{
		_InOrder(_root);
		cout << endl;
	}
private:
	void _Destroy(Node*root)
	{
		if (root == NULL)
			return;
		_Destroy(root->_left);
		_Destroy(root->_right);

		delete root;
	}
	void _InOrder(Node*root)
	{
		if (root == NULL)
			return;
		_InOrder(root->_left);
		cout << root->_value<<" ";
		_InOrder(root->_right);
	}
	bool _Insert_R(Node*&root,const K&key,const V&value)
	{
		if (root == NULL)
		{
			root = new Node(key, value);
			return true;
		}
		if (value > root->_value)
			return _Insert_R(root->_right, key, value);//遞歸右樹
		else if (value<root->_value)
			return _Insert_R(root->_left,key,value);//遞歸左樹
		return false;
	}
	Node*_Find_R(Node*root, const V&value)
	{
		if (root->_value == value)
			return root;
		if (value>root->_value)
			return _Find_R(root->_right, value);
		else if (value < root->_value)
			return _Find_R(root->_left, value);
		else
			return NULL;//根爲空或者未找到
	}
	bool _Remove_R(Node*&root, const V&value)
	{
		if (root == NULL)
			return false;
		if (root->_value>value)
			return _Remove_R(root->_left, value);
		else if (root->_value < value)
			return _Remove_R(root->_right, value);
		else//root->_value == value
		{
			Node*del = root;
			if (root->_left == NULL)
				root = root->_right;
			else if (root->_right == NULL)
				root = root->_left;
			else//左右均不爲空
			{
				Node*parent = root;
				Node*cur = root->_left;
				while (cur->_left)
				{
					parent = cur;
					cur = cur->_left;
				}
				swap(cur->_value, root->_value);
				swap(cur->_key, root->_key);
				del = cur;
				parent->_left = cur->_right;
			}
			delete del;
		}
		return true;
	}
private:
	Node*_root;
};


發佈了28 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章