二叉排序树

DNA图谱 / 问答 / 标签

数据结构 :利用二叉排序树对顺序表进行排序

我也是参考别人的,你参考下:#include<iostream>#include<cstdlib>#include<iomanip>using namespace std;const int size=1000;struct BitNode{ int date; BitNode *lchild,*rchild;//左右孩子 }*BiTree;struct Stack{ BitNode *elem[size]; int length;}S;struct List{//长度 int elem[size]; int length;}L;int InitStack(Stack &L){ L.length=0; return 0;}int Push(Stack &L,BitNode *e){//二叉排列数的插入 L.elem[L.length]=e; L.length++; return 0;}int Pop(Stack &L,BitNode *(&e)){//删除L的栈顶 元素,用e返回其值 e=L.elem[L.length-1]; L.length--; return 0;}int InitList(List &L){ L.length=0; return 0;}int ListInsert(int e,List &L){ L.elem[L.length]=e; L.length++; return 0;}bool SearchBst(BitNode *T,int key,BitNode *f,BitNode *(&p))//bool型变量,true or false.{ if(!T){p=f;return false;} //else if(T->date==key&&T->lchild!=NULL&&T->lchild->date!=key) //{p=T; return true;} else if(T->date<key) {return SearchBst(T->rchild,key,T,p);} else return SearchBst(T->lchild,key,T,p);}int SearchBst_T(BitNode *T,int key,BitNode *f,BitNode *(&p),BitNode *(&q)){ if(!T){p=f;return false;} else if(T->date==key) {q=f;p=T; return true;} else if(T->date<key) {return SearchBst_T(T->rchild,key,T,p,q);} else return SearchBst_T(T->lchild,key,T,p,q);}int InsertBst(BitNode *(&T),int key){ BitNode *s,*p=NULL; if(!SearchBst(T,key,NULL,p)){ s=new BitNode; s->date=key; s->lchild=s->rchild=NULL; if(!p) T=s; else if(p->date<key) p->rchild=s; else p->lchild=s; return true; } else return false;}void output(BitNode *T,List &L,int &i){ ListInsert(T->date,L);}int InorderTraverse(BitNode *(&T)){ BitNode *p; InitStack(S); p=T; int i=0; while(p||S.length!=0){ if(p){Push(S,p);p=p->lchild;} else{ Pop(S,p); output(p,L,i); p=p->rchild; } } return true; }int Delete(BitNode *(&T)) { BitNode *p,*q,*s; int key; cout<<"请输入删除的数:"; //删除节点 cin>>key; if(SearchBst_T(T,key,NULL,p,q)) { if(!p->lchild) { if(q==NULL) { T=p->rchild; } else if(p==q->lchild) { q->lchild=p->rchild; } else if(p==q->rchild) { q->rchild=p->rchild; } delete(p); } else{ q=p; s=p->lchild; while(s->rchild){ q=s; s=s->rchild; } p->date=s->date; if(q!=p){ q->rchild=s->lchild; } else{ q->lchild=s->lchild; } delete s; return true; } } else{ cout<<"不存在此数!"; return false; }}int welcome(){ cout<<"—————请选择数字—————"<<endl; cout<<"**** 1 二叉排序伪随机数******"<<endl; cout<<"**** 2 二叉排序树的插入******"<<endl; cout<<"**** 3 二叉排序树的删除******"<<endl; cout<<"**** 4 退出******************"<<endl; cout<<"******************************"<<endl;}int main(){ int n,D,k,flag; welcome(); while(cin>>flag) { InitList(L); if(flag==1) { cout<<"请输入随即数的个数:"; cin>>n; for(int i=0;i<n;i++) { D=1+rand()%1000; ListInsert(D,L); } BiTree=NULL; for(int j=0;j<L.length;j++) InsertBst(BiTree,L.elem[j]); cout<<"---排序前---"; for(k=0;k<L.length;k++) { if(k%10==0) cout<<endl; cout<<left<<setw(6)<<L.elem[k]; } InitList(L); InorderTraverse(BiTree); cout<<endl<<"---排序后---"; for(k=0;k<L.length;k++) { if(k%10==0) cout<<endl; cout<<left<<setw(6)<<L.elem[k]; } cout<<endl<<endl; } else if(flag==2) { cout<<"请输入插入的数:"; cin>>D; InsertBst(BiTree,D); InorderTraverse(BiTree); cout<<"---插入后---"; for(k=0;k<L.length;k++) { if(k%10==0) cout<<endl; cout<<left<<setw(6)<<L.elem[k]; } cout<<endl<<endl; } else if(flag==3){ Delete(BiTree); InorderTraverse(BiTree); cout<<endl<<"---删除后---"; for(k=0;k<L.length;k++) { if(k%10==0) cout<<endl; cout<<left<<setw(6)<<L.elem[k]; } cout<<endl<<endl; } else break;} return 0;}