資料結構筆記-部分演算法__資料結構

來源:互聯網
上載者:User

1.二叉樹先序遍曆遞迴演算法:

void PreOrder(BiTree T){

  if(T!=null){

    visit(T);

    PreOrder(T->lchild);

    PreOrder(T->rchild);

  }

}

二叉樹先序遍曆非遞迴演算法:

void PreOrder2(BiTree T){

  InitStack(S);

  BiTree p=T;

  while(p||IsEmpty(S)){

    if(p){

      visit(p);

      Push(S,p);

      p=p->lchild;

    }

    else{

      Pop(S,p);

      p=p->rchild;

    }

  }

}

2.二叉樹中序遍曆遞迴演算法:

void InOrder(BiTree T){

  if(T!=NULL){

    InOrder(T->lchild);

    visit(T);

    InOrder(T->rchild);

  }

}

二叉樹中序遍曆非遞迴演算法:

void InOrder2(BiTree T){

  InitStack(S);

  BiTree p=T;

  while(p||!IsEmpty(S)){

    if(p){

      Push(S,p);

      p=p->lchild;

}

else{

 Pop(S,p);

 visit(p);

 p=p->rchild;

}

  }

}

3.二叉樹後序遍曆遞迴演算法:

void PostOrder(BiTree T){

  if(T!=NULL){

    PostOrder(T->lchild);

    PostOrder(T->rchild);

    visit(T);

  }

}

二叉樹後序遍曆非遞迴演算法:

void PostOrder2(BiTree T){

  InitStack(S);

  BiTree p=T;

  BiTree r=NULL;

  while(p||!IsEmpty(S)){

    if(p){

      Push(S,p);

      p=p->lchild;

}

else{

 GetTop(S,p);

 if(p->rchild&&p->rchild!=r){

   p=p->rchild;

   Push(S,p);

   p=p->lchild;

 }

 else{

   Pop(S,p);

   visit(p);

   r=p;

   p=NULL;

 }

}

  }

}

4.二叉樹層序遍曆非遞迴演算法:

void LevelOrder(BiTree T){

  InitQueue(Q);

  BiTree p;

  EnQueue(Q,T);

  while(!IsEmpty(Q)){

    DeQueue(Q,p);

    visit(p);

    if(p->lchild!=NULL)

      EnQueue(Q,p->lchild);

    if(p->rchild!=NULL)

      EnQueue(Q,p->rchild);

  }

}

5.通過中序遍曆對二叉樹線索化的遞迴演算法:

void InThread(ThreadTree &p,ThreadTree &pre){

  if(p!=NULL){

    InThread(p->lchild,pre);

    if(p->lchild==NULL){

    p->lchild=pre;

    p->ltag=1;

    }

    if(pre!=NULL&&pre->rchild==NULL){

      pre->rchild=p;

      pre->rtag=1;

}

pre=p;

InThread(p->rchild,pre);

  }

}

6.圖的深度優先搜尋遞迴演算法:

void DFSTraverse(Graph 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){

  visit(v);

  visited[v]=TRUE;

  for(w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w))

    if(!visited[w]){

      DFS(G,w);

}

}

7. 圖的廣度優先搜尋非遞迴演算法:(藉助隊列)

void BFSTraverse(Graph G,Status(* Visit)(int v)){

  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>=0;w=NextAdjVex(G,u,w))

 if(!visited[w]){

   visited[w]=TRUE;

   Visit(w);

   EnQueue(Q,w);

 }        

 }

}

}

8.Primr演算法的簡單實現:

void Prim(G,T){

  T=∅;

  U={w};

  while((V-U)!=∅){

    設(u,v)是使u∈U與v∈(V-U),且權值最小的邊;

T=T∪{(u,v)};

U=U∪{v};

  }

}

9. 順序尋找演算法:

int search_seq(SSTable ST,KeyType key){

  ST.elem[0].key=key;

  for(i=ST.length;ST.elem[i]!=key;--i);

  return i;

}

10.折半尋找演算法:

int search_bin(SSTable ST,KeyType key){

  int low=1,high=ST.length;

  while(low<=high){

    mid=(low+high)/2;

    if(ST.elem[mid].key==key)

      r

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.