国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據結構-二叉搜索樹

2019-11-06 06:01:56
字體:
來源:轉載
供稿:網友
#PRagma once//K/V模型(key/value) //運用:英漢互譯  template<typename K, typename V>struct SearchBinaryTreeNode{	SearchBinaryTreeNode<K,V>* _left;	SearchBinaryTreeNode<K,V>* _right;	const K _key;  //eg:英譯漢	V _value;	SearchBinaryTreeNode(const K& key, const V& value)		: _left(NULL)		, _right(NULL)		, _key(key)		, _value(value)	{}};template<typename K, typename V>class SearchBinaryTree{	typedef SearchBinaryTreeNode<K,V> Node;public:	SearchBinaryTree()		:_root(NULL)	{}	~SearchBinaryTree()	{		_Destory(_root);	}	/*bool Insert(const K& key)	{	Node* prev = NULL;	Node* cur = _root;	while (cur)	{	if (key < cur->_key)	{	prev = cur;	cur = cur->_left;	}	else if (key > cur->_key)	{	prev = cur;	cur = cur->_right;	}	else	{	return false;	}	}	if (_root != NULL)	{	if (key < prev->_key)	{	prev->_left = new Node(key);	}	else	{	prev->_right = new Node(key);	}	}	else	{	_root = new Node(key);	}	return true;	}*/	bool Insert(const K& key,const V& value)  //非遞歸實現插入	{		Node* cur = _root;		Node* node = new Node(key,value);		while (cur)		{			if (key < cur->_key)			{				if (cur->_left == NULL)				{					cur->_left = node;					return true;				}				else				{					cur = cur->_left;				}			}			else if (key > cur->_key)			{				if (cur->_right == NULL)				{					cur->_right = node;					return true;				}				else				{					cur = cur->_right;				}			}			else			{				return false;			}		}		_root = node;		return true;	}	bool Remove(const K& key)//非遞歸實現刪除	{		Node* parent = NULL;		Node* cur = _root;		while (cur)		{			if (cur->_key == key)//已找到要刪除的節點			{				if (cur->_left == NULL)//左為空				{					if (cur == _root)					{						_root = _root->_right;					}					else if (parent->_left == cur)					{						parent->_left = cur->_right;					}					else if (parent->_right == cur)					{						parent->_right = cur->_right;					}				}				else if (cur->_right == NULL)//右為空				{					if (cur == _root)//防止斜樹刪除根節點時parent為NULL導致奔潰					{						_root = _root->_left;					}					else if (parent->_left == cur)					{						parent->_left = cur->_left;					}					else if (parent->_right == cur)					{						parent->_right = cur->_left;					}				}				else if (cur->_right != NULL && cur->_left != NULL)//左右都不為空				{					Node* rightmin = cur->_right;//要刪除節點的右子樹的最左節點(最小的)					parent = cur;					while (rightmin->_left)//找要刪除節點的右子樹的最左節點(最小的)					{						parent = rightmin;						rightmin = rightmin->_left;					}					//替換					cur->_key = rightmin->_key;					//假刪除					if (parent->_left == rightmin)					{						parent->_left = rightmin->_right;					}					else if (parent->_right == rightmin)					{						parent->_right = rightmin->_right;					}					cur = rightmin;				}				break;			}			else if (cur->_key < key)			{				parent = cur;				cur = cur->_right;			}			else			{				parent = cur;				cur = cur->_left;			}		}		delete cur;		cur = NULL;		return true;	}	Node* Find(const K& key)  //非遞歸實現查找	{		Node* cur = _root;		while (cur)		{			if (key == cur->_key)			{				return cur;			}			else if (key < cur->_key)			{				cur = cur->_left;			}			else			{				cur = cur->_right;			}		}		return NULL;	}	Node* FindR(const K& key)	{		return _FindR(_root, key);	}	bool InsertR(const K& key,const V& value)	{		return _InsertR(_root, key, value);	}	bool RemoveR(const K& key)	{		return _RemoveR(_root, key);	}	void InOrder()	{		_InOrder(_root);		cout << endl;	}	size_t Size()	{		return _Size(_root);	}protected:	Node* _FindR(Node* root, const K& key)	{		if (root == NULL)			return NULL;		if (root->_key > key)		{			return _FindR(root->_left, key);		}		else if (root->_key < key)		{			return _FindR(root->_right, key);		}		else		{			return root;		}	}	bool _InsertR(Node*& root, const K& key, const V& value) //注意第一個參數加&的道理	{		if (root == NULL)		{			root = new Node(key, value);			return true;		}		if (root->_key > key)		{			return _InsertR(root->_left, key, value);		}		else if (root->_key < key)		{			return _InsertR(root->_right, key, value);		}		else		{			return false;		}	}	bool _RemoveR(Node*& root, const K& key)//注意第一個參數加&的道理	{		if (root == NULL)			return false;		if (root->_key > key)			return _RemoveR(root->_left, key);		else if (root->_key < key)			return _RemoveR(root->_right, key);		else		{			Node* del = NULL;			if (root->_left == NULL)			{				del = root;				root = root->_right;  //等號左邊root表示上一層遞歸中root的left指針的別名,右邊的root代表當前root節點的右指針所指的節點			}			else if (root->_right == NULL)			{				del = root;				root = root->_left;			}			else			{				Node* parent = root;				Node* newnode = root->_right;  //找root為根其右子樹的最左節點				while (newnode->_left)				{					parent = newnode;					newnode = newnode->_left;				}				root->_key = newnode->_key;				del = newnode;				if (newnode == parent->_left)				{					parent->_left = newnode->_right;				}				else if (newnode == parent->_right)				{					parent->_right = newnode->_right;				}			}			delete del;			return true;		}	}	void _InOrder(Node* root)	{		if (root == NULL)		{			return;		}		_InOrder(root->_left);		cout << root->_key << " ";		_InOrder(root->_right);	}	size_t _Size(Node* _root)	{		if (_root == NULL)			return 0;		return _Size(_root->_left) + _Size(_root->_right) + 1;	}	void _Destory(Node* root)	{		if (root == NULL)			return;		_Destroy(root->_left);		_Destroy(root->_right);		delete root;	}protected:	Node* _root;};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 藁城市| 米脂县| 平谷区| 秀山| 鄂托克前旗| 枞阳县| 承德市| 泸水县| 上蔡县| 库车县| 抚松县| 临沂市| 聂荣县| 读书| 滁州市| 布拖县| 息烽县| 安平县| 满城县| 阳信县| 肃北| 和政县| 惠安县| 万宁市| 玉屏| 西吉县| 新民市| 湘西| 五寨县| 沭阳县| 沾化县| 河北省| SHOW| 许昌市| 泰宁县| 叙永县| 北辰区| 泸溪县| 玛沁县| 陇西县| 南岸区|