当前位置:网站首页>Tree search (bintree)
Tree search (bintree)
2022-08-05 01:52:00 【domineering ocean】
介绍
We are in the usual search algorithm,The most are often sequential search and halved search,And the tree search is often half-understood,This article mainly introduces the creation of binary sorting tree,插入和查找.
树的定义
树是一种数据结构,它是由n(n≥0)个有限节点组成一个具有层次关系的集合.把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的.它具有以下的特点:
每个节点有零个或多个子节点;没有父节点的节点称为根节点;每一个非根节点有且只有一个父节点;除了根节点外,每个子节点可以分为多个不相交的子树.
If a tree contains at most two subtrees at each node, it is called a binary tree.
The program definition of a binary tree is generally as follows:
typedef char Elemtype ;
typedef struct BiNode
{
Elemtype data;
struct BiNode *lchild,*rchild;
}*BiTree;
二叉排序树的插入
添加一个值为knode to the treeT上.
int BST_inser(BiTree T,Elemtype k)
{
if(T==NULL)
{
T=(BiTree)malloc(sizeof(BiNode));
T->data=k;
T->lchild=NULL;
T->rchild=NULL;
return 1;//插入成功
}
else if(T->data==k)
{
return 0;
}
else if(T->data>k)
{
return BST_inser( T=T->lchild,k);
}
else
{
return BST_inser(T=T->rchild,k);
}
}
二叉排序树的创建
The creation of a binary sorted tree is actually to create an empty tree,Then a set of data is stored in this tree species in turn.
void Creat_BST(BiTree &T,Elemtype a[],int n)
{
T=NULL;//初始时T为空树
int i=0;
while(i<n)
{
BST_inser(T,a[i]);
i++;
}
}
树形查找
Searching a binary sorted tree is actually very simple,is the valuekfrom the root node of the sorted tree,Descending in turn,当kgreater than the current alignmentTnode value,Find this nodeT的右孩子,当ksmaller than the current alignmentTnode value,Find this nodeT的左孩子,When equal to the value of this node,Return this node.
BiNode *Bst_search(BiTree T,Elemtype k)
{
while(T!=NULL&&T->data!=k)
{
if(k<T->data)
T=T->lchild;
else
T=T->rchild;
}
return T;
}
后续
如果想了解更多物联网、智能家居项目知识,可以关注我的项目实战专栏和软硬结合专栏.
欢迎关注公众号了解更多.
编写不易,感谢支持.
边栏推荐
- 蓝牙Mesh系统开发四 ble mesh网关节点管理
- Use of pytorch: Convolutional Neural Network Module
- [GYCTF2020]EasyThinking
- Why is this problem reported when installing oracle11
- CMS website construction process
- C语言基础知识 -- 指针
- PCIe Core Configuration
- Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
- 多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
- 【Word】Word公式导出PDF后出现井号括号#()错误
猜你喜欢
随机推荐
多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
Transfer Learning - Distant Domain Transfer Learning
How DHCP works
GC高德坐标和百度坐标转换
tcp中的三次握手与四次挥手
测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
The use of pytorch: temperature prediction using neural networks
迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
2022 EdgeX中国挑战赛8月3日即将盛大开幕
没有对象的程序员如何过七夕
原生js实现多选框全部选中和取消效果
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
1349. Maximum number of students taking the exam Status Compression
【翻译】CNCF对OpenTracing项目的存档
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
[parameters of PyQT5 binding functions]
ORA-00257
方法重写与Object类
Day Fourteen & Postman









