图形的遍历

本文介绍了图形遍历的方法,包括深度优先搜索(DFS)和广度优先搜索(BFS)。通过这两种方法,可以判断图形是否连通并找到连通单元和路径。深度优先搜索从一个顶点开始,标记已访问顶点,然后递归遍历未访问的相邻顶点。广度优先搜索使用队列,从一个顶点开始,依次访问其所有相邻顶点,然后继续遍历这些顶点的相邻顶点,直至队列为空。
摘要由CSDN通过智能技术生成

一个图形G=(V,E),存在某一顶点v,希望从v开始,通过此顶点相邻的顶点而去访问G中其他顶点直达全部的顶点遍历完毕。在遍历的过程中可能会重复经过某些顶点及边线,经由图形的遍历可以判断该图形是否连通,并找出连通单元和路径。
图形遍历有两种方法:

  • 深度优先搜索Deep-First-Search
  • 广度优先搜索Breadth-First-Search

一、深度优先搜索

从图形的某一顶点开始遍历,被访问过的顶点做上已访问的标记,接着从与此顶点相邻且未访问过的顶点中选择任意一个顶点,并做上已访问的记号,再以该顶点为新的起点进行深度优先搜索遍历。

我们以下图为例进行代码实现:
这里写图片描述

定义public static void DFS(int current)实现深度优先搜索,定义数组run[]来标记顶点的遍历情况,1表示已遍历,0表示未遍历。图使用邻接表进行存放,从选定顶点的链表的头结点进行判断,若该顶点未遍历,则递归调用该函数从该节点开始进行深度优先遍历,否则指针后移寻找该顶点未被遍历的顶点。

public static void DFS(int current)代码如下:

    public static void DFS(int current) {
        run[current]=1;                                        //改顶点设为已遍历
        System.out.print("["+current+"]");                     //打印顶点

        while(head[current].first!=null) {                     //从链表头结点开始
            if(run[head[current].first.data]==0)               //若该顶点未遍历就进行DFS递归调用
                DFS(head[current].first.data);
            head[current].first=head[current].first.next;      //否则指针后移
        }
    }

主要代码如下:(Node类和GraphLink类的定义见博客图表示法中的邻接表法
http://blog.csdn.net/zd454909951/article/details/78896793

public class Test {
    //静态变量可全局使用
    public static int[] run=new int[9];                     //判断顶点是否已遍历,1代表遍历,0代表未遍历
    public static GraphLink[] head=new GraphLink[9];        //声明链接表数组

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用EasyX图形库实现二叉树的图形遍历的示例代码。其中包括了前序遍历、中序遍历和后序遍历图形化展示。 ```c++ #include <graphics.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> //树结构体 typedef struct tree { int data; struct tree *left; struct tree *right; }Tree; //创建二叉树 Tree *createTree() { Tree *p; int data; scanf("%d",&data); if(data == -1) p = NULL; else { p = (Tree*)malloc(sizeof(Tree)); p->data = data; p->left = createTree(); p->right = createTree(); } return p; } //前序遍历图形化展示 void preOrder(Tree *p, int x, int y, int coe) { if(p) { char str[10]; settextcolor(RED); sprintf(str, "%d", p->data); outtextxy(x, y, str); //输出当前节点的值 if(p->left) { settextcolor(YELLOW); sprintf(str, "%d", p->left->data); line(x, y, x-coe, y+coe); //绘制连线 outtextxy(x-coe-20, y+coe-20, str); //输出左子节点的值 } preOrder(p->left, x-coe, y+coe, coe/2); //递归遍历左子树 if(p->right) { settextcolor(YELLOW); sprintf(str, "%d", p->right->data); line(x, y, x+coe, y+coe); //绘制连线 outtextxy(x+coe+20, y+coe-20, str); //输出右子节点的值 } preOrder(p->right, x+coe, y+coe, coe/2); //递归遍历右子树 } } //中序遍历图形化展示 void inOrder(Tree *p, int x, int y, int coe) { if(p) { inOrder(p->left, x-coe, y+coe, coe/2); //递归遍历左子树 char str[10]; settextcolor(RED); sprintf(str, "%d", p->data); outtextxy(x, y, str); //输出当前节点的值 if(p->left) { settextcolor(YELLOW); sprintf(str, "%d", p->left->data); line(x, y, x-coe, y+coe); //绘制连线 outtextxy(x-coe-20, y+coe-20, str); //输出左子节点的值 } if(p->right) { settextcolor(YELLOW); sprintf(str, "%d", p->right->data); line(x, y, x+coe, y+coe); //绘制连线 outtextxy(x+coe+20, y+coe-20, str); //输出右子节点的值 } inOrder(p->right, x+coe, y+coe, coe/2); //递归遍历右子树 } } //后序遍历图形化展示 void postOrder(Tree *p, int x, int y, int coe) { if(p) { postOrder(p->left, x-coe, y+coe, coe/2); //递归遍历左子树 postOrder(p->right, x+coe, y+coe, coe/2); //递归遍历右子树 char str[10]; settextcolor(RED); sprintf(str, "%d", p->data); outtextxy(x, y, str); //输出当前节点的值 if(p->left) { settextcolor(YELLOW); sprintf(str, "%d", p->left->data); line(x, y, x-coe, y+coe); //绘制连线 outtextxy(x-coe-20, y+coe-20, str); //输出左子节点的值 } if(p->right) { settextcolor(YELLOW); sprintf(str, "%d", p->right->data); line(x, y, x+coe, y+coe); //绘制连线 outtextxy(x+coe+20, y+coe-20, str); //输出右子节点的值 } } } int main() { initgraph(800, 600); //初始化图形窗口 setbkcolor(WHITE); settextstyle(20, 0, "宋体"); setlinestyle(PS_SOLID, 2); outtextxy(350, 50, "二叉树遍历图形演示"); outtextxy(100, 150, "请输入二叉树节点的值(-1表示空节点):"); Tree *root = createTree(); //创建二叉树 //前序遍历图形化展示 outtextxy(50, 300, "前序遍历:"); preOrder(root, 400, 300, 200); //中序遍历图形化展示 outtextxy(50, 400, "中序遍历:"); inOrder(root, 400, 400, 200); //后序遍历图形化展示 outtextxy(50, 500, "后序遍历:"); postOrder(root, 400, 500, 200); getch(); closegraph(); //关闭图形窗口 return 0; } ``` 在运行程序后,用户可以输入二叉树的节点值,程序将会自动创建并展示二叉树的三种遍历方式的图形化展示。用户可以通过观察图形化展示,更直观地理解二叉树的遍历方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值