查找算法之二叉查找树

一  基本概念

由于线性表的查找算法对动态操作的支持效率很低,因此将几种特殊的树或者二叉树作为表的一种组织方式。

二叉树查找树非空树的话,具有下列性质:

(1)若它的左子树非空,则左子树上所有的结点的值均小于根结点的值。

(2)若它的右子树非空,则右子树上所有的结点的值均大于根结点的值。

(3)左子树和右子树又均为一颗二叉查找树。

其实,二叉查找树的中序遍历是一个有序序列。 


二  基本操作

取二叉链表作二叉查找树的存储结构。

1.结点定义:

class node
{
public:
	node()
	{
		rightchild = nullptr;
		leftchild = nullptr;
		value = 0;
	}
	~node(){}

private:
	int value;
	node* rightchild;
	node* leftchild;
};

2.二叉树表定义:

class tree
{
	friend ostream& operator<<(ostream& out,tree& list);
public:
	tree():head(nullptr){}
	~tree(){}
private:
	node* head;
public:
	void create();   //生成二叉树表
	void find(int val); //查找
	void erase(int val);//删除
	void insert(int val);//插入
private:
	void InOrder(node* d); //中序遍历,用来辅助输出
	node* find(node* ptr, int val);
	int erasenode(node* ptr, int val);
	void insert(node*& ptr, int val);
};

3. 二叉查找树的生成:

用任意一关键字序列构造二叉查找树,其实是对这一序列数进行排序,所以二叉查找树又称为二叉排序树。

void tree::create()
{

	int num;
	cout << "输入数据" << endl;
	while (cin >> num)
	{
		if (head == nullptr)
		{
			head = new node;
			head->value = num;
		}
		else
			insert(head, num);
	}

}


4.插入算法:

(1)若二叉查找树为空,则使新插入的结点为根结点插入树中。


(2)若二叉查找树不为空,则将带插入结点的关键字值与根节点的关键字值相比较,若插入的元素值小于根节点值,则将元素插入到左子树中。若插入的元素值不小于根节点值,则将元素插入到右子树中。

void tree::insert(node*& ptr, int val)
{
	if (ptr == nullptr)
	{
		ptr = new node;
		ptr->value = val;
	}
	else
	{
		if (val > ptr->value)
			insert(ptr->rightchild, val);
		else
			insert(ptr->leftchild, val);
	}
}

5.查找算法:

(1)若二叉查找树为空,查找失败。

(2)若二叉查找树根节点的关键值与查找值相等,查找成功。

(3)若查找的元素值小于根节点值,则在左子树中查找。

(4)若插入的元素值不小于根节点值,则在右子树中查找。


(1)递归查找:

node* tree::find(node* ptr, int val)
{
	if (ptr == nullptr)        //查找失败
		return nullptr;
	else
	{
		if (ptr->value == val)  //查找成功
			return ptr;
		else
		{
			if (val > ptr->value)        
				return find(ptr->rightchild, val);    //在右子树查找
			else
				return find(ptr->leftchild, val);     //在左子树查找
		}
	}
}

(2)非递归查找

node* tree::find(node* ptr, int val)
{
	if (ptr == nullptr)           //查找失败
		return nullptr;
	else
	{
		while (ptr)           
		{

			if (ptr->value == val)     //查找成功,跳出循环
				break;
			else
			{
				if (val > ptr->value)
					ptr = ptr->rightchild;
				else
					ptr = ptr->leftchild;
			}
		}

		if (ptr == nullptr)    //查找失败
			return nullptr;
		else
			return ptr;          //查找成功,返回结点信息
	}
}

6.删除算法:

删除点p分三种情况讨论:
(1)删除结点p为叶子节点,直接删除该节点,再修改其父节点的指针(注意分是根节点和不是根节点)。
(2)删除结点p为单支节点(即只有左子树或右子树),让p的子树与p的父亲节点相连,删除p即可。
(3)删除结点p的左子树和右子树均不空,找到p的直接前驱(或直接后驱)s,利用s代替p,再删除s。(将二叉查找树中序遍历后,看到顺序序列后就知道直接前驱和直接后驱)

