二叉搜索树(BST)

这篇博客详细介绍了二叉搜索树(BST)的基本概念和特性,包括如何创建、查找、插入、删除节点,以及如何判断一棵树是否为有效的BST。提供了递归和非递归两种方法来查找节点,并实现BST的插入和删除操作。此外,还包含了找到BST中最小、最大、前驱和后继节点的方法,这些操作对于理解和操作BST至关重要。
摘要由CSDN通过智能技术生成

BST树有如下特点:

1、若它的左子树不为空,则左子树上所有的节点值都小于它的根节点值。

2、若它的右子树不为空,则右子树上所有的节点值均大于它的根节点值。

3、它的左右子树也分别可以充当为二叉查找树。

定义节点

#include<iostream>
#include<vector>
#include<stack>
using namespace std;
typedef int KeyType;
typedef struct BstNode
{
	KeyType key;
	BstNode* leftchild;
	BstNode* parent;
	BstNode* rightchild;
}BstNode, * BSTree;

创建新节点

BstNode* Buynode(KeyType kx)
{
	BstNode* s = (BstNode*)malloc(sizeof(BstNode));
	if (nullptr == s) exit(1);
	memset(s, 0, sizeof(BstNode));
	s->key = kx;
	return s;
}

释放节点

void Freenode(BstNode* p)
{
	free(p);
}

递归方法查找值

BstNode* SearchValue(BstNode* ptr, KeyType kx)
{
	if (ptr == nullptr || ptr->key == kx)
		return ptr;
	else if (kx < ptr->key)
		return SearchValue(ptr->leftchild, kx);
	else
		return SearchValue(ptr->rightchild, kx);
}

非递归方法查找值

BstNode* FindValue(BstNode* ptr, KeyType kx)
{
	while (ptr != nullptr && ptr->key != kx)
	{
		ptr = kx < ptr->key ? ptr->leftchild : ptr->rightchild;
	}
	return ptr;	
}

插入新数据

bool Insert(BstNode *&ptr,KeyType kx)
{
	if (ptr == nullptr)
	{
		ptr = Buynode(kx);
		return true;
	}
	BstNode* pa = nullptr;
	BstNode* p = ptr;
	while (p != nullptr && p->key != kx)
	{
		pa = p;
		p = kx < p->key ? p->leftchild : p->rightchild;
	}
	if (p != nullptr && p->key == kx) return false;
	p = Buynode(kx);
	p->parent = pa;
	if (p->key < pa->key)
	{
		pa->leftchild = p;
	}
	else
	{
		pa->rightchild = p;
	}
	return true;
}

判断是否是BST

bool Is_BST(BstNode* ptr)
{
	bool res = true;
	if (ptr == nullptr) return res;
	BstNode* pre = nullptr;
	std::stack<BstNode*> st;
	while (ptr != nullptr || !st.empty())
	{
		while (ptr != nullptr)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
		}
		ptr = st.top(); st.pop();
		if (pre != nullptr && pre->key >= ptr->key)
		{
			res = false;
			break;
		}
		pre = ptr;
		ptr = ptr->rightchild;
	}
	return res;
}

删除数据

BstNode* Next(BstNode* ptr)
{
	while (ptr != nullptr && ptr->leftchild != nullptr)
	{
		ptr = ptr->leftchild;
	}
	return ptr;
}
bool Remove(BstNode*& ptr, KeyType kx)
{
	bool res = false;
	if (ptr == nullptr) return res;
	BstNode* p = ptr;
	while (p != nullptr && p->key != kx)
	{
		p = kx < p->key ? p->leftchild : p->rightchild;
	}
	if (p == nullptr) return false;
	if (p->leftchild != nullptr && p->rightchild != nullptr)
	{
		BstNode* nt = Next(p->rightchild);
		p->key = nt->key;
		p = nt;
	}
	BstNode* pa = p->parent;

	BstNode* child = p->leftchild != nullptr ? p->leftchild : p->rightchild;
	if (child != nullptr) child->parent = pa;

	if (pa == nullptr)
	{
		ptr = child;
	}
	else
	{
		if (pa->leftchild == p)
		{
			pa->leftchild = child;
		}
		else
		{
			pa->rightchild = child;
		}
	}
	Freenode(p); p = nullptr;
	return true;
}

寻找最小节点

BstNode* First(BstNode* ptr)  // Min
{
	while (ptr != nullptr && ptr->leftchild != nullptr)
	{
		ptr = ptr->leftchild;
	}
	return ptr;
}

寻找最大节点

BstNode* Last(BstNode* ptr)	// Max
{
	while (ptr != nullptr && ptr->rightchild != nullptr)
	{
		ptr = ptr->rightchild;
	}
	return ptr;
}

寻找直接前驱节点

BstNode* Prev(BstNode* ptr)
{
	if (ptr == nullptr) return nullptr;
	if (ptr->leftchild != nullptr)
	{
		return Last(ptr->leftchild);
	}
	else
	{
		BstNode* pa = ptr->parent;
		while (pa != nullptr && pa->rightchild != ptr)
		{
			ptr = pa;
			pa = ptr->parent;
		}
		return pa;
	}
}

寻找直接后续节点
迭代器++ 实现原理

BstNode* Next(BstNode* ptr)
{
	if (ptr == nullptr) return nullptr;
	if (ptr->rightchild != nullptr)
	{
		return First(ptr->rightchild);
	}
	else
	{
		BstNode* pa = ptr->parent;
		while (pa != nullptr && pa->leftchild != ptr)
		{
			ptr = pa;
			pa = ptr->parent;
		}
		return pa;
	}
}

图论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值