(p169)构建二叉搜索树和进行相应的操作
来源:互联网 发布:java windows换行符 编辑:程序博客网 时间:2024/06/06 19:12
#include<stdio.h>#include<stdlib.h>#include<time.h>#define max 10struct node{int n;struct node *p,*l,*r;};void randomize(int n[],int size)/*将数组元素打乱*/{int i,tmp;time_t t;srand((unsigned int)time(&t));for (i=0;i<size;i++)n[i]=i;for (i=0;i<size;i++)/*装个逼,下面*几行是交换n[i]和n[tmp],注意n和tmp相等的时候会出错变成0*/{tmp=rand()%size;if (tmp!=i){n[i]=n[i]^n[tmp];n[tmp]=n[i]^n[tmp];n[i]=n[i]^n[tmp];}}}void insert(struct node **root,int n)/*为什么第一个参数要是二级指针?因为第一次插入的时候树为空,根是NULL,所以根指针会改变*/{struct node *next,*prev,*new;next=*root;prev=NULL;while (next!=NULL){prev=next;if (n<next->n)next=next->l;elsenext=next->r;}new=(struct node *)malloc(sizeof(struct node));new->n=n;if (prev==NULL)*root=new;else{if (n<prev->n)prev->l=new;elseprev->r=new;}new->p=prev;new->l=NULL;new->r=NULL;return;}void build(struct node **root,int n[],int size)/*root要是二级指针才行*/{int i;*root=NULL;for (i=0;i<size;i++)insert(root,n[i]);}struct node *search(struct node *root,int n){struct node *next=root;while (next!=NULL){if (next->n==n)return next;next=n<next->n?next->l:next->r;}return NULL;}struct node *minmum(struct node *root){struct node *next=root;while (next->l!=NULL)next=next->l;return next;}struct node *maxmum(struct node *root){struct node *next=root;while (next->r!=NULL)next=next->r;return next;}struct node *successor(struct node *root){struct node *next=root->p,*prev=root;if (root->r!=NULL)return minmum(root->r);else{while(next!=NULL){if (next->l==prev)return next;prev=next;next=next->p;}return next;}}struct node *predecessor(struct node *root){struct node *next=root->p,*prev=root;if (root->l!=NULL)return maxmum(root->l);else{while(next!=NULL){if (next->r==prev)return next;prev=next;next=next->p;}return next;}}void transplant(struct node **root,struct node *a,struct node *b)/*b子树移到a子树的位置,参数root的功能是为了修改root*/{if (a->p==NULL)/*注意首先要判断是不是空指针,这里也可以不改变根指针,不过这样的话就得在Delete里加好几行代码*/*root=b;elseif (a==a->p->l)a->p->l=b;elsea->p->r=b;if (b!=NULL)/*也要判断是不是空指针*/b->p=a->p;}void Delete(struct node **root,struct node *target)/*同样的root可能会改变,因此用二级指针*/{struct node *tmp;if (target==NULL)return;if (target->l==NULL)transplant(root,target,target->r);elseif (target->r==NULL)transplant(root,target,target->l);elseif ((tmp=successor(target))==target->r){target->l->p=tmp;tmp->l=target->l;transplant(root,target,tmp);}else{transplant(root,tmp,tmp->r);tmp->l=target->l;tmp->l->p=tmp;tmp->r=target->r;tmp->r->p=tmp;transplant(root,target,tmp);}free(target);}void print(struct node *root)/*以先序遍历方式输出*/{if (root==NULL){printf("# ");return;}printf("%d ",root->n);print(root->l);print(root->r);}int main(void){int n[max];randomize(n,max);struct node **root;root=(struct node**)malloc(sizeof(struct node *));build(root,n,max);print(*root);printf("\n");Delete(root,*root);print(*root);printf("\n");}
0 0
- (p169)构建二叉搜索树和进行相应的操作
- 二叉树的搜索和插入操作
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
- 搜索二叉树的操作
- 二叉搜索树的操作
- 二叉搜索树的操作
- 二叉搜索树的操作
- BinaryTree的构建和遍历,以及搜索删除(非完全二叉树,非平衡二叉树)
- 二叉搜索建树及相应的函数
- 二分法和二叉树进行搜索
- 二叉搜索树的查找、添加和删除操作
- 二叉搜索树的建树和中序遍历操作
- 【二叉树】二叉搜索树与完全二叉树的创建和操作
- 【二叉搜索树】二叉搜索树的基本操作
- 二叉树的建立(根据遍历结果构建)、遍历(非递归)和搜索
- 二叉搜索树操作
- 二叉树学习之二叉树的构建及操作
- 二叉搜索树(BST)的基本操作
- Xcode 7 缺少 *.dylib库的解决方法
- Android性能优化典范(四)
- uva825
- netctoss01_项目概述及规范【Servlet+JSP版】
- light OJ 1140
- (p169)构建二叉搜索树和进行相应的操作
- Sql数据库查询语言
- Android 操作系统的内存回收机制
- tomcat一直处于deploying状态。
- PAT 1005
- 关于php线程安全的一些东西
- 蓝桥杯 历届试题 饮料换购
- 最坏情况为线性时间的选择算法
- NYOJ——题目44:子串和