二叉搜索树的根插入、选择、删除、合并、排序等操作的实现
来源:互联网 发布:utc行家 知乎 编辑:程序博客网 时间:2024/06/05 19:18
源代码如下:
这里的Key 不当为关键字对待, 而是把Item.c作为关键字对待
#include <stdlib.h>#include <stdio.h>//#define Key inttypedef int Key;struct Item{Key key;char c;};typedef struct STnode* link;struct STnode{Item item ; link l,r; int N;};static link head , z ;static struct Item NULLitem ;Key key(Item item){return item.key;}//创建一个节点 link NEW(Item item, link l,link r, int N){link x = (link)malloc(sizeof *x);x->item = item;x->l = l;x->r = r;x->N = N;if(head==z)head=x; //这句话不能少!!! return x;}//初始化 void STinit(){head = ( z = NEW(NULLitem,0,0,0));}//节点个数 int STcount(){return head->N; } //搜索子程序 Item searchR(link h, char v){char t = h->item.c;if(h==z)return NULLitem;if(v==t) return h->item;if(v<t) return searchR(h->l,v);else return searchR(h->r,v);}//搜索主程序 Item STsearch(Key v){return searchR(head,v);}//选择第K小关键值的项 K从0开始Item selectR(link h, int k) {int t;if(h==z)return NULLitem;t = (h->l==z)?0:h->l->N;if(t>k) return selectR(h->l,k);if(t<k) return selectR(h->r,k-t-1);return h->item;}Item STselect(int k){return selectR(head,k);}//----------------根插入--------------------// //右旋转 顺时针旋转 link rotR(link h){link x = h->l; h->l = x->r; x->r=h;} //左旋转 逆时针旋转 link rotL(link h){link x = h->r; h->r = x->l; x->l=h;}//插入子程序 link insertT(link h, Item item){//Key v = key(item), t = key(h->item);char v = item.c, t = h->item.c;if(h==z)return NEW(item,z,z,1);if(v<t) {h->l = insertT(h->l,item); h = rotR(h); }else {h->r = insertT(h->r,item); h = rotL(h);}(h->N)++;return h;}//BST根插入主程序 void STinsert(Item item){ //新插入的节点都会当作根节点 head = insertT(head,item);}//----------------根插入--------------------// //----------------删除一个节点----方法1-----乾坤大挪移-----------// link partR(link h, int k) {int t = h->l->N; //为什么没有判断非空? if(t>k){h->l = partR(h->l,k);h=rotR(h);} if(t<k){h->r = partR(h->r,k-t-1);h=rotL(h);} return h;}link joinLR(link a ,link b){if(b==z)return a;b=partR(b,0);b->l = a;return b;}link delR(link h, char k){link x; char t = h->item.c;if(h==z)return z;if(t>k)h->l=delR(h->l,k);if(t<k)h->r=delR(h->r,k);if(t==k){x = h;h=joinLR(h->l,h->r);free(x);}return h;}//删除主程序 void STdelete(char v){ head = delR(head,v);}//----------------删除一个节点-----方法1-----乾坤大挪移----------// //----------------删除一个节点-----方法2---------------// //删除子程序 Item deleteR(link F){Item tmp; link p;if(F->l==NULL){p = F;tmp = F->item;F = F->r;free(p);return tmp;}else return deleteR(F->l);}//删除子程序 void deleteRR(link h , Key v){if(h!=NULL){Key t = key(h->item);if(v<t) deleteRR(h->l,v);else if(v>t) deleteRR(h->r,v);elseif(h->l==NULL) { //处理只有一颗子树或没有子树的情况 1 link p = h->r; h=p; free(p);}else if(h->r==NULL){ //处理只有一颗子树或没有子树的情况 2 link p = h->l; h=p; free(p);} else h->item= deleteR(h->r); //如果待删除的节点既有左子树又有右子树//则用该节点右子树的最左下节点替换之,维持二叉搜索树 }} //删除主程序 void STdelete2(Key v){ deleteRR(head,v);}//----------------删除一个节点-----方法2---------------// void sortR(link h){if(h==z)return;sortR(h->l);if(h->item.key!=0)printf("%c ",h->item.c);sortR(h->r);}//二叉搜索树排序 void STsort(link h){sortR(h);}//接连两颗二叉搜索树 link STjoin(link a ,link b){if(b==z)return a;if(a==z)return b;b = insertT(b,a->item);b->l = STjoin(a->l,b->l);b->r=STjoin(a->r,b->r);free(a);return b;}void test(){struct Item item1 = {322,'a'};struct Item item2 = {23,'s'};struct Item item3 = {2,'e'};struct Item item4 = {332,'r'};struct Item item5 = {332,'c'};struct Item item6 = {332,'h'};struct Item item7 = {112,'i'};STinit();STinsert(item1);STinsert(item2);STinsert(item3);STinsert(item4);STinsert(item5);STinsert(item6);STinsert(item7);STsort(head);printf("\n");struct Item item11 = STselect(2); printf("%c\n",item11.c);STdelete('i');STsort(head);}main(){test();}
1 0
- 二叉搜索树的根插入、选择、删除、合并、排序等操作的实现
- 非递归实现的bst的根插入操作
- 二叉搜索树的插入、查找、删除等操作
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- 搜索(2):二叉搜索树 BST 和 根插法
- 二叉搜索树的搜索、排序、插入、删除
- 二叉堆的插入删除等操作C++实现
- 二叉搜索树(BST)的常用操作:建树、插入、查询、删除、排序打印
- 关于搜索二叉树的各种操作(插入,删除,输出,查找,递归非递归排序)
- 二叉搜索树(二叉排序数)的基本操作实现
- 二叉查找树的查找、插入、删除、释放等基本操作的实现(C语言)
- 二叉搜索(排序)树的 查找、插入、删除
- 二叉搜索树的删除
- 二叉搜索树的建立, 查找, 删除操作...
- 二叉搜索树的插入,删除,查找操作
- 二叉搜索树的查找、插入、删除操作
- 二叉搜索树的查找、添加和删除操作
- 二叉搜索树的插入与删除操作
- UITextView 监听点击了returnKey 的方法
- Java内部类模拟实现多继承
- rsa互通密钥对生成及互通加解密(c#,java,php)
- statement preparedstatement
- Alignment(POJ--1836
- 二叉搜索树的根插入、选择、删除、合并、排序等操作的实现
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
- svn 修改文件路径(修改包名)
- Havok_2014-1-0_Pc_Xs_User_Guide(3.1-Havok动画简介)
- caffe 中的损失函数分析
- C#项目中的bin目录和obj目录的区别
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
- 编写bat脚本自动转到桌面并等待输入
- 结构型模式:适配器模式(Adapter Pattern)