BinarySearchTree

本文介绍了一种通用的二叉搜索树(BST)模板实现,包括插入、查找和删除等核心操作,并通过一个示例展示了如何使用该BST结构。
#pragma once

// Binary Search Tree Node
template<class K, class V>
struct BSTNode
{
	K _key;				// 节点关键值
	V _value;			// 节点数据
	BSTNode* _left;		// 左孩子
	BSTNode* _right;	// 右孩子

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

template<class K, class V>
class BSTree
{
	typedef BSTNode<K, V> Node;
public:
	BSTree()
		:_root(NULL)
	{}
	~BSTree()
	{}
	BSTree(const BSTree& t);
	BSTree&operator=(const BSTree& t);

public:
	BSTNode<K,V>* Find_R(const K& key)
	{
		return _Find(_root, key);
	}

	bool Insert_R(const K& key, const V& data)
	{
		return _Insert(_root, key, data);
	}

	bool Remove_R(const K& key)
	{
		return _Remove(_root, key);
	}

	void PrevOrder()
	{
		_PrevOrder(_root);
		cout<<endl;
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while(cur)
		{
			if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}

		return NULL;
	}

	bool Remove(const K& key)
	{
		if (_root == NULL)
		{
			return false;
		}
		else if (_root->_left == NULL && _root->_right == NULL)
		{
			delete _root;
			_root = NULL;
			return true;
		}

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

		if (del)
		{
			// 左树为空,则用右树调补
			if (del->_left == NULL)
			{
				// 注意删除节点为根节点的情况
				if (_root != del)
				{
					if(del == parent->_left)
						parent->_left = del->_right;
					else
						parent->_right = del->_right;
				}
				else
				{
					_root = del->_right;
				}
				
			}
			else if (del->_right == NULL)
			{
				// 注意删除节点为根节点的情况
				if (_root != del)
				{
					if(del == parent->_left)
						parent->_left = del->_left;
					else
						parent->_right = del->_left;
				}
				else
				{
					_root = del->_right;
				}
			}
			else
			{
				// 查找右子树的中序遍历的第一个节点
				Node* subRight = del->_right; 
				Node* firstInOrder = del->_right;
				Node* parent = del;
				while(firstInOrder->_left)
				{
					parent = firstInOrder;
					firstInOrder = firstInOrder->_left;
				}

				swap(del->_key, firstInOrder->_key);
				swap(del->_value, firstInOrder->_value);

				if (firstInOrder == parent->_left)
				{
					parent->_left = firstInOrder->_right;
				}
				else
				{
					parent->_right = firstInOrder->_right;
				}

				del = firstInOrder;
			}

			delete del;
		}
		else
		{
			return false;
		}

		return true;
	}

	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->_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);
		else
			parent->_left = new Node(key, value);

		return true;
	}

protected:
	void _PrevOrder(BSTNode<K,V>* root)
	{
		if (root)
		{
			_PrevOrder(root->_left);
		//	cout<<"[key:"<<root->_key<<","<<root->_value<<"]"<<"->";
			cout<<root->_key<<", ";
			_PrevOrder(root->_right);
		}
	}

	BSTNode<K,V>* _Find(BSTNode<K,V>* root, const K& key)
	{
		if (root)
		{
			if (root->_key > key)
			{
				return _Find(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _Find(root->_right, key);
			}
			else
			{
				return root;
			}
		}

		return NULL;
	}

	bool _Insert(BSTNode<K,V>*& root, const K& key, const V& data)
	{
		if (root == NULL)
		{
			root = new BSTNode<K,V>(key, data);
			return true;
		}
		else
		{
			if (root->_key > key)
			{
				return _Insert(root->_left, key, data);
			}
			else if (root->_key < key)
			{
				return _Insert(root->_right, key, data);
			}
			else
			{
				return false;
			}
		}
	}
	
	bool _Remove(BSTNode<K,V>*& root, const K& key)
	{
		if (root)
		{
			if(root->_key > key)
			{
				return _Remove(root->_left, key);
			}
			else if(root->_key < key)
			{
				return _Remove(root->_right, key);
			}
			else
			{
				// 1.如果左or右子树为空,则用一个子树进行填补。
				// 2.如果左右均不为空,则使用右子树中序的第一个节点填补
				if (root->_left == NULL)
				{
					BSTNode<K,V>* del = root;
					root = root->_right;
					delete del;
				}
				else if(root->_right == NULL)
				{
					BSTNode<K,V>* del = root;
					root = root->_left;
					delete del;
				}
				else
				{
					// 查找右子树的中序遍历的第一个节点
					BSTNode<K,V>* right = root->_right;
					while (right->_left)
					{
						right = right->_left;
					}

					swap(root->_value, right->_value);
					swap(root->_key, right->_key);
					_Remove(root->_right, right->_key);
				}

				return true;
			}
		}

		return false;
	}

private:
	BSTNode<K,V>* _root;
};