int tree::erasenode(node* ptr,int val)
{
	node* temp = ptr;
	node* ps = nullptr;
	while (temp)                 //查找待删除结点
	{
		if (temp->value == val)   //查找成功
			break;
		else
		{
			if (val > temp->value)
			{
				ps = temp;                 //记录父节点
				temp = temp->rightchild;
			}
			else
			{
				ps = temp;
				temp = temp->rightchild;
			}
		}
	}
	 
	if (temp == nullptr)    //没找到
		return 0;
	if (temp->rightchild == nullptr&&temp->leftchild == nullptr)  //待删除结点是叶子结点
	{
		if (temp == ptr)
			ptr = nullptr;
		else
		{
			if (temp == ps->leftchild)
				ps->leftchild = nullptr;
			else
				ps->rightchild = nullptr;
		}
	}

	else if ((temp->leftchild == nullptr) || (temp->rightchild == nullptr)) //待删除结点仅有左孩子或右孩子
	{
		if (temp == ptr)
		{
			if ((temp == ps->leftchild) && (temp->leftchild != nullptr))
			{
				ps->leftchild = temp->leftchild;
			}
			else if ((temp == ps->leftchild) && (temp->rightchild != nullptr))
			{
				ps->leftchild = temp->rightchild;
			}
			else if ((temp == ps->rightchild) && (temp->rightchild != nullptr))
			{
				ps->rightchild = temp->rightchild;
			}
			else if ((temp == ps->rightchild) && (temp->leftchild != nullptr))
			{
				ps->rightchild = temp->leftchild;
			}

		}
	}

	else if ((temp->leftchild != nullptr) && (temp->rightchild != nullptr)) //待删除结点同时有左孩子和右孩子
	{
		node* p = temp, *q = temp->leftchild;
		while(q->rightchild != nullptr)  //找到待删除结点的前驱
		{
			p = q;
			q = q->rightchild;
		}
		temp->value = q->value;         //待删除结点用直接前驱结点代替
		if (p == temp)
			temp->leftchild = q->leftchild;
		else
			p->rightchild = q->leftchild;
		delete q;                      //删除前驱结点代替待删除结点
	}

	return 1;                       //表示已找到要删除的结点,并成功删除
}

7.附件:

完整版程序:

#include<iostream>
#include<string>
using namespace std;


class node
{
	friend class tree;
public:
	node()
	{
		rightchild = nullptr;
		leftchild = nullptr;
		value = 0;
	}
	~node(){}

private:
	int value;
	node* rightchild;
	node* leftchild;
};

class tree
{
	friend ostream& operator<<(ostream& out,tree& list);
public:
	tree():head(nullptr){}
	~tree(){}
private:
	node* head;
public:
	void create();   //生成二叉树表
	void find(int val); //查找
	void erase(int val);//删除
	void insert(int val);//插入
private:
	void InOrder(node* d); //中序遍历,用来辅助输出
	node* find(node* ptr, int val);
	int erasenode(node* ptr, int val);
	void insert(node*& ptr, int val);
};
void tree::erase(int val)
{
	if (1 == erasenode(head, val))
		cout << "删除成功" << endl;
	else
		cout << "删除失败" << endl;
}
void tree::insert(int val)
{
	insert(head, val);
}
void tree::find(int val)
{
	node* temp = find(head, val);
	if (temp == nullptr)
		cout << "查找失败" << endl;
	else
		cout << "查找成功" << endl;
}

ostream& operator<<(ostream& out, tree& list)
{
	list.InOrder(list.head);
	//cout << endl;
	return out;
}

