数据结构实验三 树的遍历生成树
廣州大學學生實驗報告
?
開課實驗室:計算機科學與工程實驗(電子樓418A) ????2019年4月19日
| 學院 | 計算機科學與教育軟件學院 | 年級、專業、班 | 計算機科學與技術 | 姓名 | ? | 學號 | ? | |
| 實驗課程名稱 | 數據結構實驗 | 成績 | ? | |||||
| 實驗項目名稱 | 實驗三 樹的的遍歷生成樹 | 指導老師 | 玲 | |||||
| 一、實驗目的 1、把圖轉化為程序能識別的鄰接矩陣; 2、理解圖的遍歷方法及對應的生成樹。 二、使用儀器、器材 微機一臺 操作系統:Win10 編程軟件:C++ 三、實驗內容及原理 1.圖的輸入:鄰接矩陣直接寫入源程序,或鍵盤輸入,或讀入數據文件 起始結點的輸入:運行時由鍵盤輸入 輸出:生成樹的邊,用結點的序偶表示, ?
GRAPH.H #pragma once //圖的鄰接表存儲結構 #define INF 32767????????????? //定義∞ #define? MAXV 100????????????? //最大頂點個數 #define? MAX 100?????????????? ??? //最大全局數組 #define MaxSize 100 typedef char InfoType; typedef int ElemType; typedef struct ANode { ??? int adjvex; ??? struct ANode *nextarc; ??? int weight; }ArcNode; typedef struct Vnode { ??? char info; ??? ArcNode *firstarc; }VNode; typedef struct { ??? VNode adjlist[MAXV]; ??? int n, e; }AdjGraph; //環形隊列 typedef struct { ??? ElemType data[MaxSize]; ??? int front, rear;????? //隊首和隊尾指針 } SqQueue; ? void CreateAdj(AdjGraph* &G, int A[MAXV][MAXV], int n, int e);//創建圖的鄰接表 void DispAdj(AdjGraph*G);//輸出鄰接表 void DestroyAdj(AdjGraph *&G);//銷毀鄰接表 void DFS(AdjGraph* G, int v);//深度優先遍歷生成樹 ? //--------------------------------------------------------- //--廣度優先遍歷中使用隊列的基本運算算法------------------- //--------------------------------------------------------- void InitQueue(SqQueue *&q); void DestroyQueue(SqQueue *&q); bool QueueEmpty(SqQueue *q); bool enQueue(SqQueue *&q, ElemType e);//進棧 bool deQueue(SqQueue *&q, ElemType &e); //--------------------------------------------------------- ? void BFS(AdjGraph *G, int v);//廣度優先遍歷生成樹 GRAPH.CPP #include"pch.h" #include "graph.h" #include"malloc.h" #include<iostream> using namespace std; ? void CreateAdj(AdjGraph *& G, int A[MAXV][MAXV], int n, int e) ??? { ???????? int i, j; ArcNode *p; ? ???????? for (i = 0; i < n; i++)????? //給鄰接表中所有頭節點指針置初值 ???????? { ???????????? G->adjlist[i].firstarc = NULL; ???????? } ???????? for (i = 0; i < n; i++)????? //檢查鄰接矩陣每個元素 ???????? { ???????????? for (j = n - 1; j >= 0; j--) ???????????? { ????????????????? if (A[i][j] != 0 && A[i][j] != INF)?? //存在一條邊 ????????????????? { ????????????????????? p = (ArcNode*)malloc(sizeof(ArcNode));? //創建一個結點p ????????????????????? p->adjvex = j;?? //?? 存放鄰接點 ????????????????????? p->weight = A[i][j];?? //存放權 ????????????????????? p->nextarc = G->adjlist[i].firstarc;?? //頭插法 ????????????????????? G->adjlist[i].firstarc = p; ????????????????? } ???????????? } ???????? } ???????? G->n = n; G->e = e; } ? void DispAdj(AdjGraph * G) { ??? int i; ??? ArcNode *p; ??? for (i = 0; i < G->n; i++) ??? { ???????? p = G->adjlist[i].firstarc; ???????? printf("%3d: ", i); ???????? while (p != NULL) ???????? { ???????????? printf("%3d[%d]→", p->adjvex, p->weight); ???????????? p = p->nextarc; ???????? } ???????? printf("∧\n"); ??? } } ? void DestroyAdj(AdjGraph *& G) { ??? int i; ??? ArcNode *pre, *p; ??? for (i = 0; i < G->n; i++)???????? //掃描所有的單鏈表 ??? { ???????? pre = G->adjlist[i].firstarc;? //p指向第i個單鏈表的首結點 ???????? if (pre != NULL) ???????? { ???????????? p = pre->nextarc; ???????????? while (p != NULL)???????? //釋放第i個單鏈表的所有邊結點 ???????????? { ????????????????? free(pre); ????????????????? pre = p; p = p->nextarc; ???????????? } ???????????? free(pre); ???????? } ??? } ??? free(G);?????????????????????? //釋放頭結點數組 } ? int? visited[MAX] = { 0 }; void DFS(AdjGraph* G, int v)?? //深度優先遍歷 { ??? ArcNode*p; ??? visited[v] = 1;?? //置已訪問標記 ??? //cout << v << endl;? //輸出被訪問頂點的編號 ??? p = G->adjlist[v].firstarc;? //p指向頂點v的第一個鄰接點 ??? while (p != NULL) ??? { ???????? if (visited[p->adjvex] == 0) ???????? { ???????????? cout << "(" << v << "," << p->adjvex << ")" << endl; ???????????? DFS(G, p->adjvex); ???????? } ???????? p = p->nextarc;?? //p指向頂點v的下一個鄰接點 ??? } } ? void InitQueue(SqQueue *& q) { ??? q = (SqQueue *)malloc(sizeof(SqQueue)); ??? q->front = q->rear = 0; } ? void DestroyQueue(SqQueue *& q) { ??? free(q); } ? bool QueueEmpty(SqQueue * q) { ??? return(q->front == q->rear); } ? bool enQueue(SqQueue *& q, ElemType e) { ??? if ((q->rear + 1) % MaxSize == q->front)??? //隊滿上溢出 ???????? return false; ??? q->rear = (q->rear + 1) % MaxSize; ??? q->data[q->rear] = e; ??? return true; } ? bool deQueue(SqQueue *& q, ElemType & e) { ??? if (q->front == q->rear)??????????????? //隊空下溢出 ???????? return false; ??? q->front = (q->front + 1) % MaxSize; ??? e = q->data[q->front]; ??? return true; } ? void BFS(AdjGraph * G, int v)? //廣度優先遍歷 { ??? int w, i; ??? ArcNode *p; ??? SqQueue *qu;??????????????????????????? //定義環形隊列指針 ??? InitQueue(qu);????????????????????????????? //初始化隊列 ??? int visited[MAXV];??????????? ????????? //定義頂點訪問標志數組 ??? for (i = 0; i < G->n; i++) visited[i] = 0;?????? //訪問標志數組初始化 ??? //printf("%2d ", v); ?????????????????????? //輸出被訪問頂點的編號 ??? visited[v] = 1;????????????? ?????????????? //置已訪問標記 ??? enQueue(qu, v); ??? while (!QueueEmpty(qu))?????? ????????? //隊不空循環 ??? { ???????? deQueue(qu, w);???????????????????????? //出隊一個頂點w ???????? p = G->adjlist[w].firstarc; ??????????? //指向w的第一個鄰接點 ??? ??? while (p != NULL)?????????????????????? //查找w的所有鄰接點 ???????? { ???????????? if (visited[p->adjvex] == 0) ????? //若當前鄰接點未被訪問 ???????????? { ????????????????? cout << "(" << w << "," << p->adjvex << ")" << endl; ????????????????? //printf("%2d ", p->adjvex);? //訪問該鄰接點 ????????????????? visited[p->adjvex] = 1;??????? //置已訪問標記 ????????????????? enQueue(qu, p->adjvex);??????? //該頂點進隊 ???????????? } ???????????? p = p->nextarc;????????????? ????? //找下一個鄰接點 ???????? } ??? } ??? printf("\n"); } ? MAIN.CPP ? #include "pch.h" #include <iostream> #include"graph.h" using namespace std; ? int main() { ??? AdjGraph *G=new AdjGraph; ??? int A[MAXV][MAXV] = { {0,1,1,1,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,0,0,0}, ???????????? ????????????? {1,0,0,1,0,1,1,0,0,0,0},{1,0,1,0,0,0,0,1,0,0,0}, ??? ????????????????????? {0,1,0,0,0,0,0,0,0,0,0},{0,1,1,0,0,0,0,0,0,0,0}, ??? ????????????????????? {0,0,1,0,0,0,0,1,1,1,0},{0,0,0,1,0,0,1,0,0,0,1}, ??? ??????????????? ??????{0,0,0,0,0,0,1,0,0,0,0},{0,0,0,0,0,0,1,0,0,0,0}, ??? ????????????????????? {0,0,0,0,0,0,0,1,0,0,0} } ??? ; ??? int n = 11, e = 12; ??? CreateAdj(G, A, n, e);???????? //建立《教程》中圖8.1(a)的鄰接表 ??? //printf("圖G的鄰接表:\n"); ??? //DispAdj(G);????????????????? //輸出鄰接表G ??? cout << "請輸入起始結點" << endl; ??? int a; ??? cin >> a; ??? printf("深度優先序列(遞歸)生成樹:"); printf("\n"); DFS(G, a); printf("\n"); ??? printf("廣度優先序列生成樹:"); printf("\n"); BFS(G, a); printf("\n"); ??? DestroyAdj(G);???????????????? //銷毀鄰接表 ??? return 1; } ? 五、實驗結果及分析 從3出發,分別進行深度優先生成樹和廣度優先生成樹。 | ||||||||
| 從0出發,分別進行深度優先生成樹和廣度優先生成樹。 ? | ||||||||
總結
以上是生活随笔為你收集整理的数据结构实验三 树的遍历生成树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: phpStorm解释器与服务器配置(解决
- 下一篇: 计算机综合能力知识,通信工程师中级综合能