void TestBSTree()
{	
	// 5 3 4 1 7 8 2 6 0 9
	BSTree<int, double> t;
	t.Insert(5,5);
	t.Insert(3,3);
	t.Insert(4,4);
	t.Insert(1,1);
	t.Insert(7,7);
	t.Insert(8,8);
	t.Insert(2,2);
	t.Insert(6,6);
	t.Insert(0,0);
	t.Insert(9,9);

	t.PrevOrder();

	BSTNode<int,double>* ret = t.Find(9);
	cout<<"Find 9 ?: "<<ret->_key<<endl;
	ret = t.Find(10);
	cout<<"Find 10 ?: "<<ret<<endl;

	t.Remove(8);
	t.Remove(1);
	t.Remove(5);

	t.PrevOrder();
}

在Java中,以 `public class BinarySearchTree {` 开头的代码通常用于定义一个公共的二叉搜索树类。以下从类的定义、使用方法、实现细节、错误排查等方面进行阐述: ### 类的定义 在Java里,使用 `public class` 来定义一个公共类,意味着该类能够被其他包中的类访问。当以 `public class BinarySearchTree {` 开头时,代表定义了一个名为 `BinarySearchTree` 的公共类,文件名必须与类名一致,即 `BinarySearchTree.java`,这是Java语言的规定,有助于更好地组织代码,让一个文件形成一个功能单元,便于代码管理[^1][^2]。 ### 使用方法 以下是一个简单的使用示例: ```java public class BinarySearchTree { private Node root; private class Node { int key; Node left, right; public Node(int item) { key = item; left = right = null; } } public BinarySearchTree() { root = null; } public void insert(int key) { root = insertRec(root, key); } private Node insertRec(Node root, int key) { if (root == null) { root = new Node(key); return root; } if (key < root.key) root.left = insertRec(root.left, key); else if (key > root.key) root.right = insertRec(root.right, key); return root; } public static void main(String[] args) { BinarySearchTree tree = new BinarySearchTree(); tree.insert(50); tree.insert(30); tree.insert(20); tree.insert(40); tree.insert(70); tree.insert(60); tree.insert(80); } } ``` 在这个示例中,首先定义了 `BinarySearchTree` 类,其中包含一个内部类 `Node` 用于表示树的节点。`insert` 方法用于向二叉搜索树中插入新的节点。在 `main` 方法里,创建了 `BinarySearchTree` 的实例,并插入了一些节点。 ### 实现细节 - **节点类**:通常会在 `BinarySearchTree` 类内部定义一个 `Node` 类,用来表示树的节点,每个节点包含一个键值和左右子节点的引用。 - **插入操作**:插入操作是二叉搜索树的基本操作之一。插入时,从根节点开始比较,如果插入的键值小于当前节点的键值,则递归地插入到左子树中;如果大于,则递归地插入到右子树中。 - **查找操作**:查找操作也是常见操作。从根节点开始比较,如果查找的键值等于当前节点的键值,则找到;如果小于,则在左子树中继续查找;如果大于,则在右子树中继续查找。 ### 错误排查 - **文件名不匹配**:如果文件名与 `public class` 声明的类名不一致,编译器会报错。例如,若类名为 `BinarySearchTree`,文件名必须是 `BinarySearchTree.java`。 - **插入逻辑错误**:插入操作时,如果逻辑不正确,可能会导致树的结构不符合二叉搜索树的定义,即左子树的所有节点键值小于根节点,右子树的所有节点键值大于根节点。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值