用C语言完成 1.编写程序建立深度遍历序列为a,b,c,e,f,d,广度遍历序列为a,b,d,e,c,f(递归算法) 2.编写图的深度优先遍历算法,并输出从顶点 a 开始的深度优先遍历序列。 3.编写图的广度优先遍历算法,并输出从顶点 a 开始的广度优先遍历算法。
时间: 2024-05-10 08:16:35 浏览: 97
图的深度广度遍历程序C语言实现
对于第一个问题,由于深度和广度遍历序列唯一,因此我们可以根据给定的序列构造出一棵树,然后进行深度和广度优先遍历。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
char val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
int find(char* arr, int start, int end, char val) {
for (int i = start; i <= end; i++) {
if (arr[i] == val) {
return i;
}
}
return -1;
}
TreeNode* buildTree(char* inorder, int inStart, int inEnd, char* levelorder, int levelStart, int levelEnd) {
if (inStart > inEnd || levelStart > levelEnd) {
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = levelorder[levelStart];
int index = find(inorder, inStart, inEnd, root->val);
root->left = buildTree(inorder, inStart, index - 1, levelorder, levelStart + 1, levelStart + index - inStart);
root->right = buildTree(inorder, index + 1, inEnd, levelorder, levelStart + index - inStart + 1, levelEnd);
return root;
}
void dfs(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%c ", root->val);
dfs(root->left);
dfs(root->right);
}
void bfs(TreeNode* root) {
if (root == NULL) {
return;
}
TreeNode* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
TreeNode* node = queue[front++];
printf("%c ", node->val);
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
}
int main() {
char inorder[] = {'a', 'b', 'c', 'e', 'f', 'd'};
char levelorder[] = {'a', 'b', 'd', 'e', 'c', 'f'};
TreeNode* root = buildTree(inorder, 0, 5, levelorder, 0, 5);
dfs(root); // a b c e f d
printf("\n");
bfs(root); // a b c d e f
return 0;
}
```
对于第二个问题,图的深度优先遍历可以用递归实现。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct Graph {
int n;
int adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
int visited[MAX_VERTICES];
void dfs(Graph* g, int v) {
visited[v] = 1;
printf("%d ", v);
for (int i = 0; i < g->n; i++) {
if (g->adj[v][i] && !visited[i]) {
dfs(g, i);
}
}
}
int main() {
Graph g = {6, {{0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 1, 0}}};
dfs(&g, 0); // 0 1 2 4 5 3
return 0;
}
```
对于第三个问题,图的广度优先遍历可以用队列实现。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct Graph {
int n;
int adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
int visited[MAX_VERTICES];
void bfs(Graph* g, int v) {
visited[v] = 1;
printf("%d ", v);
int queue[MAX_VERTICES];
int front = 0, rear = 0;
queue[rear++] = v;
while (front < rear) {
int u = queue[front++];
for (int i = 0; i < g->n; i++) {
if (g->adj[u][i] && !visited[i]) {
visited[i] = 1;
printf("%d ", i);
queue[rear++] = i;
}
}
}
}
int main() {
Graph g = {6, {{0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 1},
{1, 0, 0, 0, 1, 0}}};
bfs(&g, 0); // 0 1 3 2 5 4
return 0;
}
```
阅读全文