二叉排序树

1.二叉排序树定义

(1)若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
例如这样一组数据3, 5, 1, 7, 2。它的有它构成的排序树为:

在这里插入图片描述

2.二叉排序树操作(举例为左小右大)

(1)插入:根据二叉树的性质,使用指针和循环或者使用递归寻找到相应位置插入节点。
(2)删除:对于二叉排序树的每个结点,它的左子树上的元素都小于该结点上的元素 ,右子树上的元素都大于该结点上的元素,根据这个性质。
1.如果要删的这个结点是叶子结点的话就直接删除,
2.如果这个结点的左树或右树为空,则把该结点不为空的那边的树的第一个结点的地址给它,相当于把不为空的那边的树拖上来。
3.如果这个结点的左树或右树都不空,根据二叉排序树的性质:所以我们有两种解决思路。
第一种:找到其左子树中的最大的那个结点,把它放在要删的那个位置,然后删掉那个左子树中最大的结点
第二种:或者找到其右子树中的最小的那个结点,把它放在要删的那个位置,然后删掉那个右子树中最小的结点。

(1).如果要删的这个结点是叶子结点的话就直接删除,如下图删除7.
在这里插入图片描述
(2)如果这个结点的左树或右树为空,则把该结点不为空的那边的树的第一个结点的地址给它,相当于把不为空的那边的树拖上来,如果不为空的那边的树的节点只有一个节点,就相当于上图;

(3)如果这个结点的左树或右树都不空,根据二叉排序树的性质:所以我们有两种解决思路。
第一种:找到其左子树中的最大的那个结点,把它放在要删的那个位置,然后删掉那个左子树中最大的结点
第二种:或者找到其右子树中的最小的那个结点,把它放在要删的那个位置,然后删掉那个右子树中最小的结点。

如果我们要删除3。
第一种:用2替换3
在这里插入图片描述
第二种:用5替换3
在这里插入图片描述
3.遍历
由二排序树的定义可知,左子树的所有值均小于根节点,右子树的所有值均大于根节点,而这个特点正符合二叉树的中序遍历即左子树->根节点->右子树不谋而合,所以对二叉排序树进行中序遍历得到的正好是一个有序的序列。
4.求最大最小值
取排序树的最左端或者最右端。

3. 代码实现:

#include <iostream>
using namespace std;

typedef struct Node
{
	int data;
	struct Node *lchild, *rchild;
}*BiTree;

//向一个二叉排序树内插入元素
void EnterItem(BiTree root, int key);

//把一组无序数据构造为二叉排序树
void Sort(BiTree root, int data[], int length);

//寻找排序树的最大值
int max(BiTree root);

//寻找排序树的最小值
int min(BiTree root);

//删除排序树的结点
bool DeleteItem(BiTree &root, int item);

//求树的深度
int depth(BiTree root);


//向一个二叉排序树内插入元素
void EnterItem(BiTree root, int key)
{
	if (root)
	{
		BiTree newnode = (BiTree)malloc(sizeof(Node));
		newnode->data = key;
		newnode->lchild = newnode->rchild = NULL;
		BiTree head = root;
		BiTree parent = root;
		while (head)
		{
			parent = head;
			if (head->data > key)
			{
				head = head->lchild;
			}
			else
			{
				head = head->rchild;
			}
		}
		if (parent->data < key)
		{
			parent->rchild = newnode;
		}
		else
		{
			parent->lchild = newnode;
		}
	}
	else
	{
		cout << "根结点为空";
	}	
}

//递归实现向一个二叉排序树内插入元素
BiTree BFSEnterItem(BiTree root, int key)
{
	if (!root)
	{
		BiTree newnode = (BiTree)malloc(sizeof(Node));
		newnode->data = key;
		newnode->lchild = newnode->rchild = NULL;
		return newnode;
	}
	if (key < root->data)
		root->lchild = BFSEnterItem(root->lchild, key);
	else
		root->rchild = BFSEnterItem(root->rchild, key);

	return root;
}

//把一组无序数据构造为二叉排序树
void Sort(BiTree root,int data[],int length)
{	
	for (int i = 1; i < length; i++)
	{		
		int key = data[i];//基准数
		EnterItem(root, key);
		
	}	
}

int max(BiTree root)
{
	BiTree parent=root;
	//大的元素在排序树的右端
	while (root)
	{
		parent = root;
		root = root->rchild;
	}
	return parent->data;
}

int min(BiTree root)
{
	BiTree parent = root;
	//大的元素在排序树的左端
	while (root)
	{
		parent = root;
		root = root->lchild;
	}
	return parent->data;
}

int depth(BiTree root)
{
	if (root==NULL)
	{
		return 0;
	}
	else
	{//每层比较取大值加1;
		int left = depth(root->lchild);
		int right = depth(root->rchild);
		return left > right ? left + 1 : right + 1;
	}
	
}

//删除排序树的结点的思路:对于二叉排序树的每个结点,它的左子树上的元素都小于该结点上的元素 ,
//右子树上的元素都大于该结点上的元素 。
//1.如果要删的这个结点是叶子结点的话就直接删除,
//2.如果这个结点的左树或右树为空,则把该结点不为空的那边的树的第一个结点的地址给它,
//3.如果这个结点的左树或右树都不空,根据二叉排序树的性质:所以我们有两种解决思路。
//第一种:找到其左子树中的最大的那个结点,把它放在要删的那个位置,然后删掉那个左子树中最大的结点
//第二种:或者找到其右子树中的最小的那个结点,把它放在要删的那个位置,然后删掉那个右子树中最小的结点
bool DeleteItem(BiTree &root, int item)//注意要传入地址的引用进去才可以改变该地址
{
	if (root==NULL)
	{
		return false;
	}
	else if(root->data> item)
	{
		DeleteItem(root->lchild, item);
	}
	else if (root->data<item)
	{
		DeleteItem(root->rchild, item);
	}
	else
	{
		//如果这个结点的左树或右树都不空,就使用两种解决方案的其中一种(这里使用了第一种)
		if (root->lchild!=NULL&&root->rchild!= NULL)
		{
			   BiTree temp = root->lchild;
				while (temp->rchild!=NULL)
		        {
				 temp = temp->rchild;
		        }
				root->data = temp->data;
				DeleteItem(root->lchild, temp->data);
		}
		else
		{//否则就是第1,2种情况			
			if (root->lchild!=NULL)
			{
				root = root->lchild;
			}
			else
			{
				root = root->rchild;
			}
		}
		return true;
	}
}

void ZhongPrintf(BiTree root)
{
	if (root)
	{
		ZhongPrintf(root->lchild);
		cout << root->data;
		ZhongPrintf(root->rchild);
		
	}	
}

int main()
{
	int data[]={3, 5, 1, 7, 2};
	BiTree root;
	root = (BiTree)malloc(sizeof(Node));
	root->lchild = root->rchild = NULL;
	root->data = data[0];	
	Sort(root,data,5);
	ZhongPrintf(root);
	cout << "\n";
	cout <<"min:"<< min(root);
	cout << "\n";
	cout << "max:" << max(root);
	cout << "\n";
	cout << "深度:" << depth(root);
	DeleteItem(root,3);
	DeleteItem(root, 7);
	cout << "\n";
	cout << "深度:" << depth(root);
	cout << "\n";
	ZhongPrintf(root);
	return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值