七八大吧 关注:6贴子:28
  • 10回复贴,共1

七八大,代码看这里

只看楼主收藏回复

等着


1楼2014-05-04 15:18回复
    二叉搜索树
    #include <iostream>using namespace std;struct BinSearchNode;typedef struct BinSearchNode *PBinSearchNode;struct BinSearchNode{ int key; PBinSearchNode llink,rlink;};typedef struct BinSearchNode *BinSearchTree;typedef BinSearchTree *PBinSearchTree;int search(PBinSearchTree ptree,int key,PBinSearchNode*position){ PBinSearchNode p,q; p=*ptree;q=p; while(p!=NULL){ q=p; if(p->key==key){*position=p;return 1;} else if(p->key>key)p=p->llink; else p=p->rlink; } *position=q;;return 0;}int insert(PBinSearchTree ptree,int key){ PBinSearchNode p,position; if(search(ptree,key,&position)==1)return 1; p=new BinSearchNode; p->key=key;p->llink=p->rlink=NULL; if(position==NULL)*ptree=p; else if(key<position->key)position->llink=p; else position->rlink=p; return 1;}void pre_order(PBinSearchNode p) { cout<<p->key<<" " ; if(p->llink!=NULL) pre_order(p->llink); if(p->rlink!=NULL) pre_order(p->rlink); }int main(){ PBinSearchTree tree1; tree1=new PBinSearchNode; int key; while(cin>>key) { insert(tree1,key); } pre_order(*tree1); return 0;}


    2楼2014-05-04 15:21
    回复
      2025-05-14 16:47:01
      广告
      堆结构
      #include <iostream>using namespace std;struct PriorityQueue{ int MAXNUM; int n; int pq[100000];};#define MAXNUM=100000typedef struct PriorityQueue *PPriorityQueue;void add_heap(PPriorityQueue papq,int x){ int i; for(i=papq->n;i>0&&papq->pq[(i-1)/2]>x;i=(i-1)/2) papq->pq[i]=papq->pq[(i-1)/2]; papq->pq[i]=x;papq->n++;} void removeMin_heap(PPriorityQueue papq){ int s,i,child,temp; cout<<papq->pq[0]<<endl; s=--papq->n; temp=papq->pq[s]; i=0;child=1; while(child<s){ if(child<s-1&&papq->pq[child]>papq->pq[child+1]) child++; if(temp>papq->pq[child]) {papq->pq[i]=papq->pq[child];i=child;child=2*i+1;} else break; } papq->pq[i]=temp; }int main(){ int m,i,type,u; PPriorityQueue p; p=new PriorityQueue; p->n=0; cin>>m; for(i=0;i<m;i++) { cin>>type; if(type==1) { cin>>u; add_heap(p,u); } if(type==2) removeMin_heap(p); } return 0;}


      3楼2014-05-04 15:22
      回复
        #include <iostream>#include <malloc.h>#define MAXINT 10000using namespace std;struct HtNode{ int ww; int parent,llink,rlink; };struct HtTree{ int m; int root; struct HtNode *ht; };typedef struct HtTree *PHtTree;PHtTree huffman(int m,int *w){ PHtTree pht; int i,j,x1,x2,m1,m2; pht=new HtTree; pht->ht=(struct HtNode*)malloc(sizeof(struct HtNode)*(2*m-1)); for(i=0;i<2*m-1;i++){ pht->ht[i].llink=-1;pht->ht[i].rlink=-1;pht->ht[i].parent=-1; if(i<m)pht->ht[i].ww=w[i];else pht->ht[i].ww=-1; } for(i=0;i<m-1;i++) { m1=MAXINT; m2=MAXINT; x1=-1;x2=-1; for(j=0;j<m+i;j++) if((pht->ht[j].ww<m1)&&(pht->ht[j].parent==-1)) { m2=m1; x2=x1; m1=pht->ht[j].ww; x1=j; } else if(pht->ht[j].ww<m2&&pht->ht[j].parent==-1) { m2=pht->ht[j].ww; x2=j; } pht->ht[x1].parent=m+i; pht->ht[x2].parent=m+i; pht->ht[m+i].ww=m1+m2; pht->ht[m+i].llink=x1; pht->ht[m+i].rlink=x2; } pht->root=2*m-2; return pht;}int main(){ int n,i,j,k,t=0; int a[100],b[100]; cin>>n; for(i=0;i<n;i++) { b[i]=0; cin>>a[i]; } PHtTree pht; pht=new HtTree; pht=huffman(n,a); for(j=0;j<n;j++) { k=j; while(pht->ht[k].parent!=-1) { k=pht->ht[k].parent; b[j]=b[j]+1; } } for(j=0;j<n;j++) t=t+a[j]*b[j]; cout << t<< endl; return 0;}


        4楼2014-05-04 15:23
        收起回复