数据结构算法
来源:互联网 发布:雅思考试推荐用书 知乎 编辑:程序博客网 时间:2024/05/16 07:01
二叉排序树的非递归删除算法
bool Remove(BSTree &root,DataType x){//二叉排序树的非递归删除算法 //在root为根的二叉排序树中删除关键字值为 x 的结点,成功返回true,失败返回false BSTNode *s,*p,*f; p=Search(root,x,f);//寻找删除结点 if(p==NULL) return false;//查找失败,不作删除 if(p->lchild!=NULL&&p->rchild!=NULL) { s=p->lchild;//找 p 的中序前驱 s while(s->rchild!=NULL) { f=s; s=s->rchild; } p->data=s->data; p=s; } if(p->lchild!=NULL) s=p->lchild;//记录非空子女结点 else s=p->rchild; if(p==root) root=s;//被删结点为根结点 else if(s->data<f->data) f->lchild=s; else f->rchild=s; free(p);//释放被删结点 return true;}
void InsertSort(DataType l[],int left,int right){//直接插入排序 DataType temp; int i,j; for(i=left+1;i<right;i++) if(l[i]<l[i-1]) { temp=l[i]; j=i-1; do{ l[j+1]=l[j]; j--; }while(j>=left&&temp<l[j]); l[j+1]=temp; }} void BinaryInsertSort(DataType l[],int left,int right){//折半插入排序 DataType temp; int i,j,low,high,middle; for(i=left+1;i<=right;i++) { temp=l[i]; low=left; hight=i-1; while(low<=high) { middle=(low+hight)/2; if(temp<l[middle]) high=middle-1; else low=middle+1; } for(j=i-1;j>=low;j--) l[j+1]=l[j]; l[low]=temp; }} void ShellSort(DataType l[],int left,int right){//希尔排序 DataType temp; int i,j,gap=right-left+1; do{ gap=gap/3+1; for(i=left+gap;i<=right;i++) if(l[i]<l[i-gap]) { temp=l[i]; j=i-gap; do{ l[j+gap]=l[j]; j=j-gap; }while(j>left&&temp<l[j]); l[j+gap]=temp; } }while(gap>1);} void BubbleSort(DataType l[],int left,int right){//气泡排序 bool exchangel; DataType temp; int i,j; for(i=left;i<right;i++) { exchange=false; for(j=right;j>=i+1;j--) if(l[j-1]>l[j]) { temp=l[j-1]; l[j-1]=l[j]; l[j]=temp; exchange=true; } if(exchange==false)return; }} void QuickSort(DataType l[],int left,int right){//快速排序 if(left<right) { int pivotpos=Partition(left,right); QuickSort(l,left,pivotpos-1); QuickSort(l,pivotpos+1,right); }}int Partition(DataType l[],int low,int high){ int l=low,j=high; DataType pivot=l[low]; while(i<j) { while(i<j&&l[j]>=pivot) j--; if(i<j) l[i++]=l[j]; while(i<j&&l[i]<=pivot) i++; if(i<j) l[j--]=l[i]; } l[i]=pivot; return i;} void SelectSort(DataType l[],int left,int right){//选择排序 DataType temp; int i,j,k; for(i=left;i<right;i++) { k=i; for(j=i+1;i<right;i++) if(l[j]<l[k]) k=j; if(k!=i) { temp=l[i]; l[i]=l[k]; l[k]=temp; } }} void Merge(DataType l1[],DataType l2[],int left,int mid,int right){ for(int k=left;k<=right;k++) l2[k]=l1[k]; int s1=left,s2=mid+1,t=left; while(s1<=mid&&s2<=right) if(l2[s1]<=l2[s2]) l1[t++]=l2[s1++]; else l1[t++]=l2[s2++]; while(s1<=mid) l1[t++]=l2[s1++]; while(s2<=right)l1[t++]=l2[s2++];} int Width(BinTree bt){//用层次遍历求最大宽度 if(bt==NULL) return 0; Queue Q; front=rear=last=1; temp=maxw=0; Q[rear]=bt; while(front<=last) { p=Q[front++]; temp++; if(p->lchild!=NULL) Q[++rear]=p->lchild; if(p->rchild!=NULL) Q[++rear]=p->rchild; if(front>last) { last=rear; if(temp>maxw) maxw=temp; temp=0; } }} int JudgeComplete(BinTree bt){//判断完全二叉树 int tag=0; BinTree p=bt; Queue Q; if(p==NULL) return 1; Enqueue(Q,P); while(!QueueEmpty(Q)) { p=Dequeue(Q); if(p->lchild&&!tag) Enqueue(Q,p->lchild); else if(p->lchild) return 0; else tag=1; if(p->rchild&&!tag) Enqueue(Q,p->rchild); else if(p->rchild) return 0; else tag=1; } return 1;} void BFST Traversec(Graph G){//广度优先遍历图 for(v=0;v<G.vexnum;++v) visited[v]=false; InitQueue(Q); for(v=0;v<G.vexnum;++v) { if(!visited[v]) { visited[v]=true; visit(v); Enqueue(Q,v); while(!QueueEmpty(Q)) { Dequeue(Q,u); for(w=FirstAdjvex(G,u);w;w=NextAdjvex(G,u,w)) { if(!visited[w]) { visited[w]=true; visit(w); Enqueue(Q,w); } } } } }} void DFSTraverse(Gruph G){//图的深度优先遍历 for(v=0;v<G.vexnum;v++) visited[v]=false; for(v=0;v<G.vexnum;++v) { if(!visited[v]) DFS(G,v); }}void DFS(Graph G,int v){ visited[v]=true; visit(v); for(w<FirstAdjvex(G,v);w;w=NextAdjvex(G,v,w)) { if(!visited[w]) DFS(G,w); }}或者void DFS(Graph &G,int v,bool visited[]){ visit(getValue(g,v)); visited[v]=true; int w=getFirstNeighbor(G,v); while(w!=-1) { if(visited[w]==false) DFS(G,w,visited); w=getNextNeighbor(G,v,w); }} status InOrderThreading(BinThrTree &Thrt,BinThrTree T){ //中序遍历二叉树T,并将其中序线索化,Thrt指向头节点 if(!(Thrt=(BinThrTree)malloc(sizeof(BinThrNode)))) exit(OVERFLOW); Thrt->ltag=link; Thrt->rtag=Thread; Thrt->rchild=Thrt; if(!T) Thrt->lchild=Thrt; else { Thrt->lchild=T; pre=Thrt; InThreading(T); pre->rchild=Thrt; pre->rtag=Thread; Thrt->rchild=pre; } return OK;}void InThreading(BinThrTree p){ if(p) { InThreading(p->lchild); if(!p->lchild) { p->ltag=Thread; p->lchild=pre; } if(!pre->rchild) { pre->rtag=Thread; p->rchild=p; } pre=p; InThreading(p->rchild); }} status InOrder-Thr(BinThrTree T,status(*visit)(ElemType e)){//中序遍历线索二叉树 p=T->lchild; while(p!=T) { while(p>ltag==link) { p=p->lchild; } if(!(visit(p->data))) return ERROR; while(p->rtag==Thread&&p->rchild!=T) { p=p->rchlid; visit(p->data); } p=p->rchild; }} int depth(BinTree T){//求二叉树的深度 if(T==NULL) return 0; else { if((ldepth=depth(t.lchild))>(rdepth=depth(T.rchild))) return ldepth+1; else return rdepth+1; }} void LevelOrder(BinTree T){//二叉树层次遍历 Queue Q; BinTree bt; if(T!=NULL) { visit(T); if(T->lchild!=NULL) Enqueue(Q,T->lchild); if(T->rchild!=NULL) Enqueue(Q,T->rchild); } bt=Dequeue(Q); LevelOrder(bt);} //利用两个栈S1,S2模拟队列status Enqueue(stack S1,ElemType e){ if(top1==n&&!Sempty(S2)) { printf("栈满"); return ERROR; } if(top1==n&&Sempty(S2)) { while(!Sempty(S1)) { pop(S1,e); push(S2,e); } } push(S1,e); return OK;}void Dequeue(stack S1,stack S2){ if(!Sempty(S2)) { pop(S2,e); printt("出队元素为:",e); } else { if(Sempty(S1)) { printf(""队列空"); exit(0); } else { while(!Sempty(S1)) { pop(S1,e); push(S2,e); } pop(S2,e); printf("出队列元素为:",e); } }} int size(BinTreeNode *t){//求二叉树节点个数 if(t==NULL) return 0; return 1+size(t->lchild)+size(t->rchild);}int leaves(BinTreeNode *t){//返回二叉树的叶子节点个数 if(t==NULL) return 0; if(t->lchild==NULL&&t->rchild==NULL) return 1; return leaves(t->lchild)+leaves(t->rchild);}int height(BinTree *t){//返回二叉树的高度 if(t==NULL) return 0; int hl=height(t->lchild); int hr=height(t->rchild); if(hl>hr) return hl+1; else return hr+1;}int level(BinTreeNode* t,BinTreeNode* p){//返回以*t为根的二叉树中指定节点*p所在层次 if(t==NULL) return 0; if(t==p) return 1; int levell=level(t->lchild,p); if(level>0) return 1+levell; int levelr=level(t->rchlild,p); if(levelr>0) return 1+levelr;return 0;}void reflect(BinTreeNode *t){//交换以为*t根的二叉树中每一个节点的两个子女 if(t==NULL) return; reflect(t->lchild); reflect(t->rchild); BinTreeNode *P=t->lchild; t->lchild=t->rchild; t->rchild=p;}void defoliate(BinTreeNode *t){//从以*t为根的二叉树中删去所有叶子节点 if(t==NULL) return; if(t->lchild==NULL&&t->rchild==NULL) { delete t; t=NULL; } else { defoliate(t->lchild); defoliate(t->rchild); }}int Degree1(BinTreeNode *t){//统计以*t为节点的二叉树中度为1的节点个数,有此可推出求度数为2的节点个数 if(t==NULL) return 0; if(t->lchild!=NULL&&t->rchild==NULL||t->rchild!=NULL&&t->lchild==NULL) return 1+Degree1(t->lchild)+Degree1(t->rchild);}//统计以*t为根的二叉树的宽度:先用前序遍历求出每一层的宽度,再求最大宽度int levelNumber(BinTreeNode *t,int a[],int level){ if(t!=NULL) { a[level]+=1; levelNumber(t->lchild,a,level+1); levelNumber(t->rchild,a,level+1); }}int width(BinTreeNode *t){ int a[n+1],level=0,i,wid; for(i=0;i<=n;i++) a[i]=0; levelNumber(t,a,level); wid=a[0]; for(i=1;i<=n;i++) if(a[i]>wid) wid=a[i]; return wid;}DataType MaxValue(BinTree *t,DataType max){//计算以*t为根的二叉树节点中最大元素的值 if(t!=NULL) { if(t->data>max) max=t->data; max=MaxValue(t->lchild,max); max=MaxValue(t->rchild,max); } return max;}void nodePrint(BinTreeNode *t,int i){//以前序输出一棵以为根的二叉树所有节点,数据值以及所在层次 if(t!=NULL) { cout<<t->data<<","<<i<<endl; nodePrint(t->lchild,i+1); nodePrint(t->rchild,i+1); }}void ConstructTree(DataType T[],int n,int i,BinTreeNode *ptr){//一棵完全二叉树存在一个一维数组T[n]中,建立一棵链接二叉树与之对应 if(i>=n) ptr=NULL; else { ptr=(BinTreeNode *)malloc(sizeof(BinTreeNode)); ptr->data=T[i]; ConstructTree(T,n,i*2+1,ptr->lchild); ConstructTree(T,n,i*2+2,ptr->rchild); }}oid InOrder(BinTree bt){ //二叉树链接实现,非递归中序遍历 Stack S; p=bt; while(p!=NULL||!isEmpty(S)); { while(p!=NULL) { push(S,p); p=p->lchild; } if(!isEmpty(S)) { pop(S,p); Visit(p->data); p=p->rchild; } }}bool Insert(BSTree &root,DataType x){//二叉排序树非递归插入算法 BSTNode *s,*p,*f; p=Search(root,x,f);//f指向查找节点父节点 if(p!=NULL) return false; s=(BSTNode*)malloc(sizeof(BSTNode)); if(s==NULL) return false; s->data=x; s->lchild=NULL; s->rchild=NULL; if(f==NULL) root=s; else if(x<f->data) f->lchild=s; else f->rchild=s; return true;}bool Remove(BSTree &root,DataType x){//二叉排序树的非递归删除算法 BSTNode *s,*p,*f; p=Search(root,x,f); if(p==NULL) return false; if(p->lchild!=NULL&&p->rchild!=NULL) { s=p->lchild; while(s->rchild!=NULL) { f=s; s=s->rchild; } p->data=s->data; p=s; } if(p->lchild!=NULL) s=p->lchild; else s=p->rchild; if(p==root) root=s; else if(s->data<f->data) f->lchild=s; else f->rchild=s; free(p); return true;} Prim算法(求最小生成树)const int MaxInt=INT_MAX;const int n=6;typedef int AdjMatrix[n][n];typedef struct{ int fromVex,toVex; int weight; };typedef TreeEdgeNode MST[n-1];void PrimMST(AdjMatrix G,MST T,int rt){//从顶点rt出发构造G的最小生成树T,rt为根 TreeEdgeNode e; int i,k=0,min,minpos,v; for(i=0;i<n;i++) { if(i!=rt) { T[k].fromVex=rt; T[k].toVex=i; T[k++].weight=G[rt][i]; } } for(k=0;k<n-1;k++) { min=MaxInt; for(i=k;i<n-1;i++) { if(T[i].weight<min) { min=T[i].weight; minpos=i; } } if(min==MaxInt) { cout<<"Graph is disconnected!"<<endl; exit(0); } e=T[minpos]; T[minpos]=T[k]; T[k]=e; v=T[k].toVex; for(i=k+1;i<n-1;i++) { if(G[v][T[i].toVex]<T[i].weight) { T[i].weight=G[v][T[i].toVex]; T[i].fromVex=v; } } }} 拓扑排序void topsort(hdnodes graph[],int n){ int i,j,k,topl; node_pointer ptr; top=-1; for(i=0;i<n;i++) { if(!graph[i].count) { graph[i].count=top; top=i; } } for(i=0;i<n;i++) { if(top==-1) { fprintf(stderr,"\\n graph has a cycle!\\n"); exit(1); } else{ j=top; top=-1; printf("v%d",j); for(ptr=graph.link;ptr;ptr=ptr->link)
- 数据结构&算法
- 数据结构算法
- 数据结构算法
- 数据结构算法
- 数据结构算法
- 数据结构算法
- 数据结构算法
- 数据结构算法
- 数据结构 算法
- 数据结构-算法
- 数据结构算法
- 数据结构-算法
- 数据结构+算法
- 数据结构&算法
- 数据结构+算法
- 数据结构:算法
- 数据结构~算法
- 数据结构-算法
- delphi bmp scanline
- 简单的程序诠释C++ STL算法系列之十一:search_n
- 使用VBS程序自动配置计算机IP地址(改进版)
- android获取手机屏幕大小
- 使用宏定义,简单实现jni函数命名
- 数据结构算法
- C#类,类成员访问修饰符
- VPN加密算法大全
- Jsp页面使用JavaBeans
- Samsung Announced First Smart TV App Developed Using Adobe AIR
- java接口作用(2)
- 关于文件流的写入与读取总结
- IP组播与组播协议
- GCT进行时