溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行搜索二叉樹分析

發(fā)布時(shí)間:2021-12-13 17:33:59 來源:億速云 閱讀:149 作者:柒染 欄目:編程語言

如何進(jìn)行搜索二叉樹分析,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

一、搜索二叉樹

1、定義:它是一棵排序二叉樹,可為空樹。

2、性質(zhì):

  • 每個(gè)節(jié)點(diǎn)都有一個(gè)作為搜索依據(jù)的關(guān)鍵碼(key),所有節(jié)點(diǎn)的關(guān)鍵碼互不相同;

  • 左子樹上所有節(jié)點(diǎn)的關(guān)鍵碼(key)都小于根節(jié)點(diǎn)的關(guān)鍵碼(key);

  • 右子樹上所有節(jié)點(diǎn)的關(guān)鍵碼(key)都大于根節(jié)點(diǎn)的關(guān)鍵碼(key);

  • 左、右子樹都是二叉搜索樹。

二、源代碼

1、定義節(jié)點(diǎn)

template<class K,class V>
struct BSTreeNode
{
	BSTreeNode<K,V> *_left;//左節(jié)點(diǎn)
	BSTreeNode<K,V> *_right;//右節(jié)點(diǎn)
	K _key;//節(jié)點(diǎn)權(quán)值
	V _value;

	BSTreeNode(const K& key,const V& value)
		:_key(key)
		,_value(value)
		,_left(NULL)
		,_right(NULL)
	{}
};

2、搜索二叉樹及其相關(guān)實(shí)現(xiàn)

template<class K,class V>
class BSTree
{
	typedef BSTreeNode<K,V> Node;
public:
	BSTree()
		:_root(NULL)
	{}
	
	//非遞歸
	Node* Find(const K& key)
	{
		return _Find(_root,key);
	}
	bool Insert(const K& key,const V& value)
	{
		return _Insert(_root,key,value);
	}
	bool Remove(const K& key)
	{
		return _Remove(_root,key);
	}

	//遞歸
	bool InOrder() //中序遍歷 --> 有序序列
	{
		return _InOrder(_root);
		cout<<endl;
	}
	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);
	}
protected:
	//非遞歸
	Node* _Find(Node *root,const K& key)
	{
		if(root == NULL) return NULL;
		Node *cur=root;
		if(cur->_key > key)
		{
			cur=cur->_right;
		}
		else if(cur->_key < key)
		{
			cur=cur->_left;
		}
		else 
		{
			return cur;
		}
		return NULL;
	}	
	bool _Insert(Node *&root,const K& key,const V& value)
	{
		if(root == NULL)
		{
			root=new Node(key,value);
			return true;
		}
		Node *cur=root;
		Node *parent=NULL;
		while(cur)
		{
			if(cur->_key < key)
			{
				parent=cur;
				cur=cur->_right;
			}
			else if(cur->_key > key)
			{
				parent=cur;
				cur=cur->_left;
			}
			else
				return false;
		}
		if(parent->_key < key)
		{
			parent->_right=new Node(key,value);
			parent->_right=parent;
		}
		else
		{
			parent->_left=new Node(key,value);
			parent->_left=parent;
		}
		return true;
	}
	bool _Remove(Node*& root,const K& key )
	{
		if(root == NULL) return false;
		Node *cur=root;
		Node *parent=NULL;
		while(cur) //找節(jié)點(diǎn)
		{
			if(cur->_key > key)
			{
				parent=cur;
				cur=cur->_left;
			}
			else if(cur->_key < key)
			{
				parent=cur;
				cur=cur->_right;
			}
			else //找到節(jié)點(diǎn)
			{
				if(cur->_left == NULL)//左為空
				{
					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)//右為空
				{
					if(parent == NULL)
						root=cur->_left;
					else if(parent->_left == cur)
						parent->_left=cur->_left;
					else
						parent->_right=cur->_left;
				}
				else //左右都不為空
				{
					Node *parent=cur;
					Node *left=cur->_right;//右子樹的最左節(jié)點(diǎn)
					while(left->_left)
					{
						left=left->_left;
					}
					cur->_key=left->_key;//替換結(jié)點(diǎn)
					cur->_value=left->_value;
					if(parent->_left == left)
						parent->_left=left->_left;
					else
						parent->_right=left->_right;
					delete left;
				}
			}
			return true;
		}
		return false;
	}

	//遞歸
	bool _InOrder(Node *root)
	{
		if(root == NULL) return false;
		_InOrder(root->_left);
		cout<<root->_left<<" ";
		_InOrder(root->_right);
		return true;
	}
	Node* _FindR(Node *root,const K& key)
	{
		if(root == NULL) return NULL;
		if(root->_key == key)
			return root;
		else if(root->_key > key)
			return _FindR(root->_left,key);
		else
			return _FindR(root->_right,key);
	}
	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  //找到節(jié)點(diǎn)
		{
			Node *del=NULL;
			if(root->_left == NULL) 
				root=root->_right;
			else if(root->_right == NULL)
				root=root->_left;
			else 
			{
				Node *parent=NULL;
				Node *left=root;
				while(left->_left)
				{
					parent=left;
					left=left->_left;
				}
				root->_key=left->_key;
				root->_value=left->_value;
				del=left;
				if(parent->_left == left)
					parent->_left=left->_left;
				else
					parent->_right=left->_right;
				delete del;
				return true;
			}
		}
		return false;
	}
protected:
	Node *_root;
};

3、總結(jié):

    搜索二叉樹是一棵排序二叉樹,可為空樹。它的每一個(gè)節(jié)點(diǎn)都遵從搜索二叉樹的性質(zhì)。

    搜索二叉樹的中序遍歷后為升序序列;其查找根據(jù)key值以及性質(zhì)進(jìn)行;其插入需先根據(jù)其key值找到插入的節(jié)點(diǎn),隨后添加節(jié)點(diǎn),另外其key值唯一;

    其刪除節(jié)點(diǎn)時(shí),需分3種情況:

   (1)僅左為空;

   (2)僅右為空;

   (3)該節(jié)點(diǎn)左右皆不為空。

        刪除該節(jié)點(diǎn),即需 找到 右子樹的最左節(jié)點(diǎn) 或 左子樹的最右節(jié)點(diǎn),作為替換結(jié)點(diǎn)。

看完上述內(nèi)容,你們掌握如何進(jìn)行搜索二叉樹分析的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI