当前位置:网站首页>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");} 边栏推荐
- C语言文件操作
- Everyone, the default configuration of oracle cdc occasionally takes 30 seconds to capture data. How to optimize this?
- 第11章 数据库的设计规范【2.索引及调优篇】【MySQL高级】
- 动态代理-cglib
- 761. Special Binary Sequences
- 数据库学习之数据类型
- 2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
- JS中初始化对象为null和空对象的区别
- Text-to-Image最新论文、代码汇总
- 修改 QtCreator 配置解决 “无法运行 rc.exe” 问题
猜你喜欢
随机推荐
DRM Memory Management
3.事务篇【mysql高级】
QScroller的QScrollerProperties参数研究
求职
OSPF的dr和bdr
椭圆曲线离散对数问题以及求解
关于研究鼠标绘制平滑曲线的阶段总结
Unity3d famous project-Dark Tree translation
交换机的功能和ipv4
UnityShader入门精要-基础纹理
Kernel Image File Format
Reproduce dns out-band data combined with sqlmap
Qt信号槽与事件循环的关系
UnityShader入门精要-阴影
2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
C language file operation
UnityShader入门精要--Unity中的基础光照
强化学习_11_Datawhale模仿学习
修改 QtCreator 配置解决 “无法运行 rc.exe” 问题
vscode + ccls环境配置









