1. //先序遍历非递归
    2. void PreOrder(BiTNode *bt){
    3. if(bt!=NULL){
    4. BiTNode *Stack[maxsize];
    5. int top=-1;
    6. BiTNode *p;
    7. Stack[++top]=bt;
    8. while(top!=-1||p!=NULL){
    9. p=Stack[top--];
    10. visit(p);
    11. if(p->rchild){
    12. Stack[++top]=p->rchild;
    13. }
    14. if(p->lchild)
    15. Stack[++top]=p->lchild;
    16. }
    17. }
    18. }
    19. //中序遍历非递归
    20. void InOrder(BiTNode *bt){
    21. if(bt!=NULL){
    22. BiTNode *Stack[maxsize];
    23. int top=-1;
    24. BiTNode *p=bt;
    25. while(top!=-1||p!=NULL){
    26. if(p){
    27. Stack[++top]=p;
    28. p=p->lchild;
    29. }
    30. else{
    31. p=Stack[top--];
    32. visit(p);
    33. p=p->rchild;
    34. }
    35. }
    36. }
    37. }
    38. //后序非递归
    39. typedef struct BiTNode{
    40. ElemType data;
    41. struct BiTNode *lchild,*rchild;
    42. int tag;
    43. }
    44. void PostOrder(BiTNode *bt){
    45. if(bt!=NULL){
    46. BiTNode *Stack[maxsize];
    47. int top=-1;
    48. BiTNode *p=bt;
    49. while(top!=-1||p!=NULL){
    50. if(p){
    51. Stack[++top]=p;
    52. p=p->lchild;
    53. }
    54. else{
    55. p=Stack[top--];
    56. if(p->rchild&&p->tag==0){
    57. Stack[++top]=p->rchild;
    58. p=p->rchild;
    59. }
    60. else{
    61. visit(p);
    62. p->tag=1;
    63. p=NULL;
    64. }
    65. }
    66. }
    67. }
    68. }
    1. //层序遍历
    2. typedef struct Queue{
    3. BiTNode *Q[maxsize];
    4. int front,rear;
    5. }Queue;
    6. void LevelOrder(BiTNode *bt){
    7. if(bt!=NULL){
    8. Queue Q;
    9. InitQueue(Q);
    10. BiTNode *P;
    11. EnQueue(Q,bt);
    12. while(!isEmpty(Q)){
    13. DeQueue(Q,p);
    14. visit(p);
    15. if(p->lchild)
    16. EnQueue(Q,p->lchild);
    17. if(p->rchild)
    18. EnQueue(Q,p->rchild);
    19. }
    20. }
    21. }
    22. //手动模拟队列
    23. void LevelOrder(BiTNode *bt){
    24. if(bt!=NULL){
    25. BiTNode *Q[maxsize];
    26. int front=rear=0;
    27. BiTNode *p;
    28. rear=(rear+1)%maxsize;
    29. Q[rear]=bt;
    30. while(front!=rear){
    31. front=(front+1)%maxsize;
    32. p=Q[front];
    33. visit(p);
    34. if(p->lchild){
    35. rear=(rear+1)%maxsize;
    36. Q[rear]=p->lchild;
    37. }
    38. if(p->rchild){
    39. rear=(rear+1)%maxsize;
    40. Q[rear]=p->rchild;
    41. }
    42. }
    43. }
    44. }
    1. #define MaxVertexNum 100
    2. //邻接表
    3. typedef struct ArcNode{//边表结点
    4. int adjvex;//边指向顶点
    5. struct ArcNode *next;
    6. }ArcNode;
    7. typedef struct VNode{//顶点表结点
    8. VertexType data;//顶点数据
    9. ArcNode *first;
    10. }VNode.AdjList[MaxVertexNum];
    11. typedef struct{
    12. AdjList vertices;//邻接表
    13. int vexnum,arnnum;//顶点数,边数
    14. }ALGraph;
    15. //邻接矩阵
    16. typedef char VertexType;//顶点数据类型
    17. typedef int EdgeType;//权值类型
    18. typedef struct{
    19. VertexType Vex[MaxVertexNum];//顶点表
    20. EdgeType Edge[MaxVertexNum][MaxVertexNum];//邻接矩阵
    21. int vexnum,arcnum;
    22. }MGraph;
    23. //广度优先遍历
    24. bool visited[max_vertex_num];
    25. void BFSTrave(Graph G){
    26. for(int i=0;i<G.vexnum;i++){
    27. visited[i]=false;
    28. }
    29. Queue Q;
    30. InitQueue(Q);
    31. for(i=0;i<G.vexnum;i++){
    32. if(!visited[i]){
    33. BFS(G,i);
    34. }
    35. }
    36. }
    37. void BFS(Graph G,int v0){
    38. visit(v0);
    39. visited[v0]=true;
    40. EnQueue(Q,v0);
    41. while(!isEmpty(Q)){
    42. DeQueue(Q,v);
    43. for(int w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w)){
    44. if(!visited(w)){
    45. visit(w);
    46. visited[w]=true;
    47. EnQueue(Q,w);
    48. }
    49. }
    50. }
    51. }
    52. //深度优先搜索
    53. void DFSTrave(Graph G){
    54. for(int i=0;i<G.vexnum;i++)
    55. visited[i]=false;
    56. for(i=0;i<G.vexnum;i++){
    57. if(!visited[i])
    58. DFS(G,i);
    59. }
    60. }
    61. void DFS(Graph,int v0){
    62. visit(v0);
    63. visited[v0]=true;
    64. for(w=FirstNeighbor(G,v0);w>=0;w=NextNeighbor(G,v0,w)){
    65. if(!visited[w])
    66. visit(w);
    67. DFS(G,w);
    68. }
    69. }
    70. //借助栈
    71. void DFS(Graph G,int v0){
    72. Stack S;
    73. initStack(S);
    74. for(int i=0;i<G.vexnum;i++)
    75. visited[i]=false;
    76. Push(S,v0);
    77. visited[v0]=true;
    78. while(!isEmpty(S)){
    79. int k=Pop(S);
    80. visit(k);
    81. for(int w=FirstNeighbor(G,k);w>=0;w=NextNeighbor(G,k,w)){
    82. if(!visited[w]){
    83. Push(S,w);
    84. visited[w]=true;
    85. }
    86. }
    87. }
    88. }
    1. //冒泡排序
    2. void swap(int a,int b){
    3. int temp=a;
    4. a=b;
    5. b=temp;
    6. }
    7. void BubbleSort(int A[],int n){
    8. for(int i=0;i<n;i++){
    9. bool flag=false;
    10. for(int j=n-1;j>i;j--){
    11. if(A[j-1]>A[j]){
    12. swap(A[j-1],A[j]);
    13. flag=true;
    14. }
    15. }
    16. if(flag==false)
    17. return;
    18. }
    19. }
    20. //快排
    21. int Partition(int A[],int low,int high){
    22. int pivot=A[low];
    23. while(low<high){
    24. while(low<high&&A[high]>=pivot)
    25. --high;
    26. A[low]=A[high];
    27. while(low<high&&A[low]<=pivot)
    28. ++low;
    29. A[high]=A[low];
    30. }
    31. A[low]=pivot;
    32. return low;
    33. }
    34. void QuickSort(int A[],int low,int high){
    35. if(low<high){
    36. int pivotpos=Partition(A,low,high);
    37. QuickSort(A,low,pivotpos-1);
    38. QuickSort(A,pivotpos+1,high);
    39. }
    40. }
    1. #define maxsize 100
    2. //邻接矩阵
    3. typedef struct{
    4. VertexType Vex[maxsize];//顶点表
    5. EdgeType Edge[maxsize][maxsize];//邻接矩阵
    6. int vexnum,arcnum;//顶点数,边数
    7. }MGraph;
    8. //邻接表
    9. typedef struct ArcNode{//边表结点
    10. int adjvex;//边指向顶点位置
    11. struct ArcNode *next;//指向下一条边的指针
    12. InfoType info;//边的权值
    13. }ArcNode;
    14. typedef struct VNode{//顶点表结点
    15. VertexType data;//顶点信息
    16. ArcNode *first;//指向第一条依附该顶点的边的指针
    17. }VNode,AdjList[maxsize];
    18. typedef struct{
    19. AdjList vertices;//邻接表
    20. int vexnum,arcnum;
    21. }ALGraph;