当前位置:网站首页>Sort binary tree code
Sort binary tree code
2022-08-10 06:35:00 【The milk is still pure】
#include// #include#include#includetypedef int DataType;typedef struct BST_node{DataType data;struct BST_node *lchild,*rchild;}BST_T;BST_T* Search_BST(BST_T** root,DataType key){BST_T *p =*root;while(p){if(p->data==key) return p;p=(key < p->data)?p->lchild:p->rchild;}return NULL;}void Insert_BST(BST_T**T,DataType data){BST_T *p_bst =(BST_T*)malloc(sizeof(BST_T));if(!p_bst) return;p_bst->data =data;p_bst->lchild=p_bst->rchild=NULL;//empty treeif((*T)==NULL){*T = p_bst;return;}//Whether it exists, if it already exists, return it, do not insertif (Search_BST(T, data) != NULL) return;//insertBST_T* tnode=NULL; BST_T *troot=(*T);while(troot){tnode = troot;troot =(datadata)? troot->lchild:troot->rchild;}if(data < tnode->data)tnode->lchild =p_bst;elsetnode->rchild =p_bst;}void CreateBST(BST_T** T,int a[],int n){int i;for(i=0;ilchild);//cout << T->data << " ";printf("%d\t",T->data);MidOrderTraverse(T->rchild);}}void PostOrderTraverse(BST_T* T){if (T){PostOrderTraverse(T->lchild);PostOrderTraverse(T->rchild);printf("%d\t",T->data);}}void PreOrderTraverse(BST_T* T){if (T){printf("%d\t",T->data);PreOrderTraverse(T->lchild);PreOrderTraverse(T->rchild);}}void DeleteBSTNode(BST_T** root,DataType data){BST_T *p = *root;BST_T *parent =NULL;BST_T *s =NULL;if (!(*root)) return;if(p->data == data){// delete the leaf nodeif(p->lchild==NULL && p->rchild==NULL)*root =NULL;// only one child nodeelse if(!p->rchild&&p->lchild)*root =p->lchild;else if (!p->lchild&&p->rchild)*root =p->rchild; //To exit the recursion//The left subtree has both the right subtreeelse{s = p->rchild;//delete the node of the penultimate layerif(!s->lchild)s->lchild =p->lchild;//Delete the node from the penultimate layer upelse{while(s->lchild){parent = s;s= s->lchild;}parent->lchild =s->rchild;s->lchild =p->lchild;s->rchild = p->rchild;}(*root) = s;}free(p);}else if(data > p->data)DeleteBSTNode(&(p->rchild),data);else if(data < p->data)DeleteBSTNode(&(p->lchild),data);}int main(){int arr[] = { 17,12,19,10,15,18,25,8,11,13,16,22};BST_T *root =NULL;CreateBST(&root,arr,12);//printf("\nCreate BST: ");// printf("\nMid order traverse: ");// MidOrderTraverse(root);printf("\npre order traverse: ");PreOrderTraverse(root);// printf("\npost order traverse: ");// PostOrderTraverse(root);//find a nodeBST_T *result = Search_BST(&root, 12);printf("Search result: %d\n",result->data);//delete node 12 in the binary sorted treeDeleteBSTNode(&root, 8);printf("\npre order traverse: ");PreOrderTraverse(root);// printf("\npost order traverse: ");// PostOrderTraverse(root);// printf("\n");}
边栏推荐
- 2022河南萌新联赛第(五)场:信息工程大学 J - AC自动机
- MVCC详解
- 2022河南萌新联赛第(五)场:信息工程大学 B - 交通改造
- 2022 Henan Mengxin League No. 5: University of Information Engineering J-AC Automata
- Qt列表下方增加弹出加载数据提示效果
- 强化学习_11_Datawhale模仿学习
- UnityShader入门精要-渲染纹理 镜子 玻璃 效果
- [Reinforcement Learning] "Easy RL" - Q-learning - CliffWalking (cliff walking) code interpretation
- 如何在AdsPower中设置YiLu代理?
- 强化学习_06_pytorch-DQN实践(CartPole-v0)
猜你喜欢
随机推荐
语法基础(判断语句)
Make a boot floppy and boot with bochs emulator
mysql数据库定时备份(保留近7天的备份)
Grammar Basics (Judgment Statements)
Qt信号槽与事件循环的关系
UnityShader入门精要--Unity中的基础光照
CuteOneP is a PHP-based OneDrive multi-network disk mount program with member synchronization and other functions
2022河南萌新联赛第(五)场:信息工程大学 C - 丢手绢
The difference between initializing objects as null and empty objects in JS
COLMAP+OpenMVS实现物体三维重建mesh模型
Mysql表数据在命令行窗口下中文乱码问题解决方法
Ladies and gentlemen, oracle11g, cdc2.2, flink1.13.6, single-table incremental synchronization.Without adding data
UE 游戏模式
Reproduce dns out-band data combined with sqlmap
神经网络可视化有3D版本了,美到沦陷 已开源
I would like to ask you guys, when FLink SQL reads the source, specify the time field of the watermark. If the specified field is in the grid
order by注入与limit注入,以及宽字节注入
Hypervisor, KVM, QEMU总结
OpenGL学习笔记(LearnOpenGL)-第二部分 绘制三角形
如何正确理解线程机制中常见的I/O模型,各自主要用来解决什么问题?