当前位置:网站首页>二叉排序树的查找、插入和删除
二叉排序树的查找、插入和删除
2022-06-22 18:28:00 【单单一个越字】
• 二叉排序树(Binary Sort Tree)又称为二叉查找树,它或者是一棵空树,或者是具有下列性质的二叉树:
– 若它的左子树不为空,则左子树上所有结点的值均小于它的根节点的值;
– 若它的右子树不为空,则右子树上所有结点的值均大于它的根节点的值;
– 它的左、右子树也分别为二叉排序树(递归)。

二叉排序树用中序遍历之后即为顺序数组;
查找:
// 二叉树的二叉链表结点结构定义
typedef struct BiTNode
{
int data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
// 递归查找二叉排序树 T 中是否存在 key
// 指针 f 指向 T 的双亲,其初始值调用值为 NULL
// 若查找成功,则指针 p 指向该数据元素结点,并返回 TRUE
// 否则指针 p 指向查找路径上访问的最后一个结点,并返回 FALSE
Status SearchBST(BiTree T, int key, BiTree f, BiTree *p)
{
if( !T ) // 查找不成功
{
*p = f;
return FALSE;
}
else if( key == T->data ) // 查找成功
{
*p = T;
return TRUE;
}
else if( key < T->data )
{
return SearchBST( T->lchild, key, T, p ); // 在左子树继续查找
}
else
{
return SearchBST( T->rchild, key, T, p ); // 在右子树继续查找
}
}
插入:
// 当二叉排序树 T 中不存在关键字等于 key 的数据元素时,
// 插入 key 并返回 TRUE,否则返回 FALSE
Status InsertBST(BiTree *T, int key)
{
BiTree p, s;
if( !SearchBST(*T, key, NULL, &p) )
{
s = (BiTree)malloc(sizeof(BiTNode));
s->data = key;
s->lchild = s->rchild = NULL;
if( !p )
{
*T = s; // 插入 s 为新的根结点
}
else if( key < p->data )
{
p->lchild = s; // 插入 s 为左孩子
}
else
{
p->rchild = s; // 插入 s 为右孩子
}
return TRUE;
}
else
{
return FALSE; // 树中已有关键字相同的结点,不再插入
}
}
删除:
Status DeleteBST(BiTree *T, int key)
{
if( !*T )
{
return FALSE;
}
else
{
if( key == (*T)->data )
{
return Delete(T);
}
else if( key < (*T)->data )
{
return DeleteBST(&(*T)->lchild, key);
}
else
{
return DeleteBST(&(*T)->rchild, key);
}
}
}
Status Delete(BiTree *p)
{
BiTree q, s;
if( (*p)->rchild == NULL ) //需要删除的节点没有右子树时,直接用该节点的左子树覆盖该节点
{
q = *p;//q用来记录需要删除的节点
*p = (*p)->lchild;
free(q);
}
else if( (*p)->lchild == NULL )//需要删除的节点没有左子树时,直接用该节点的左子树覆盖该节点
{
q = *p;
*p = (*p)->rchild;
free(q);
}
else//需要删除的节点既有左子树又有右子树时,可用其前驱节点A的值替换该节点的值,并用其前驱节点A的左子树覆盖节点A
{
q = *p; //q用来记录其前驱节点的双亲节点
s = (*p)->lchild;
while( s->rchild )
{
q = s;
s = s->rchild;
}
(*p)->data = s->data;
if( q != *p )
{
q->rchild = s->lchild;
}
else
{
q->lchild = s->lchild;
}
free(s);
}
return TRUE;
}
边栏推荐
- Pat a 1093 count Pat's (25 points)
- Teachers, I want to ask you a question. I run flinkcdc locally to synchronize MySQL data. The timestamp field parsing is normal,
- 使用 qrcodejs2 生成二维码详细API和参数
- Solution de pin hors grille dans altium designer
- 元宇宙怎么就这么火,市场喊起来的10万亿是吹嘘还是真相?
- 创建者模式大汇总
- delegate
- vs2008 水晶报表升级到 vs2013对应版本
- C #, introductory tutorial -- a little knowledge about function parameter ref and source program
- 使用 Order by 与 rownum SQL 优化案例一则
猜你喜欢

Digital business cloud: build a digital supply chain system to enable enterprises to optimize and upgrade their logistics supply chain

技术管理进阶——你了解成长的全貌吗?

Wavelet transform DB4 for four layer decomposition and signal reconstruction matlab analysis and C language implementation

ActiveReports报表实战应用教程(十九)——多数据源绑定

1.4-----PCB设计?(电路设计)确定方案

使用 qrcodejs2 生成二维码详细API和参数

记可视化项目代码设计的心路历程以及理解

84. (cesium chapter) movement of cesium model on terrain

Initial experience of ABAQUS using RSG drawing plug-in

Explain in simple terms the bloom filter
随机推荐
vim中快速缩进用法
数字货币钱包开发不知道怎么选?
Download files through Base64 (convert Base64 to BLOB)
Longest common subsequence
Screw database document generator
K8s deploy MySQL
How to judge whether text is an array in the slot
详解openGauss多线程架构启动过程
ABAQUS 使用RSG绘制插件初体验
结构型模式之代理模式
取zip包中的文件名
Input two strings and output the longest substring with the same length
0.1-----用AD画PCB的流程
K个一组翻转链表[链表拆解/翻转/拼装]
08_一句话让你人间清醒
The custom control autoscalemode causes the problem of increasing the width of font
1.4-----PCB设计?(电路设计)确定方案
Teachers, I want to ask you a question. I run flinkcdc locally to synchronize MySQL data. The timestamp field parsing is normal,
Openpnp使用过程的一些问题记录
华为云招募工业智能领域合作伙伴,强力扶持+商业变现