搜索二叉树的实现与应用

一、基本概念:
  • 二叉搜索树又称二叉排序树。它或者是一棵空树,或者是具有以下性质的二叉树:
  1. 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值。
  2. 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值。
  3. 它的左右子树也分别为二叉搜索树。
二、二叉搜索树的操作:
  1. 二叉树的查找
    操作流程:
    二叉树的查找
  2. 二叉树的删除
    首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
    a. 要删除的结点无孩子结点
    b. 要删除的结点只有左孩子结点
    c. 要删除的结点只有右孩子结点
    d. 要删除的结点有左、右孩子结点

    看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程如下:
    情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点
    情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
    情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中
    ,再来处理该结点的删除问题。
  3. 二叉树的插入
    ①树为空,则直接插入。
    ②树不为空,找到插入位置,插入新节点。
三、二叉搜索树的模拟实现:

树节点的定义


template<typename Type>
class BSTNode
{
	friend class BSTree<Type>;
public:
	BSTNode(Type d = Type(), BSTNode<Type>*left = nullptr, BSTNode<Type>*right = nullptr)
		: data(d), leftChild(left), rightChild(right)
	{}
	~BSTNode()
	{}
private:
	Type data;
	BSTNode<Type> *leftChild;
	BSTNode<Type> *rightChild;
};

②二叉搜索树的实现:

template<typename Type>
class BSTree
{
public:
	BSTree() : root(nullptr)//构造函数,构造空的二叉搜索树
	{}
	BSTree(Type ar[], int n) : root(nullptr)//根据数据构造二叉搜索树
	{
		for (int i = 0; i<n; ++i)
		{
			Insert(ar[i]);//插入数据
		}
	}
	~BSTree()//析构函数
	{
		Clear();
	}
public:
	Type Max()const;//求最大值
	Type Min()const;//求最小值
	bool Insert(const Type &x);//插入
	void Order()const;//中序遍历
	BSTNode<Type>* Find(const Type &key);//查找
	void Clear();//清空
	bool Remove(const Type &key);//删除
protected:
	bool Insert(BSTNode<Type> *&t, const Type &x)
	{
		if (t == nullptr)
		{
			t = new BSTNode<Type>(x);
			return true;
		}
		else if (x < t->data)
			return Insert(t->leftChild, x);
		else if (x > t->data)
			return Insert(t->rightChild, x);
		return false;
	}
	Type Max(BSTNode<Type> *t)const
	{
		assert(t != nullptr);
		while (t->rightChild != nullptr)
			t = t->rightChild;
		return t->data;
	}
	void Order(BSTNode<Type> *t)const
	{
		if (t != nullptr)
		{
			Order(t->leftChild);
			cout << t->data << " ";
			Order(t->rightChild);
		}
	}
	BSTNode<Type>* Find(BSTNode<Type> *t, const Type &key)
	{
		if (t == nullptr || key == t->data)
			return t;
		if (key < t->data)
			return Find(t->leftChild, key);
		else if (key > t->data)
			return Find(t->rightChild, key);
	}
	void Clear(BSTNode<Type> *&t)
	{
		if (t != nullptr)
		{
			Clear(t->leftChild);
			Clear(t->rightChild);
			delete t;
			t = nullptr;
		}
	}
	bool Remove(BSTNode<Type> *&t, const Type &key)
	{
		if (t == nullptr)
			return false;
		if (key < t->data)
			return Remove(t->leftChild, key);
		else if (key >t->data)
			return Remove(t->rightChild, key);
		else
		{
			//
			if (t->leftChild == nullptr &&t->rightChild == nullptr)
			{
				delete t;
				t = nullptr;
			}
			else if (t->leftChild != nullptr &&t->rightChild == nullptr)
			{
				BSTNode<Type> *p = t;
				t = p->leftChild;
				delete p;
			}
			else if (t->leftChild == nullptr &&t->rightChild != nullptr)
			{
				BSTNode<Type> *p = t;
				t = p->rightChild;
				delete p;
			}
			else
			{
				BSTNode<Type> *p = t->leftChild;
				while (p->rightChild != nullptr)
					p = p->rightChild;
				t->data = p->data;
				Remove(t->leftChild, p->data);
			}

			return true;
		}
	}
private:
	BSTNode<Type> *root;//头节点
};

template<typename Type>
bool BSTree<Type>::Insert(const Type &x)
{
	return Insert(root, x);
}
template<typename Type>
Type BSTree<Type>::Max()const
{
	return Max(root);
}

template<typename Type>
void BSTree<Type>::Order()const
{
	Order(root);
}

template<typename Type>
BSTNode<Type>* BSTree<Type>::Find(const Type &key)
{
	return Find(root, key);
}

template<typename Type>
void BSTree<Type>::Clear()
{
	Clear(root);
}

template<typename Type>
bool BSTree<Type>::Remove(const Type &key)
{
	return Remove(root, key);
}

二叉搜索树的应用

  1. K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。
    比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
    (1)以单词集合中的每个单词作为key,构建一棵二叉搜索树。
    (2)在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
  2. KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:
    (1)比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;
    (2)再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对。

性能分析

  • 插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
  • 最优情况下,二叉搜索树为完全二叉树,其平均比较次数为:logn
  • 最差情况下,二叉搜索树退化为单支树,其平均比较次数为:n/2
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值