二叉树的深度优先遍历与广度优先遍历
2014年11月22日
二叉树的深度优先遍历就是二叉树地前序遍历.二叉树的广度遍历就是层序遍历.(最后面有完整的代码).
一.深度优先遍历(DFS)
深度优先遍历与前序遍历的结果是一样的.那么递归实现应该是
1 2 3 4 5 6 7 8 9 10 11 |
void dfs_r(T) { if (T == NULL) { return ; } print(T.k); dfs_r(T.left); dfs_r(T.right); } |
与之对应的非递归算法是,使用栈的先进后出的特性.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void dfs(T) { push(q,T); while (!is_empty(q)) { n = pop(q); print(n.k); if (n.right) { push(q,n.right); } if (n.left) { push(q,n.left); } } } |
二.广度优先遍历(BFS)
二叉树的广度优先遍历其实很简单,大概的思路是访问一个结点时,将它的子结点放入队列后面,当本层访问结束后,队列中就剩下下一层的所有结点,直到队列中没有结点为止.
可以看到队列的前部是当前层剩下的结点,队列的后部是当前层已经访问过的结点的子结点.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void bfs(T) { push(s,T); while (!is_empty(s)) { n = pop(s); print(n.k); if (n.left) { push(s,n.left); } if (n.right) { push(s,n.right); } } return ; } |
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
#include <stdio.h> #include <stdlib.h> /*implement list*/ typedef struct link_s link_t; typedef struct link_s list_t; struct link_s { link_t *next; link_t *prev; }; /*list init*/ static void list_init(list_t *l) { l->next = l->prev = l; } /*list insert tail*/ static void list_insert_t(list_t *l,link_t *n) { n->prev = l->prev; n->next = l; n->prev->next = n; n->next->prev = n; } /*list insert head*/ static void list_insert_h(list_t *l,link_t *n) { n->prev = l; n->next = l->next; n->prev->next = n; n->next->prev = n; } static link_t* list_remove(link_t *n) { n->next->prev = n->prev; n->prev->next = n->next; return n; } static int list_is_empty(list_t *l) { return l->prev == l; } /*binary tree*/ typedef struct bnode_s btree_t; typedef struct bnode_s bnode_t; struct bnode_s { int k; bnode_t *l; bnode_t *r; }; btree_t *btree_create(int *k,int *n,int m); void btree_bfs(btree_t *T); void btree_dfs(btree_t *T); btree_t *btree_create(int *k,int *n,int m) { btree_t *T; int i; i = *n; *n = i + 1; if (i > m || k[i] == -1) { return NULL; } T = malloc(sizeof(btree_t)); if (T == NULL) { exit(-2); } T->k = k[i]; T->l = btree_create(k,n,m); T->r = btree_create(k,n,m); return T; } /************** 1 / \ 2 5 / \ 3 4 (1). create a binary tree (2). BFS: 1 2 5 3 4 (3). DFS: 1 2 3 4 5 **************/ int main(int argc,char **argv) { btree_t *T; int k[] = {1,2,3,-1,-1,4,-1,-1,5,-1,-1}; /*-1: NULL*/ int p,n; n = sizeof(k) / sizeof(k[0]); p = 0; T = btree_create(k,&p,n); if (T == NULL) { fprintf(stderr,"create a binary tree failed!"); exit(-2); } btree_bfs(T); btree_dfs(T); return 0; } typedef struct node_s node_t; struct node_s{ link_t link; /*MUST be FIRST*/ bnode_t *node; }; static node_t *alloc_node(bnode_t *nd) { node_t *b; b = (node_t *)malloc(sizeof(node_t)); if (b == NULL) { return NULL; } b->node = nd; return b; } void free_node(node_t *bfs) { free(bfs); } /*BFS*/ void btree_bfs(btree_t *T) { node_t *n,*top; link_t q; list_init(&q); printf("BFS: "); /*push root*/ n = alloc_node(T); list_insert_t(&q,&n->link); while (!list_is_empty(&q)) { /*remove front*/ top = (node_t*)list_remove(q.next); printf("%d ",top->node->k); /*push left child*/ if (top->node->l) { n = alloc_node(top->node->l); list_insert_t(&q,&n->link); } /*push right child*/ if (top->node->r) { n = alloc_node(top->node->r); list_insert_t(&q,&n->link); } free_node(top); } printf("\n"); return ; } /*DFS*/ void btree_dfs(btree_t *T) { node_t *n,*top; link_t s; list_init(&s); printf("DFS: "); /*push root*/ n = alloc_node(T); list_insert_h(&s,&n->link); while (!list_is_empty(&s)) { top = (node_t*)list_remove(s.next); printf("%d ",top->node->k); /*push right*/ if (top->node->r) { n = alloc_node(top->node->r); list_insert_h(&s,&n->link); } /*push left*/ if (top->node->l) { n = alloc_node(top->node->l); list_insert_h(&s,&n->link); } free_node(top); } printf("\n"); return ; } |
分类: 算法导论