void tree::InOrder(node* d)
{
	if (d)
	{
		InOrder(d->leftchild);
		cout << d->value <<" ";
		InOrder(d->rightchild);
	}
}
void tree::create()
{

	int num;
	cout << "输入数据" << endl;
	while (cin >> num)
	{
		if (head == nullptr)
		{
			head = new node;
			head->value = num;
		}
		else
			insert(head, num);
	}

}
void tree::insert(node*& ptr, int val)
{
	if (ptr == nullptr)
	{
		ptr = new node;
		ptr->value = val;
		ptr->leftchild = nullptr;
		ptr->rightchild = nullptr;
	}
	else
	{
		if (val > ptr->value)
			insert(ptr->rightchild, val);
		else
			insert(ptr->leftchild, val);
	}
}


/*node* tree::find(node* ptr, int val)
{
	if (ptr == nullptr)        //查找失败
		return nullptr;
	else
	{
		if (ptr->value == val)  //查找成功
			return ptr;
		else
		{
			if (val > ptr->value)        
				return find(ptr->rightchild, val);    //在右子树查找
			else
				return find(ptr->leftchild, val);     //在左子树查找
		}
	}
}*/


node* tree::find(node* ptr, int val)
{
	if (ptr == nullptr)           //查找失败
		return nullptr;
	else
	{
		while (ptr)           
		{

			if (ptr->value == val)     //查找成功,跳出循环
				break;
			else
			{
				if (val > ptr->value)
					ptr = ptr->rightchild;
				else
					ptr = ptr->leftchild;
			}
		}

		if (ptr == nullptr)    //查找失败
			return nullptr;
		else
			return ptr;          //查找成功,返回结点信息
	}
}


int tree::erasenode(node* ptr,int val)
{
	node* temp = ptr;
	node* ps = nullptr;
	while (temp)                 //查找待删除结点
	{
		if (temp->value == val)   //查找成功
			break;
		else
		{
			if (val > temp->value)
			{
				ps = temp;                 //记录父节点
				temp = temp->rightchild;
			}
			else
			{
				ps = temp;
				temp = temp->rightchild;
			}
		}
	}
	 
	if (temp == nullptr)    //没找到
		return 0;
	if (temp->rightchild == nullptr&&temp->leftchild == nullptr)  //待删除结点是叶子结点
	{
		if (temp == ptr)
			ptr = nullptr;
		else
		{
			if (temp == ps->leftchild)
				ps->leftchild = nullptr;
			else
				ps->rightchild = nullptr;
		}
	}

	else if ((temp->leftchild == nullptr) || (temp->rightchild == nullptr)) //待删除结点仅有左孩子或右孩子
	{
		if (temp == ptr)
		{
			if ((temp == ps->leftchild) && (temp->leftchild != nullptr))
			{
				ps->leftchild = temp->leftchild;
			}
			else if ((temp == ps->leftchild) && (temp->rightchild != nullptr))
			{
				ps->leftchild = temp->rightchild;
			}
			else if ((temp == ps->rightchild) && (temp->rightchild != nullptr))
			{
				ps->rightchild = temp->rightchild;
			}
			else if ((temp == ps->rightchild) && (temp->leftchild != nullptr))
			{
				ps->rightchild = temp->leftchild;
			}

		}
	}

	else if ((temp->leftchild != nullptr) && (temp->rightchild != nullptr)) //待删除结点同时有左孩子和右孩子
	{
		node* p = temp, *q = temp->leftchild;
		while(q->rightchild != nullptr)  //找到待删除结点的前驱
		{
			p = q;
			q = q->rightchild;
		}
		temp->value = q->value;         //待删除结点用直接前驱结点代替
		if (p == temp)
			temp->leftchild = q->leftchild;
		else
			p->rightchild = q->leftchild;
		delete q;                      //删除前驱结点代替待删除结点
	}

	return 1;                       //表示已找到要删除的结点,并成功删除
}

int main()
{
	tree lst;
	lst.create();
	cout << lst << endl;
	lst.find(10);
	lst.erase(5);
	cout << lst << endl;

	lst.insert(18);
	cout << lst << endl;
	system("pause");
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值