当前位置:网站首页>二叉排序树(C语言)
二叉排序树(C语言)
2022-07-30 00:13:00 【曼切斯特的流氓】
代码中用到了二级指针,由于如果只传入一级指针,是无法在保留方法中malloc分配的内存,方法执行结束就系统返还内存,所以需要用到二级指针,创建二叉排序树时传入二级指针,通过(*BST)的方法创建对象,一定要加括号
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;
typedef int KeyType ;
typedef struct BSTNode
{
int data;
struct BSTNode *lchild;
struct BSTNode *rchild;
} BSTNode,*BSTree;
void InOrderTraverse(BSTree T)
{
if(T!=NULL)
{
InOrderTraverse(T->lchild);
printf("%d-->",T->data);
InOrderTraverse(T->rchild);
}
}
//非递归搜索
BSTNode *BST_Search(BSTree T,ElemType key)
{
while(T!=NULL&&key!=T->data)
{
if(key<T->data)T=T->lchild;
else T=T->rchild;
}
return T;
}
//插入
bool BST_Insert(BSTree *T,KeyType k)
{
if(*T==NULL)
{
*T=(BSTree)malloc(sizeof(BSTNode));
(*T)->data=k; //二级指针必须加括号
(*T)->lchild=(*T)->rchild=NULL;
return true;
}
else if(k==(*T)->data)return false;//已经存在
else if(k<(*T)->data)return BST_Insert(&(*T)->lchild,k);
else return BST_Insert(&(*T)->rchild,k);
}
//将待删除结点的左子树最右侧结点,作为拼接结点
void Delete_Node(BSTree *p)
{
BSTree q,s;
if(!(*p)->lchild)//左子树为空
{
q=*p;
*p=(*p)->rchild;
free(q);
}
else if(!(*p)->rchild) //右子树为空
{
q=*p;
*p=(*p)->lchild;
free(q);
}
else
{
s=(*p)->lchild;
while(s->rchild)s=s->rchild;
s->rchild=(*p)->rchild;//将p的右子树,连接到s的右子树上
q=(*p);
*p=(*p)->lchild;//重新拼接
free(q);//删除当前节点
}
}
//找到左子树最右侧结点
bool BST_Delete(BSTree *T,KeyType key)
{
if(*T==NULL)return false;
else
{
if(key==(*T)->data)
{
Delete_Node(&(*T));
return true;
}
else if(key<(*T)->data)
return BST_Delete(&(*T)->lchild,key);
else return BST_Delete(&(*T)->rchild,key);
}
}
BSTree Create_BSTree(int *arr,int len)
{
BSTree T=NULL;
for(int i=0; i<len; i++)
BST_Insert(&T,arr[i]);
return T;
}
int main()
{
int num;
printf("请输入节点个数: \n");
scanf("%d",&num);
int *arr=(int*)malloc(num*sizeof(int));
printf("依次输入数据\n");
for(int i=0; i<num; i++)
scanf("%d",arr+i);
BSTree T=Create_BSTree(arr,num);
printf("中序遍历二叉排序树结果:\n");
InOrderTraverse(T);
printf("\n");
int key;
printf("请输入要查找的整数:");
scanf("%d",&key);
if(BST_Search(T,key))
printf("查找成功\n");
else
printf("查找不到该整数\n");
printf("请输入要插入的整数:");
scanf("%d",&key);
if(BST_Insert(&T,key))
{
printf("插入成功,插入后的中序遍历结果:");
InOrderTraverse(T);
printf("\n");
}
else
printf("插入失败,该二叉排序树中已经存在整数%d\n",key);
//删除给定的整数
printf("请输入要删除的整数:");
scanf("%d",&key);
if(BST_Delete(&T,key))
{
printf("删除成功,插入后的中序遍历结果:");
InOrderTraverse(T);
printf("\n");
}
else
printf("删除失败,该二叉排序树中不存在整数%d\n",key);
}
边栏推荐
- 多商户商城系统功能拆解18讲-平台端商家售后
- Unity Addressables
- The difference and usage of call, apply and bind
- Based on TNEWS 'today's headline news in Chinese short text classification
- At the age of 29, I was fired from a functional test. Can't find a job after 2 months of interviews?
- opencv基本图像的滤波
- Introduction to Worthington Elastase & Hyaluronidase
- 新媒体运营必备的4个热点查询网
- QTableWidget使用示例
- Elephant Swap:借助ePLATO提供加密市场的套利空间
猜你喜欢

MySQL 用 BETWEEN AND 日期查询包含范围边界

【分层强化学习】HAC源码解读

BEVDetNet:Bird‘s Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi

谷歌浏览器(google)设置翻译中文,翻译选项不生效或没有弹出翻译选项

29岁从事功能测试被辞,面试2个月都找不到工作吗?

rk-boot框架实战(1)

Music theory & guitar skills
![[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes](/img/07/7f19751f4eadf9fd5612f56d1936d1.png)
[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes

指令集数据产品如何设计和实现报表协同系统——基于指令集物联网操作系统的工业协同制造项目开发实践

从尾到头打印链表
随机推荐
某团实习面经
Docker install MySQL8.0
EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know
4 hotspot inquiry networks necessary for new media operations
3 tips for using hot events to create press releases?A must-see for self-media people
基于TNEWS‘ 今日头条中文新闻(短文本)分类
MySQL 用 BETWEEN AND 日期查询包含范围边界
EA&UML日拱一卒-多任务编程超入门-(8)多任务安全的数据类
利用热点事件来创作软文的3大技巧?自媒体人必看
[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes
rk-boot框架实战(1)
Music theory & guitar skills
Based on TNEWS 'today's headline news in Chinese short text classification
c语言小游戏扫雷
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
QTableWidget usage example
月薪15k的阿里测试岗,面试原来这么简单
I.MX6U-驱动开发-3-新字符驱动
Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件
Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"